This repository has been archived by the owner on Aug 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathiview-cli
executable file
·196 lines (161 loc) · 5.26 KB
/
iview-cli
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
189
190
191
192
193
194
195
196
#!/usr/bin/env python
import sys, os, getopt, urllib2
import iview.config
def usage():
print("""usage:
-i, --index prints the iView index (number is the series ID)
-s, --series <id> get the list of episodes for the series
-p, --programme prints the entire iView programme at once
(warning: very slow!)
-d, --download <url> download an episode
(pass the url you got from -s or -p)
-t, --subtitles <url> download subtitles in SRT format for an episode
(pass the same url as for --download)
-o, --output specify a file to output to (use - for stdout)
-a, --print-auth print debug iView auth information
-x, --proxy <host:port> used specified SOCKS proxy
-h, --help this page""")
def config():
try:
iview.comm.get_config()
except urllib2.HTTPError, error:
print 'Error: could not retrieve an important configuration file from iView.'
print 'Please make sure you are connected to the Internet.\n'
print 'If iView works fine in your web browser, the iView API has possibly changed.' \
'Try to find an updated version of this program or contact the author.'
print '\nDebug: could not load URL', error.url
sys.exit(1)
def programme():
""" Downloads the iView index, and then prints each item in all series.
This is a slow function. Using index() and subsequently cherrypicking
a series to pass to series() is much faster.
"""
config()
index = iview.comm.get_index()
for series in index:
print series['title'] + ':'
items = iview.comm.get_series_items(series['id'])
for item in items:
print '\t' + item['title'] + '\t(' + item['url'] + ')'
def index():
""" Downloads the iView index, and prints the corresponding series and IDs.
"""
config()
index = iview.comm.get_index()
for series in index:
print series['id'] + '\t' + series['title']
def series(series_id):
config()
items = iview.comm.get_series_items(series_id)
for item in items:
print item['title'] + '\t(' + item['url'] + ')'
def print_auth():
config()
auth = iview.comm.get_auth()
print 'iView auth data:'
print '\tToken:', auth['token']
print '\tRTMP URL:', auth['rtmp_url']
print '\tPlaypath Prefix:', auth['playpath_prefix']
print '\tUnmetered:', str(auth['free'])
def download(url, output=None):
config()
iview.fetch.fetch_program(url, execvp=True, dest_file=output)
def subtitles(name, output=None):
# In this function, we print >> sys.stderr because we have the possibility
# of outputting subtitles to stdout, so we need to keep stdout clean.
config()
url = ''.join(name.split('.')[:-1])
if output is not None:
srt = output
else:
srt = ''.join(url.split('/')[-1]) + '.srt'
if not srt == '-' and os.path.isfile(srt):
print >> sys.stderr, 'Subtitles have already been downloaded to %s' % srt
return False
print >> sys.stderr, 'Downloading subtitles to %s...' % srt,
try:
subtitles = iview.comm.get_captions(url)
except urllib2.HTTPError, error:
print >> sys.stderr, 'failed'
print >> sys.stderr, 'Got an error when downloading %s' % error.url
return False
except KeyboardInterrupt:
return False
if not srt == '-':
f = open(srt, 'wb')
else:
f = sys.stdout
f.write(subtitles.encode('utf-8'))
f.flush()
if not srt == '-':
os.fsync(f.fileno())
f.close()
print >> sys.stderr, 'done'
def parse_proxy_argument(proxy):
""" Try to parse 'proxy' as host:port pair. Returns an error message
if it cannot be understood. Otherwise, it configures the settings
in iview.config and returns None
"""
# this could be done much nicer with partition()
# but it's not available in older versions of python
parts = proxy.split(':')
if len(parts) == 2:
try:
iview.config.socks_proxy_port = int(parts[1])
except ValueError:
return "Port '%s' is not numeric" % parts[1]
elif len(parts) != 1:
return "Too many ':' in '%s'" % proxy
iview.config.socks_proxy_host = parts[0]
return None
try:
opts, args = getopt.getopt(sys.argv[1:],
'cd:pis:t:x:o:ah',
['cache', 'subtitle=', 'subtitles=', 'download=', 'programme', 'program', 'index', 'series=', 'proxy=', 'output=', 'print-auth', 'help'])
if not len(opts) > 0:
raise getopt.GetoptError('Not enough arguments')
except getopt.GetoptError, err:
usage()
sys.exit(2)
download_arg = None
subtitle_arg = None
output_arg = None
try:
for o, a in opts:
if o in ('-x', '--proxy'):
err = parse_proxy_argument(a)
if err is not None:
print "Invalid proxy specification: %s\n" % err
usage()
sys.exit(4)
elif o in ('-d', '--download'):
download_arg = a
elif o in ('-t', '--subtitle', '--subtitles'):
subtitle_arg = a
elif o in ('-o', '--output'):
output_arg = a
elif o in ('-h', '--help'):
usage()
sys.exit(2)
# iview.comm (and therefore iview.fetch) can only be imported after
# configuring the proxy as we need to override the socket before
# importing urllib2
import iview.fetch
import iview.comm
for o, a in opts:
if o in ('-c', '--cache'):
iview.comm.cache = True
elif o in ('-p', '--programme', '--program'):
programme()
elif o in ('-i', '--index'):
index()
elif o in ('-s', '--series'):
series(a)
elif o in ('-a', '--print-auth'):
print_auth()
except KeyboardInterrupt: # don't traceback on a Ctrl+C
pass
if download_arg is not None:
download(download_arg, output_arg)
elif subtitle_arg is not None:
subtitles(subtitle_arg, output_arg)