-
Notifications
You must be signed in to change notification settings - Fork 1
/
pytvshows
executable file
·666 lines (632 loc) · 26.9 KB
/
pytvshows
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
#!/usr/bin/python
# encoding: utf-8
"""
PyTVShows - Downloads torrents from tvrss.net based on
http://tvshows.sourceforge.net/
Depends on http://feedparser.org/
Copyright (C) 2007, Ben Firshman
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
__version__ = '0.2'
# TODO:
# * Support range of episodes (21-22 for example)
# * Check more than one episode in Show.get_details() in case of
# seasonepisode special
# * Check for dead torrent before downloading
# * Split up into libraries
import ConfigParser
import datetime
import feedparser
import getopt
import operator
import os
import re
import socket; socket_errors = []
for e in ['error', 'gaierror']:
if hasattr(socket, e): socket_errors.append(getattr(socket, e))
import sys
import time
import urllib
import urllib2
from operator import itemgetter
warn = sys.stderr
help_message = '''pytvshows %s
Usage: pytvshows [options]
pytvshows downloads torrents for TV shows from RSS feeds provided by
tvRSS.net. It is based on http://tvshows.sourceforge.net/.
Options:
-cFILE, --config FILE
Path to config file. Default: ~/.pytvshows.cfg
-fFEED, --feed=FEED
Override the tvrss.net feed. %%s is replaced with the
exact show name.
-h, --help This help message
-oDIR, --output_dir=DIR
Directory to save torrents. Default: ~/
-ODIR, --output_dir2=DIR
Directory to save torrents if output_dir doesn't exist.
Handy for rtorrent if you move the directory to disable
downloads.
-qQUAL, --quality=QUAL
The TV show quality required. 'normal', 'high' or
'veryhigh', where 'high' is high resolution and 'veryhigh'
is 720p. Default: normal
-v, --verbose Print verbose output
''' % __version__
commands = ['update', 'subscribe', 'unsubscribe', 'run']
config = {
'config_file': os.path.expanduser("~/.pytvshows.cfg"),
'feed': "http://tvrss.net/search/index.php?show_name=%s&show_name_exact" \
"=true&mode=rss",
'output_dir': os.path.expanduser("~/"),
'output_dir2': None,
'quality_matches': {
"720p": 3,
"720p:": 3,
"720P": 3,
"[720p": 3,
"[720P": 3,
"[HR": 2,
"[HD": 1,
"HDTV": 1,
"[DSRIP": 1,
"[TVRIP": 1,
"PDTV": 1,
"[PDTV": 1,
"[DVD": 1,
},
'quality': 1,
'verbose': False,
}
class Episode(object):
"""The parent class for any episode object"""
def __init__(self, show, torrent_url, published_time, quality):
self.show = show
self.torrent_url = torrent_url
self.published_time = published_time
self.quality = quality
def download(self):
if os.path.exists(config['output_dir']):
path = os.path.join(config['output_dir'], self.torrent_file())
elif config['output_dir2'] and os.path.exists(config['output_dir2']):
path = os.path.join(config['output_dir2'], self.torrent_file())
else:
print >> warn, "W: Output directory doesn't exist."
if config["verbose"]:
print "Downloading %s to %s" % (self.torrent_url, path)
try:
urllib.urlretrieve(self.torrent_url, path)
return True
except IOError, e:
print >>warn, "W: Could not download torrent: %s" % e
return False
def clean_name(self, name):
name = name.replace("/", " ")
name = name.replace(":", " ")
name = name.replace(".", " ")
return name
class EpisodeWithSeasonAndEpisode(Episode):
"""
Represents an episode classified by a season number and an episode
number. For example, "Lost"
"""
def __init__(self, show, torrent_url, published_time, season, episode,
quality):
super(EpisodeWithSeasonAndEpisode, self).__init__(show, torrent_url,
published_time, quality)
self.season = season
self.episode = episode
def torrent_file(self):
name = self.clean_name(self.show.human_name)
return "%s %02dx%02d.torrent" % (name, self.season, self.episode)
def __str__(self):
return "%s: Season %s, Episode %s, Quality %s" % \
(self.show, self.season, self.episode, self.quality)
class EpisodeWithDate(Episode):
"""
Represents an episode classified by a date.
For example, "The Daily Show"
"""
def __init__(self, show, torrent_url, published_time, date, quality):
super(EpisodeWithDate, self).__init__(show, torrent_url,
published_time, quality)
self.date = date
def torrent_file(self):
name = self.clean_name(self.show.human_name)
return "%s %s.torrent" % (name, self.date)
def __str__(self):
return "%s: %s, Quality %s" % \
(self.show, self.date, self.quality)
class EpisodeWithTitle(Episode):
"""
Represents an episode with no classification.
For example "Discovery Channel"
"""
def __init__(self, show, torrent_url, published_time, title, quality):
super(EpisodeWithTitle, self).__init__(show, torrent_url,
published_time, quality)
self.title = title
def torrent_file(self):
name = self.clean_name(self.show.human_name)
title = self.clean_name(self.title)
return "%s %s.torrent" % (name, title)
def __str__(self):
return "%s: %s, Quality %s" % \
(self.show, self.title, self.quality)
class Show(object):
"""Represents a show. For example, "Friends"."""
def __init__(self, exact_name, args):
super(Show, self).__init__()
self.exact_name = exact_name
self.human_name = args['human_name']
self.show_type = args['show_type']
self.season = args['season']
self.episode = args['episode']
self.feedurl = args['feedurl']
#YYYY-MM-DD HH:MM:SS
if args['date']:
self.date = datetime.datetime(*(time.strptime(
args['date'], "%Y-%m-%d")[0:6])).date()
else:
self.date = None
if args['time']:
self.time = datetime.datetime(*(time.strptime(
args['time'], "%Y-%m-%d %H:%M:%S")[0:6]))
else:
self.time = None
self.ignoremissingdetails = bool(args['ignoremissingdetails'])
self.rss = None
self._get_rss_feed()
self.episodes = None
if not self.show_type or not self.human_name \
or (self.show_type == "date" and not self.date) \
or (self.show_type == "time" and not self.time) \
or (self.show_type == "seasonepisode" \
and (not self.season or not self.episode)):
self.get_details()
else:
# this needs to be done half way through get_details
self._parse_rss_feed()
if self.season:
self.season = int(self.season)
if self.season:
self.episode = int(self.episode)
def get_details(self):
"""Tries to get the details for the show from the RSS feed. This
should only be run once if the configs are all working OK."""
if config["verbose"]:
print "Getting details for %s..." % self
if not self.rss:
return False
try:
episode = self.rss['entries'][0]
except IndexError:
print >> warn, "There are no episodes in the RSS feed for %s." % \
self
return False
# Determine human title
r = re.compile('Show Name\s*: (.*?);')
name_match = r.search(episode.description)
if not name_match:
print >> warn, "Could not determine show name for %s." % self
return False
self.human_name = name_match.group(1)
# Determine show type
r = re.compile('Show\s*Title\s*:\s*(.*?);')
title_match = r.search(episode.description)
r = re.compile('Season\s*:\s*([0-9]*?);')
season_match = r.search(episode.description)
r = re.compile('Episode\s*:\s*([0-9]*?)$')
episode_match = r.search(episode.description)
r = re.compile('Episode\s*Date:\s*([0-9\-]+)$')
date_match = r.search(episode.description)
if season_match and episode_match:
self.show_type = 'seasonepisode'
elif date_match:
self.show_type = 'date'
elif titlematch and titlematch.group(1) != 'n/a':
self.show_type = 'time'
else:
print >> warn, "Could not determine show type for %s." % self
return False
# Determine highest key
self._parse_rss_feed()
if not self.episodes:
return False
max_key = max(self.episodes.keys())
if not max_key:
print >> warn, "Could not determine last episode for %s." % self
return False
if self.show_type == 'seasonepisode' \
and (not self.season or not self.episode):
(self.season, self.episode) = max_key
# So we can keep track of specials
# TODO: need a better way to do this, this is a quick hack.
# If there is a special after the latest normal episode, it will
# download it and we don't want that
self.time = self.episodes[max_key][0].published_time
elif self.show_type == 'date' and not self.date:
self.date = max_key
elif self.show_type == 'time' and not self.time:
self.time = max_key
def get_new_episodes(self):
"""Gets new episodes for the show and updates the key based on what
show type it is."""
if self.show_type == 'seasonepisode':
(self.season, self.episode) = self._get_new_episodes_with_key(
(self.season, self.episode))
elif self.show_type == 'date':
self.date = self._get_new_episodes_with_key(self.date)
elif self.show_type == 'time':
self.time = self._get_new_episode_with_key(self.time)
def _get_rss_feed(self):
"""Gets the feedparser object."""
if self.feedurl:
url = self.feedurl
else:
url = config['feed'] % self.exact_name
if config['verbose']:
print "Downloading and processing %s..." % url
r = feedparser.parse(url)
http_status = r.get('status', 200)
http_headers = r.get('headers', {
'content-type': 'application/rss+xml',
'content-length':'1'})
exc_type = r.get("bozo_exception", Exception()).__class__
if http_status != 304 and not r.entries and not r.get('version', ''):
if http_status not in [200, 302]:
print >>warn, "W: error", http_status, url
elif 'html' in http_headers.get('content-type', 'rss'):
print >>warn, "W: looks like HTML", url
elif http_headers.get('content-length', '1') == '0':
print >>warn, "W: empty page", url
elif hasattr(socket, 'timeout') and exc_type == socket.timeout:
print >>warn, "W: timed out on", url
elif exc_type == IOError:
print >>warn, "W:", r.bozo_exception, url
elif hasattr(feedparser, 'zlib') \
and exc_type == feedparser.zlib.error:
print >>warn, "W: broken compression", f.url
elif exc_type in socket_errors:
exc_reason = r.bozo_exception.args[1]
print >>warn, "W:", exc_reason, f.url
elif exc_type == urllib2.URLError:
if r.bozo_exception.reason.__class__ in socket_errors:
exc_reason = r.bozo_exception.reason.args[1]
else:
exc_reason = r.bozo_exception.reason
print >>warn, "W:", exc_reason, url
elif exc_type == KeyboardInterrupt:
raise r.bozo_exception
else:
print >>warn, \
"E:", r.get("bozo_exception", "can't process"), f.url
return False
self.rss = r
return r
def _parse_rss_feed(self):
if not self.rss:
return False
episodes = {}
# we've defined a feedurl in the config file...
if self.feedurl:
for episode in self.rss['entries']:
if self.show_type == 'seasonepisode':
#r = re.compile('S([0-9]+)E([0-9]+)')
if config["verbose"]:
print "Title: %s" % (episode.title)
r = re.compile(r'\bS?([0-9]{1,2})\s*(E|x)?\s*([0-9]{1,2})\b')
match = r.search( episode.title )
season_num = int(match.group(1))
episode_num = int(match.group(3))
if config["verbose"]:
print "Found Show: Season %i, Episode %i" % (season_num, episode_num)
quality = 0
for key, value in sorted(config["quality_matches"].items(), key=itemgetter(1), reverse=True):
if key in episode.title:
if config["verbose"]:
print "quality = %i" % value
quality = value
break
if season_num != 0 and episode_num != 0:
obj = EpisodeWithSeasonAndEpisode(
self,
episode.link,
datetime.datetime(* episode.updated_parsed[:6]),
season_num,
episode_num,
quality)
try:
episodes[season_num, episode_num].append(obj)
except KeyError:
episodes[season_num, episode_num] = [obj]
elif config['verbose']:
print 'Season or episode number is 0 in %s' \
% episode.description
else:
for episode in self.rss['entries']:
if self.show_type == 'seasonepisode':
r = re.compile('Season\s*: ([0-9]*?);')
season_match = r.search(episode.description)
r = re.compile('Episode\s*:\ ([0-9]*?)$')
episode_match = r.search(episode.description)
if not season_match or not episode_match:
# This might be a special with a title
r = re.compile('Show\s*Title\s*:\s*(.*?);')
title_match = r.search(episode.description)
if title_match and title_match.group(1) != 'n/a' \
and title_match.group(1) != '':
title = title_match.group(1)
if config["verbose"]:
print "Found episode with title %s and no " \
"season or episode in seasonepisode show." % title
quality = 0
for key, value in sorted(config["quality_matches"].items(), key=itemgetter(1), reverse=True):
if key in episode.title:
quality = value
break
date = datetime.datetime(* episode.updated_parsed[:6])
obj = EpisodeWithTitle(
self,
episode.link,
date,
title,
quality)
last_key = 0
for key in episodes.keys():
if key[0] == 0 and key[1] > last_key:
last_key = key[1]
episodes[0, last_key] = [obj]
elif not self.ignoremissingdetails:
print >> warn, 'W: Could not match season and/or ' \
'episode in %s' % episode.description
else:
quality = 0
for key, value in sorted(config["quality_matches"].items(), key=itemgetter(1), reverse=True):
if key in episode.title:
quality = value
break
season_num = int(season_match.group(1))
episode_num = int(episode_match.group(1))
if season_num != 0 and episode_num != 0:
obj = EpisodeWithSeasonAndEpisode(
self,
episode.link,
datetime.datetime(* episode.updated_parsed[:6]),
season_num,
episode_num,
quality)
try:
episodes[season_num, episode_num].append(obj)
except KeyError:
episodes[season_num, episode_num] = [obj]
elif config['verbose']:
print 'Season or episode number is 0 in %s' \
% episode.description
elif self.show_type == 'date':
r = re.compile('Episode\s*Date:\s*([0-9\-]+)$')
date_match = r.search(episode.description)
if not date_match:
if not self.ignoremissingdetails:
print >>warn, 'W: Could not match date in %s' % \
episode.description
else:
quality = 0
for key, value in sorted(config["quality_matches"].items(), key=itemgetter(1), reverse=True):
if key in episode.title:
quality = value
break
date = datetime.datetime(*(time.strptime(
date_match.group(1), "%Y-%m-%d")[0:6])).date()
obj = EpisodeWithDate(
self,
episode.link,
datetime.datetime(* episode.updated_parsed[:6]),
date,
quality)
try:
episodes[date].append(obj)
except KeyError:
episodes[date] = [obj]
elif self.show_type == 'time':
r = re.compile('Show\s*Title\s*:\s*(.*?);')
title_match = r.search(episode.description)
if not title_match:
if not self.ignoremissingdetails:
print >>warn, 'W: Could not match title in %s' % \
episode.description
title = ""
else:
title = title_match.group(1)
quality = 0
for key, value in sorted(config["quality_matches"].items(), key=itemgetter(1), reverse=True):
if key in episode.title:
quality = value
break
date = datetime.datetime(* episode.updated_parsed[:6])
obj = EpisodeWithTitle(
self,
episode.link,
date,
title,
quality)
try:
episodes[date].append(obj)
except KeyError:
episodes[date] = [obj]
self.episodes = episodes
return episodes
def _get_new_episodes_with_key(self, min_key):
downloaded_episode_keys = []
if not self.episodes:
return min_key
episodes = self.episodes # so we can fuck with it
# What's the best quality available for the last 7 episodes?
best_quality = 0
i = 0
done = False
for ep_set in episodes.values():
for ep in ep_set:
if ep.quality > best_quality:
best_quality = ep.quality
i += 1
wanted_quality = min(config["quality"], best_quality)
# Only get unseen episodes
# Check seasonepisode specials
last_time = None
if self.show_type == 'seasonepisode' and (0, 0) in episodes.keys():
last_time = None
for key in episodes.keys():
if key[0] == 0:
if last_time is None \
or episodes[key][0].published_time > last_time:
last_time = episodes[key][0].published_time
if self.time \
and episodes[key][0].published_time <= self.time:
del episodes[key]
if last_time:
self.time = last_time
# Check normal episodes
for key in episodes.keys():
if (self.show_type != 'seasonepisode' or key[0] != 0) \
and key <= min_key:
del episodes[key]
# First try : download the episodes for which we have the wanted
# quality
for key, ep_set in episodes.items():
for ep in ep_set:
if ep.quality == wanted_quality:
if config["verbose"]:
print "Downloading %s..." % ep
if ep.download():
downloaded_episode_keys.append(key)
break
# Second try : download the episodes for which the quality delay has
# expired, with the best guess for quality
for key, ep_set in episodes.items():
if key not in downloaded_episode_keys:
ep_set.sort(key=operator.attrgetter("published_time"))
min_published_time = ep_set[0].published_time
d = (datetime.datetime.now() - min_published_time)
if (d.days*86400 + d.seconds) > 6*3600*wanted_quality:
# Try to match wanted quality
ep_set.sort(key=operator.attrgetter("quality"))
episode = None
for ep in ep_set:
if ep.quality > wanted_quality and (not episode
or ep.quality > episode.quality):
episode = ep
if not episode:
episode = ep_set[0]
if config["verbose"]:
print "Downloading %s..." % episode
if episode.download():
downloaded_episode_keys.append(key)
if len(downloaded_episode_keys) > 0:
downloaded_episode_keys.sort()
if self.show_type == 'seasonepisode' \
and downloaded_episode_keys[-1:][0][0] == 0:
return min_key
return downloaded_episode_keys[-1:][0]
return min_key
def __str__(self):
if self.human_name:
return self.human_name
else:
return self.exact_name
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:],
"c:f:ho:O:q:v",
["config=", "feed=", "help", "output_dir=", "output_dir2=",
"quality=", "verbose"])
except getopt.error, msg:
raise Usage(msg)
# option processing
for option, value in opts:
if option in ("-c", "--config"):
config["config_file"] = os.path.expanduser(value)
if option in ("-f", "--feed"):
try:
value % ''
except TypeError:
raise Usage("Specified feed does not include %s")
config['feed'] = value
if option in ("-h", "--help"):
print help_message
return
if option in ("-o", "--output_dir"):
config['output_dir'] = os.path.expanduser(value)
if option in ("-O", "--output_dir2"):
config['output_dir2'] = os.path.expanduser(value)
if option in ("-q", "--quality"):
if value == "normal":
config['quality'] = 1
elif value == "high":
config['quality'] = 2
elif value == "veryhigh":
config['quality'] = 3
else:
raise Usage("Invalid quality")
if option in ("-v", "--verbose"):
config['verbose'] = True
#if args:
# if command not in commands:
# raise Usage("Invalid command.")
# command = args[0]
#else:
# raise Usage("No command specified.")
except Usage, err:
print >> warn, sys.argv[0].split("/")[-1] + ": " + str(err.msg)
print >> warn, "\t for help use --help"
return 2
if config["verbose"]:
print "pytvshows %s" % __version__
print "Loading configuration file..."
f = ConfigParser.ConfigParser()
f.read(config['config_file'])
for exact_name in f.sections():
l = f.options(exact_name)
args = {
'human_name': None,
'show_type': None,
'season': None,
'episode': None,
'date': None,
'time': None,
'ignoremissingdetails': False,
'feedurl': None,
}
for key in args.keys():
if f.has_option(exact_name, key):
args[key] = f.get(exact_name, key)
if config["verbose"]:
print "Getting episodes for %s..." % exact_name
show = Show(exact_name, args)
show.get_new_episodes()
for (key, value) in args.items():
if getattr(show, key) != value:
f.set(exact_name, key, getattr(show, key))
if config["verbose"]:
print "Saving configuration file..."
fp = open(config['config_file'], "w+")
f.write(fp)
fp.close()
if __name__ == "__main__":
sys.exit(main())