-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·70 lines (60 loc) · 2.06 KB
/
main.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
#!/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
import json
from downloader import Transmission
from showsapi import ShowAPI
from config import TRANSMISSION_SETTINGS, SHOWS_JSON_FILE
class ShowDownloader(object):
'''
Main class for downloading new episodes.
'''
def __init__(self):
'''
Initializes the class.
Gets show json.
Initializes Transmission class and ShowAPI class.
'''
self.get_shows_json()
self.transmission = Transmission(**TRANSMISSION_SETTINGS)
self.showsapi = ShowAPI(self.shows, self.download_episode)
def get_shows_json(self):
'''
Gets show dictionary from the shows json file.
'''
shows_json = open(SHOWS_JSON_FILE, 'rb')
self.shows = json.load(shows_json)
shows_json.close()
def download_episode(self, show, season, episode, torrent):
'''
Callback for showsapi on each episode.
Gets the episode and runs the download on transmission.
Then updates the shows dictionary.
:param str show: show name
:param int season: season number
:param int episode: episode number
:param dict torrent: torrent dictionary
'''
print 'Downloading %s' % torrent['release']
self.transmission.add_torrent(torrent['download']['magnet'])
if season == self.shows[show]['season']:
if episode >= self.shows[show]['episode']:
self.shows[show]['episode'] = episode + 1
elif season > self.shows[show]['season']:
self.shows[show]['season'] = season
self.shows[show]['episode'] = episode + 1
def run(self):
'''
Runs the api.
Updates the shows json file when finished.
'''
self.showsapi.run()
shows_json = open(SHOWS_JSON_FILE, 'wb')
json.dump(self.shows, shows_json, indent=4, sort_keys=True)
shows_json.close()
def main():
'''
Main Function
'''
s = ShowDownloader()
s.run()
if __name__ == '__main__':
main()