This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvnplayer.py
188 lines (147 loc) · 5.13 KB
/
tvnplayer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#!/usr/bin/env python
"""Usage: tvnplayer [-o FILE | -p COMMAND] [--debug] [URL]
-h --help Show this help
-o --output FILE Write stream to FILE; if - is used as FILE,
stream will be written to standard output
-p --player COMMAND Pass stream URL to player
--debug Show HTTP responses
The default action is to print the stream URL to standard output.
"""
from __future__ import print_function
from collections import OrderedDict
import contextlib
import json
import os
import re
import subprocess
import sys
try:
from urlparse import urlsplit
except ImportError:
from urllib.parse import urlsplit
from docopt import docopt
import requests
try:
input = raw_input
except NameError:
pass
api_url = 'http://api.tvnplayer.pl/api/'
api_params = {
'authKey': '453198a80ccc99e8485794789292f061',
'format': 'json',
'platform': 'ConnectedTV',
'terminal': 'Samsung2',
'v': '3.6',
}
api_headers = {
'User-Agent':
'Mozilla/5.0 (SmartHub; SMART-TV; U; Linux/SmartTV; Maple2012) '
'AppleWebKit/534.7 (KHTML, like Gecko) SmartTV Safari/534.7',
}
http_max_retries = 3
http_content_chunk_size = 4096
profiles = ('hd', 'bardzo wysoka', 'wysoka', 'sd', 'standard')
video_id_re = re.compile(r',([0-9]+)$')
def print_status(message):
print('[tvnplayer] {}'.format(message), file=sys.stderr)
def response_callback(response, *args, **kwargs):
print_status('[{}|{}] {}'.format(response.request.method.lower(),
response.status_code, response.url))
def assert_response(response, status_code=requests.codes.ok, debug=False):
if debug:
headers = json.dumps(dict(response.headers), sort_keys=True, indent=2)
print_status('[headers]\n' + headers)
try:
body = json.dumps(response.json(), sort_keys=True, indent=2)
except ValueError:
body = response.text
print_status('[body]\n' + body)
if response.status_code != status_code:
print_status('Request returned {} (expected {})'.format(
response.status_code, status_code))
sys.exit(1)
@contextlib.contextmanager
def open_output(name):
if name == '-':
output_file = os.fdopen(sys.stdout.fileno(), 'wb', 0)
else:
output_file = open(name, 'wb')
try:
yield output_file
finally:
if name != '-':
output_file.close()
def readable_size(bytes):
for unit in ['B', 'KB', 'MB', 'GB']:
if bytes < 1024.0:
return '{:.1f} {}'.format(bytes, unit)
bytes /= 1024.0
return '{:.1f} TB'.format(bytes)
def main():
args = docopt(__doc__)
url = args['URL'] or input('Video URL: ')
debug = args['--debug']
match = video_id_re.search(urlsplit(url).path)
if not match:
print_status('This URL seems to be invalid...')
sys.exit(1)
session = requests.Session()
session.headers.update(api_headers)
session.hooks.update({'response': response_callback})
session.mount('http://',
requests.adapters.HTTPAdapter(max_retries=http_max_retries))
params = {'m': 'getItem', 'id': int(match.group(1))}
params.update(api_params)
response = session.get(api_url, params=params)
assert_response(response, debug=debug)
video = response.json().get('item')
if not video:
print_status('Video not available')
sys.exit(1)
streams = video['videos']['main']['video_content']
if not streams:
print_status('No video streams available')
sys.exit(1)
streams = OrderedDict([(stream['profile_name'].lower(),
stream.get('url') or stream['src'])
for stream in streams])
print_status('Found {} video stream(s)'.format(len(streams)))
for profile in profiles:
try:
stream_url = streams[profile]
except KeyError:
pass
else:
break
else:
stream_url = streams.values()[0]
if stream_url.startswith('http://tvnplayer.pl'):
response = session.get(stream_url)
assert_response(response, debug=debug)
stream_url = response.text
output = args['--output']
player = args['--player']
if output:
response = session.get(stream_url, stream=True)
assert_response(response)
with open_output(output) as output_file:
size = int(response.headers['content-length'])
print_status("Downloading {} to '{}'".format(readable_size(size),
output))
for chunk in response.iter_content(http_content_chunk_size):
if not chunk:
break
output_file.write(chunk)
else:
response = session.head(stream_url, allow_redirects=True)
assert_response(response, debug=debug)
if player:
print_status("Launching '{}'".format(player))
subprocess.call([player, response.url])
else:
print(response.url)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass