-
Notifications
You must be signed in to change notification settings - Fork 4
/
t3.py
122 lines (107 loc) · 4.07 KB
/
t3.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
#!/usr/bin/env python
from lxml import etree
import re
import json
try:
import urllib.request
except:
import urllib2
import sys
import os
import argparse
import sendemail
import sendxmpp
def size_numeric(size_string):
'''
Return the size of torrent in MBs as a float.
'''
if "GB" in size_string.upper():
numeralstring = size_string.upper()[:size_string.upper().index("GB")]
integer = float(numeralstring.strip())
in_mb = integer * 1024.00
elif "MB" in size_string.upper():
in_mb = float(size_string.upper()[:size_string.upper().index("MB")])
return in_mb
parser = argparse.ArgumentParser(description='Download TV show torrents')
parser.add_argument(
"--email", help="Specify email address to send notification")
parser.add_argument(
"--xmpp", help="Specify jabber address to send notification")
args = parser.parse_args()
# Reading the nextone file that specifies the next episode to be downloaded
nextonefile = open('nextone')
nextonecontent = nextonefile.read()
nextonefile.close()
try:
tv_shows = json.loads(nextonecontent)
except:
sys.exit(0)
# Setting request header to mimic Mozilla FIrefox
headers = {'User-Agent': 'Mozilla/5.0'}
for show in tv_shows:
if not tv_shows[show]['nextseason'] or not tv_shows[show]['nextepisode']:
continue
# Generate the string SxxExx from input file
nextitem = "S" + tv_shows[show]['nextseason'] + \
"E" + tv_shows[show]['nextepisode']
url = tv_shows[show]['url']
# Get the source HTML from corresponding web page
try:
req = urllib.request.Request(url, None, headers)
page = urllib.request.urlopen(req)
except:
req = urllib2.Request(url, None, headers)
page = urllib2.urlopen(req)
sourcecontent = page.read()
out = {}
tree = etree.HTML(sourcecontent)
# Find all the rows with class 'forum_header_border'
# (From analysing the page source, the links use this format'
r = tree.xpath('//tr[contains(@class, "forum_header_border")]')
for row in r:
name = row.xpath('td/a/@title')[0]
episode_regex = re.compile(r"S[0-9]*")
regexes = [
"S(?P<season>[0-9]*)E(?P<episode>[0-9]*)", # SxxExx format
"(?P<season>[0-9]*)x(?P<episode>[0-9]*)" # NNxNN format
]
for regex in regexes:
match = re.search(regex, name)
if match:
break
season = match.group('season').zfill(2)
episode = match.group('episode').zfill(2)
seasonepisodestring = "S" + season + "E" + episode
magnet_link = row.xpath('td/a[contains(@class, "magnet")]/@href')
# Get the size column of the row
size = size_numeric(row.xpath('td/text()')[-2])
# Populate the nested json object with seasonepisodestring
# and size as keys
if seasonepisodestring not in out:
out[seasonepisodestring] = {}
out[seasonepisodestring][size] = magnet_link
if nextitem in out:
min_size = min(out[nextitem]) # Find the link with smallest size
os.system("deluge-console add '" + out[nextitem][min_size][0] + "'")
if args.email:
sendemail.sendmail(
args.email, 'Torrent Added', name)
if args.xmpp:
sendxmpp.sendmsg(args.xmpp, 'Torrent Added : ' + name)
if tv_shows[show]['nextepisode'] == '24':
# If the number of episodes in a season has exceeded,
# increment the season count and initialize episode to 01.
# 24 is the normal number of episodes in TV shows
nextseason = "%02d" % (int(tv_shows[show]['nextseason']) + 1)
nextepisode = "01"
else:
# Increment the episode count
nextseason = tv_shows[show]['nextseason']
nextepisode = "%02d" % (int(tv_shows[show]['nextepisode']) + 1)
tv_shows[show]['nextseason'] = nextseason
tv_shows[show]['nextepisode'] = nextepisode
else:
continue
nextonefile = open('nextone', 'w')
nextonefile.write(json.dumps(tv_shows, indent=4))
nextonefile.close()