-
Notifications
You must be signed in to change notification settings - Fork 0
/
shores.py
executable file
·136 lines (107 loc) · 3.64 KB
/
shores.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
#!/usr/bin/env python
import ConfigParser
import cPickle
import os
import sys
import urllib
import urlparse
from BeautifulSoup import BeautifulSoup as bsoup
from feedparser import parse as rssparse
from watchedli import WatchedLi
BASE_URL = 'http://showrss.karmorra.info/?cs=feeds'
FEED_URL = 'http://showrss.karmorra.info/feeds/%s.rss'
CFG_DIR = os.path.expandvars('$HOME/.shores')
CACHE_FILE = os.path.join(CFG_DIR, 'cache')
CFG_FILE = os.path.join(CFG_DIR, 'config')
def dlfile(link, targetDir):
url = urlparse.urlparse(link)
path = os.path.join(targetDir, os.path.basename(url[2]))
if os.path.exists(path):
return False
u = urllib.urlopen(link)
torrent = u.read()
f = file(path, 'wb')
f.write(torrent)
f.close()
return True
def list_shows():
result = []
try:
inputFP = urllib.urlopen(BASE_URL)
except Exception, e:
print e
return result
for option in bsoup(inputFP)('option'):
optContents = ''.join(option.contents)
optValue = option['value']
if optValue.isnumeric():
result.append({optContents: optValue})
inputFP.close()
return result
def is_in_cache(cache, key, element):
if cache.has_key(key):
return element in cache[key]
return False
def store_in_cache(cache, key, element):
if not cache.has_key(key):
cache[key] = []
cache[key].append(element)
def load_cache():
if not os.path.exists(CACHE_FILE):
return {}
inputFP = file(CACHE_FILE)
result = cPickle.load(inputFP)
inputFP.close()
return result
def store_cache(to_cache):
outputFP = file(CACHE_FILE, 'w')
cPickle.dump(to_cache, outputFP)
outputFP.close()
def handle_feed(feed_url, cache, feed_id, targetDir):
inputFP = urllib.urlopen(feed_url)
for item in rssparse(inputFP)['entries']:
for link in item['links']:
linked = link['href']
if not is_in_cache(cache, feed_id, linked):
try:
if dlfile(linked, targetDir):
store_in_cache(cache, feed_id, linked)
print 'Fetched torrent file "%s".' % linked
except Exception, e:
print e
inputFP.close()
def handle_show(showName, listOfShows, targetDir):
for show in listOfShows:
curShowName = show.keys()[0]
if curShowName.lower() == showName.lower():
print 'Found matching show "%s".' % curShowName
handle_feed(FEED_URL % show[curShowName], cache, curShowName,
targetDir)
store_cache(cache)
return
print 'Show "%s" does not exist.' % showName
if __name__ == '__main__':
if not os.path.exists(CFG_DIR):
os.mkdir(CFG_DIR)
dummy_config = file(CFG_FILE, 'w')
dummy_config.write('[shores]\ntargetDir = %s' % os.path.expandvars('$HOME'))
dummy_config.close()
config = ConfigParser.ConfigParser()
config.read(CFG_FILE)
targetDir = config.get('shores', 'targetDir')
cache = load_cache()
listOfShows = list_shows()
if len(sys.argv) == 1:
try:
watchedLiUser = config.get('shores', 'watchedLiUser')
watchedLiPass = config.get('shores', 'watchedLiPass')
watched = WatchedLi(watchedLiUser, watchedLiPass)
for show in watched.shows():
handle_show(show, listOfShows, targetDir)
except:
print 'Available shows:'
for show in listOfShows:
print '\t%s' % show.keys()[0]
else:
showName = ' '.join(sys.argv[1:])
handle_show(showName, listOfShows, targetDir)