/
download.py
111 lines (96 loc) · 3.2 KB
/
download.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
"""
Fetch new YIFY releases using API.py
Install Transmission RPC:
pip install transmissionrpc
NOTES:
http://stackoverflow.com/questions/862173/how-to-download-a-file-using-python-in-a-smarter-way
http://pythonhosted.org/transmissionrpc/
http://docs.python.org/2/library/sqlite3.html
http://stackoverflow.com/questions/8250814/sqlite-autoincrement-how-to-insert-values
"""
import yify, urllib2, transmissionrpc, logging, urllib, httplib, sqlite3, json, time
# logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
# downloaddir
WATCH_DIR="/Users/apple/Desktop/"
# transmission
HOST=""
PORT=9091
USER=""
PASSWORD=""
# pushover
APP_TOKEN=""
USER_KEY=""
MSG="YIFY release downloaded"
# YIFY
QUALITY="720p"
RATING=0
def save(URL):
global WATCH_DIR
rfile = urllib2.urlopen(URL)
try:
rfilename = rfile.info()['Content-Disposition'] #attachment; filename="The_Butler_2013_1080p_BluRay_x264_YIFY_mp4.torrent"
rfilename = rfilename.split("filename=")[1][1:-1]
except KeyError:
rfilename = URL.split('/')[-1]
f = open(WATCH_DIR+rfilename, "wb")
f.write(rfile.read())
f.close()
def send(tc, hash):
tc.add_torrent(hash)
def notify(URL=False, APP_TOKEN=APP_TOKEN, USER_KEY=USER_KEY, MSG=MSG):
conn = httplib.HTTPSConnection("api.pushover.net:443")
conn.request("POST", "/1/messages.json",
urllib.urlencode({
"token": APP_TOKEN,
"user": USER_KEY,
"message": MSG,
"url": URL,
"timestamp": int(time.time()),
}), { "Content-type": "application/x-www-form-urlencoded" })
conn.getresponse()
# Create transmissionClient obj (send) or update WATCH_DIR (save)
tc = transmissionrpc.Client(address=HOST, port=PORT, user=USER, password=PASSWORD)
# get list of new releases
movies = yify.listMovies(quality=QUALITY)
# Count of total results that match the listMovies() query we sent
movieCount = movies['MovieCount']
conn = sqlite3.connect('yify.db')
c = conn.cursor()
newlist = False
oldlist = []
for movie in movies['MovieList']:
c.execute("select count(MovieID) from movies where MovieID = :id", {'id':movie['MovieID']})
values = (movie['DateUploaded'], movie['DateUploadedEpoch'], movie['MovieID'],
movie['State'], movie['MovieUrl'], movie['MovieTitle'], movie['MovieYear'],
movie['Quality'], movie['ImdbCode'], movie['TorrentUrl'], movie['TorrentHash'],
movie['TorrentMagnetUrl'])
if 0 == c.fetchone()[0]:
newlist = []
newlist.append(movie['MovieTitle'].encode('ascii','ignore'))
# record release
c.execute('''
INSERT INTO movies(
DateUploaded, DateUploadedEpoch, MovieID, State,
MovieUrl, MovieTitle, MovieYear, Quality,
ImdbCode, TorrentUrl, TorrentHash, TorrentMagnetUrl
)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?)''', values)
# send/save release
#send(tc, movie['TorrentMagnetUrl'])
else:
oldlist.append(movie['MovieTitle'].encode('ascii','ignore'))
logging.info("New releases: %i" % (len(newlist) if newlist else 0))
logging.debug("New releases:\n%s" % newlist)
logging.info("Old releases: %i" % len(oldlist))
logging.debug("Old releases:\n%s" % oldlist)
if newlist:
MSG = "Fetched:"
for x in newlist:
MSG += "\n%s" % x
else:
MSG = "Nothing new released toady"
logging.info("Pushover MSG parameter: %s" % MSG)
notify(MSG=MSG)
conn.commit()
conn.close()