-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfloamtv.py
executable file
·716 lines (583 loc) · 23.7 KB
/
floamtv.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
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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
#!/usr/bin/env python
# floamtv.py (Copyright 2008 Aaron Gyes)
# distributed under the GPLv3. See LICENSE
"""
Run without any arguments to use normally. For usage instructions, check out
http://aaron.gy/stuff/floamtv
"""
from __future__ import with_statement
import re, os, csv, yaml, sys, errno, atexit, pytz, shutil, resource, logging
import twisted, base64
from cStringIO import StringIO
from twisted.internet import reactor, task, defer
from pytz.reference import Local as localtz
from twisted.web import xmlrpc, server, client
from twisted.python import usage, log
from twisted.web.client import getPage
from urllib import urlencode
from xmlrpclib import ServerProxy
from datetime import datetime as dt, timedelta
from collections import defaultdict
dbpath = os.path.expanduser('~/.floamtvdb2')
configpath = os.path.expanduser('~/.floamtvconfig2')
pidfile = os.path.expanduser('~/.floamtvpid')
version = "internal"
tasks = {}
tr = re.compile(r"tvrage\.com/.*/([\d]{4,})")
defaults = {
'config': { 'logfile': None,
'max-connections': 3,
'hellanzb-pass': 'changeme',
'nzbclient': 'hella',
'sabnzbd-host': 'localhost',
'sabnzbd-port': 8080,
'sabnzbd-apikey': 'changeme',
'hellanzb-host': 'localhost',
'newzbin-interval': 8,
'tvrage-interval': 500,
'retention': 100,
'port': 19666,
'bind': '',
'sets': list() },
'set': { 'shows': dict(),
'timezone': 'US/Eastern',
'rules': dict() },
'rules': { 'min-megs': '',
'max-megs': '',
'groups': '',
'query': '' }
}
class Collection(yaml.YAMLObject, xmlrpc.XMLRPC):
"""
The Collection object holds all our Shows. It also provides functions to
do things to our collection of Shows.
An individual episode can be chosen through Collection[id], where id is a
humanize()'d version of the TVRage ID.
"""
yaml_tag = '!Collection'
allowNone = False
def __init__(self, sets=None):
self.shows = []
if sets:
self.refresh(sets)
def refresh(self, sets, _firstrun=False):
"""
Populate this Collection with new show information from TVRage. Existing
Shows have new episodes added to them and the rest are updated with new
episodes. sets looks like this:
[
{ 'rules': { ... }, 'shows': ['Show Name 1', 'Show Name 2'] },
{ 'rules': { ... }, 'shows': [ ... ] },
...
]
Rules don't affect this step at all.
"""
logging.info('Getting new data from TVRage.')
def _check_for_brand_new_shows(_):
def _start(x):
if _firstrun:
print "Quitting early to give you a chance to catch up."
if reactor.running: reactor.stop()
elif not tasks['newzbin'].running:
tasks['newzbin'].start(60*config['newzbin-interval'])
shows = set()
tz = {}
for a in sets:
try:
tz.update((s, a['timezone']) for s in a['shows'])
shows.update(a['shows'])
except TypeError:
if reactor.running: reactor.stop()
raise Exception, "You may have a colon in a show name. Shows " \
"with colons need to be enclosed in quotes."
alreadyin = dict((t.title, t) for t in self.shows)
newshows = []
def new_show(info, timezone):
if info:
new_show = Show(info['wecallit'], timezone)
dfrd = new_show.update(info)
self.shows.append(new_show)
return dfrd
maxcon = min(10, int(config['max-connections']))
ds = defer.DeferredSemaphore(tokens=maxcon)
for show in shows.symmetric_difference(alreadyin):
if show not in alreadyin:
newshow = ds.run(tvrage_info, show, None)
newshow.addCallbacks(new_show, tvrageerr, (tz[show],))
newshow.addErrback(getpage_err)
newshows.append(newshow)
elif show not in shows:
logging.info("Pruning %s" % alreadyin[show])
self.shows.remove(alreadyin[show])
ns = defer.DeferredList(newshows)
ns.addCallback(_start)
ns.addCallback(lambda _: self.save())
return ns
if tasks.has_key('newzbin') and tasks['newzbin'].running:
tasks['newzbin'].stop()
pageinfos = []
for show in self.shows:
ashow = tvrage_info(show.title, None)
ashow.addCallback(show.update)
ashow.addErrback(tvrageerr)
ashow.addErrback(getpage_err)
pageinfos.append(ashow)
pageinfos = defer.DeferredList(pageinfos)
pageinfos.addCallback(_check_for_brand_new_shows)
return pageinfos
def status(self, verbose):
"""
Returns a pretty listing of shows we know about. If verbose is True,
all shows we know about are included, else only wanted shows.
"""
out = "\n Episodes\n"\
" ========\n\n"
for ep in sorted(self._episodes(), key=lambda e: e.show):
if ep.wanted or verbose:
out += " (%c) %s\n" % ('+' if ep.wanted else ' ', ep)
out += " (%s)\n\n" % relative_datetime(ep.airs)
out += " ( ) = unwanted, (+) = wanted"
return out
def look_on_newzbin(self, allow_probation=False):
"""
Goes through and does a newzbin search for all wanted episodes. If we can
resolve a newzbin id for a show we want, we enqueue it with hellanzb.
We call episode.was_fake(sure=False) on episodes that are more than one
hour early. This puts them in a state where we will only enqueue them if
they are still on newzbin in two hours.
If allow_probation is True, we enqueue episodes even if they're too early.
"""
def _enqueue_new_stuff(results):
for e in (ep for ep in self._episodes() if ep.wanted and ep.newzbinid):
if e.airs and e.airs > dt.now(pytz.utc):
if not allow_probation:
e.was_fake(sure=False)
else:
e.enqueue(allow_probation)
self.save()
logging.info('Looking for new shows on newzbin.')
dfrds = []
for ruleset in config['sets']:
inset = [e for e in self._episodes() if e.show in ruleset['shows']]
rdict = dict(defaults['rules'].items() + ruleset['rules'].items())
dfrds.append(search_newzbin(inset, rdict))
searches = defer.DeferredList(dfrds)
searches.addCallback(_enqueue_new_stuff)
def unwant(self, floamid):
"""
Given a humanize()'d ID for an episode, set the episode not to enqueue
when it becomes available. 'aired' will unwant all aired episodes.
"""
if floamid == 'aired':
out = ''
for ep in self._episodes():
if ep.wanted and ep.airs and ep.airs < dt.now(pytz.utc):
out += self.unwant(humanize(ep.tvrageid)) + "\n"
return out
try:
if self[floamid].wanted:
self[floamid].wanted = False
self[floamid].newzbinid = None
self.save()
return "Will not download %s when available." % self[floamid]
else:
return "Error: %s is already unwanted" % self[floamid]
except KeyError:
return "Error: %s is not a valid id." % floamid
def rewant(self, floamid):
"""
Given a humanize()'d ID for an episode, set the episode to enqueue when it
becomes available.
"""
try:
if not self[floamid].wanted:
self[floamid].wanted = True
self[floamid].newzbinid = None
self.save()
return "Will download %s when available." % self[floamid]
else:
return "Error: %s is already wanted" % self[floamid]
except KeyError:
return "Error: %s is not a valid id." % floamid
def save(self):
"""
Write the entire Collection to disk (at global dbpath) in YAML format.
"""
with open(dbpath + '~', 'w') as savefile:
yaml.dump(self, savefile, indent=4, default_flow_style=False)
shutil.move(dbpath + '~', dbpath)
def _episodes(self):
for show in self.shows:
for episode in show.episodes:
yield episode
def __getitem__(self, item):
for episode in self._episodes():
if humanize(episode.tvrageid) == item:
return episode
else:
raise KeyError, "No episode with id %s" % item
xmlrpc_status = status
xmlrpc_unwant = unwant
xmlrpc_rewant = rewant
class Show(yaml.YAMLObject):
"""
A Show represents a television show, which has Episodes as children. Takes
only one argument, the show title. Does not automatically populate itself,
you will need to .update() it with information.
"""
yaml_tag = '!Show'
def __init__(self, title, timezone):
self.episodes = []
self.title = title
self.timezone = timezone
def _add_episode(self, info):
"Given a dict with TVRage info, create a new Episode in self.episodes"
if info['airs']:
local = pytz.timezone(self.timezone)
info['airs'] = local.localize(info['airs']).astimezone(pytz.utc)
if info and info.get('tvrageid'):
ep = Episode(**info)
for gotit in (e for e in self.episodes if e.number == ep.number):
attrs = ['title', 'airs', 'tvrageid']
pert = lambda ep: [getattr(ep, attr) for attr in attrs]
if ep and gotit and pert(ep) != pert(gotit):
gotit.title = ep.title
gotit.airs = ep.airs
gotit.tvrageid = ep.tvrageid
logging.info("Updated episode: %s" % gotit)
break
else:
self.episodes.append(ep)
logging.info("New episode: %s" % ep)
def add(self, episode):
"""
Given an episode-number string in the format used by TVRage, ('3x04' being
season 3, episode 4), look up further information on this show and add it
to self.episodes. If we already know about the show we won't do anything.
"""
newepisode = tvrage_info(self.title, episode)
newepisode.addCallbacks(self._add_episode, tvrageerr)
newepisode.addErrback(getpage_err)
return newepisode
def update(self, rageinfo):
"""
Given a dict with TVRage information on this show, try to add both the
latest episode and the upcoming episode to this Show.
"""
rcnt = filter(bool, [rageinfo['latest'], rageinfo['next']])
cbs = [self.add(ep) for ep in rcnt]
self.episodes = [e for e in self.episodes if e.number in rcnt or e.wanted
or len(rcnt) < 2]
return defer.DeferredList([dfrd for dfrd in cbs if dfrd])
def __repr__(self):
return "<Show %s with episodes %s>" \
% (self.title, (' and ').join(map(str, self.episodes)))
def __str__(self):
return self.title
class Episode(yaml.YAMLObject):
"""
Represents a television episode, takes a bunch of arguments:
wecallit: Show's title, however not the real name, but what we searched for.
title: Episode title.
tvrageid: Integer ID number used by TVRage to identify an episode.
airs: datetime.datetime() or None if unknown airtime.
number: Something like '3x05', season 3 episode 5.
"""
yaml_tag = '!Episode'
def __init__(self, wecallit, number, title, tvrageid, airs, *a, **kw):
self.show = wecallit
self.number = number
self.title = title
self.tvrageid = tvrageid
self.airs = airs
self.newzbinid = None
self.wanted = True
def enqueue(self, allow_probation=False):
"""
Enqueue episodes that have a Newzbin ID resolved with Hellanzb. If
allow_probation is True, don't exclude shows that are on probation.
"""
def _handle_sab(result, ep):
if 'error' in result:
logging.error("Unable to enqueue %s" % ep)
logging.error("Reason: %s" % result)
else:
logging.info("Enqueued %s" % ep)
ep.wanted = False
def _handle_sab_fail(err, ep):
logging.error("Unable to enqueue %s" % ep)
if self.newzbinid and self.wanted != 'later' or allow_probation:
if 'sab' in config['nzbclient']:
url = urlencode({'mode': 'addid',
'name': self.newzbinid,
'apikey': config['sabnzbd-apikey']})
url = "http://%s:%s/sabnzbd/api?%s" % (config['sabnzbd-host'],
config['sabnzbd-port'],
url)
attempt = getPage(url, timeout=60)
attempt.addCallback(_handle_sab, self)
attempt.addErrback(_handle_sab_fail, self)
else:
try:
hella = ServerProxy("http://hellanzb:%s@%s:8760"
% (config['hellanzb-pass'], config['hellanzb-host']))
hella.enqueuenewzbin(self.newzbinid)
except:
logging.error("Unable to enqueue %s" % self)
return False
else:
logging.info("Enqueued %s" % self)
self.wanted = False
def educate(self):
"""
After loading data from YAML, the datetimes will be naive.
"""
try:
self.airs = pytz.utc.localize(self.airs)
except (AttributeError, ValueError):
return
def was_fake(self, sure=True):
"""
Call this if we find out a newzbinid for a show is actually pointing at a
fake post. It'll be put to the same state it was at before we found a
newzbinid for it. Call with sure = False if the post is suspect prior to
downloading -- this will put it on 'probation' for a couple hours and only
download it if it isn't deleted from Newzbin in the interim.
"""
if self.wanted != 'later':
if sure:
logging.warning("%s was fake. Requeueing." % self)
self.newzbinid = None
self.wanted = True
elif self.wanted:
self.wanted = 'later'
later = min(timedelta(hours=2), self.airs-dt.now(pytz.utc))
latertime = (later + dt.now(localtz)).strftime("%I:%M %p %Z")
logging.warning("%s is too early. Will try again at %s." % (self,
latertime))
reactor.callLater(later.seconds, self.enqueue, True)
def __repr__(self):
return "<Episode %s - %s - %s>" \
% (self.show, self.number, self.title)
def __str__(self):
return "%s - %s - %s, [%s]" % (self.show, self.number, self.title,
humanize(self.tvrageid))
class Options(usage.Options):
def opt_version(self):
print "floamtv %s" % version
sys.exit()
optFlags = [
['verbose', 'v', 'Show superfluous information when possible.'],
['status', 's', 'Show list of currently wanted episodes, and unwanted'\
' episodes if verbose.'],
['daemonize', 'D', 'Causes floamtv to run as a daemon.'],
['shutdown', 'k', 'Quit the running floamtv.']
]
optParameters = [
['unwant', None, None, 'Set an episode not to download when available'],
['rewant', None, None, 'Set an episode to download when available again'],
]
def am_server():
"Returns True if there isn't another running instance of floamtv."
pid = check_pid()
if pid is None or pid == os.getpid():
return True
def at_exit(showset):
for e in showset._episodes():
if e.wanted == 'later':
e.wanted = True
os.unlink(pidfile)
showset.save()
logging.info('Graceful exit.')
def check_pid():
if os.path.exists(pidfile):
with open(pidfile) as f:
pid = f.read()
try:
os.kill(int(pid), 0)
except os.error, err:
if err.errno == errno.ESRCH:
os.unlink(pidfile)
else:
return int(pid)
def daemonize():
if os.fork() > 0: sys.exit()
os.chdir('/')
os.setsid()
os.umask(0)
if os.fork() > 0: sys.exit()
devnull = os.open(os.devnull, os.O_RDWR)
for i in range(3):
os.close(i)
os.dup2(devnull, i)
os.close(devnull)
def defaultize(D, U):
U.update((k, type(D[k])(v)) for k,v in U.items() if k in D)
U.update((k,v) for k,v in D.items() if k not in U)
U.update((k, defaultize(v, U[k])) for k,v in D.items() if type(v) is dict)
return U
def getpage_err(err):
return err.trap(twisted.internet.error.ConnectionLost)
def humanize(q):
'Converts number to a base33 format, 0-9,a-z except i,l,o (look like digits)'
if q < 0: raise ValueError, 'must supply a positive integer'
letters = '0123456789abcdefghjkmnpqrstuvwxyz'
converted = []
while q != 0:
q, r = divmod(q, 33)
converted.insert(0, letters[r])
return ('').join(converted)
def load():
with open(dbpath, 'r') as savefile:
ss = yaml.load(savefile)
for e in ss._episodes():
e.educate()
return ss
def parse_tvrage(text, wecallit, is_episode):
if text.startswith('No Show Results'):
logging.warning("Show %r does not exist at TVRage." % wecallit)
raise ValueError
rage = defaultdict(lambda: None)
for line in text.splitlines():
part = line.split('@')
rage[part[0]] = part[1].split('^') if '^' in part[1] else part[1]
if is_episode and not rage.has_key('Episode URL'):
raise ValueError
clean = { 'wecallit': wecallit,
'title': rage['Show Name'],
'next': rage['Next Episode'] and rage['Next Episode'][0],
'latest': rage['Latest Episode'] and rage['Latest Episode'][0] }
if is_episode:
clean['tvrageid'] = int(tr.findall(rage['Episode URL'])[-1])
clean['number'] = rage['Episode Info'][0]
clean['title'] = rage['Episode Info'][1]
try:
d = dt.strptime(rage['Episode Info'][2], "%d/%b/%Y")
t = dt.strptime(re.findall("(\d\d.*)", rage['Airtime'])[0], "%I:%M %p")
clean['airs'] = dt.combine(d.date(), t.time())
except:
clean['airs'] = None
return clean
def relative_datetime(date):
if date:
date = date.astimezone(localtz)
diff = date.date() - dt.now(localtz).date()
if diff.days == 0:
return "airs %s today" % date.strftime("%I:%M %p")
elif diff.days == -1:
return "aired %s yesterday" % date.strftime("%I:%M %p")
elif diff.days < -1:
return "aired on %s" % date.strftime("%m/%d/%Y")
elif diff.days == 1:
return "airs %s tomorrow" % date.strftime("%I:%M %p")
elif diff.days < 7:
return "airs %s" % date.strftime("%I:%M %p %A")
else:
return "airs on %s" % date.strftime("%m/%d/%Y")
else: return 'Unknown Airtime'
def search_newzbin(sepis, rdict):
def _process_results(contents, sepis):
rd = csv.reader(StringIO(contents))
try:
results = [(int((tr.findall(r[4]) or [0])[0]), int(r[1])) for r in rd]
tvrageids = set(tvid for tvid, nbid in results)
except IndexError:
tvrageids = []
for ep in (e for e in sepis if e.tvrageid in tvrageids):
for tvid, nbid in results:
if tvid == ep.tvrageid:
ep.newzbinid = nbid
break
query = urlencode({ 'searchaction': 'Search',
'group': (' or ').join(rdict['groups']) if rdict['groups'] else '',
'q': rdict['query'] or '',
'category': 8,
'u_completions': 1,
'u_post_states': 3,
'u_post_larger_than': rdict['min-megs'] or '',
'u_post_smaller_than': rdict['max-megs'] or '',
'q_url': (' or ').join([str(e.tvrageid) for e in sepis]),
'sort': 'ps_edit_date',
'order': 'desc',
'u_post_results_amt': 999,
'u_v3_retention': config['retention'] * 24 * 60 * 60,
'fpn': 'p',
'feed': 'csv' })
basicauth = base64.encodestring("%s:%s" % (config.get('newzbin-user'),
config.get('newzbin-password')))
authheader = "Basic %s" % basicauth.strip()
search = getPage("https://www.newzbin.com/search/?%s" % query, timeout=60,
headers={"Authorization": authheader})
search.addCallback(_process_results, sepis)
search.addErrback(getpage_err)
return search
def set_up_logging(where):
class ignr(logging.Filter):
def filter(self, r): return r.levelname != "INFO"
format = "%(asctime)s %(levelname)s: %(message)s" if where else "%(message)s"
filename = os.path.expanduser(where) if where else None
logging.basicConfig(level=logging.DEBUG, format=format,
datefmt="%b %d %H:%M:%S", filename=filename,
stream=sys.stdout)
twistlog = logging.getLogger('twisted')
twistlog.addFilter(ignr())
try:
observer = log.PythonLoggingObserver()
except AttributeError:
observer = log.DefaultObserver()
observer.start()
def tvrageerr(err):
return err.trap(ValueError)
def tvrage_info(show_name, episode):
episode = episode or ''
u = urlencode({'show': show_name, 'ep': episode})
info = getPage("http://tvrage.com/quickinfo.php?%s" % u, timeout=60)
info.addCallback(parse_tvrage, show_name, episode != '')
return info
def main():
set_up_logging(config['logfile'])
if not am_server() and any(options.values()):
showset = ServerProxy('http://localhost:19666/')
if options['shutdown']:
os.kill(check_pid(), 15)
if options['status'] and check_pid():
logging.info("Daemon is running.")
else:
if check_pid():
return 'floamtv is already running.'
if options['daemonize']:
daemonize()
with open(pidfile, "w") as f:
f.write("%d" % os.getpid())
if os.path.exists(dbpath):
showset = load()
first = False
else:
showset = Collection()
first = True
if options['unwant']:
for show in options['unwant'].split(' '):
logging.info(showset.unwant(show))
elif options['rewant']:
for show in options['rewant'].split(' '):
logging.info(showset.rewant(show))
elif options['status']:
logging.info(showset.status(bool(options['verbose'])))
elif am_server():
atexit.register(at_exit, showset)
tasks['tvrage'] = task.LoopingCall(showset.refresh, config['sets'], first)
tasks['newzbin'] = task.LoopingCall(showset.look_on_newzbin)
tasks['tvrage'].start(60 * config['tvrage-interval'])
reactor.listenTCP(19666, server.Site(showset), interface=config['bind'])
reactor.run()
if __name__ == '__main__':
options = Options()
options.parseOptions()
try:
with open(configpath, 'r') as configf:
config = dict(defaults['config'].items() + yaml.load(configf).items())
### Come up with like apply_defaults function that goes through and
### hooks all the defaults up ahead of time.
except IOError:
sys.exit('You need to set up a config file first. See the docs.')
sys.exit(main())