Last active
December 21, 2015 15:29
-
-
Save tanelpuhu/6326634 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# coding=utf-8 | |
import feedparser | |
import requests | |
import urlparse | |
import hashlib | |
import urllib | |
import time | |
import sys | |
import os | |
import re | |
# Feed URL | |
URL = "https://kickass.to/movies/?rss=1&field=seeders&sorder=desc" | |
# Feed cache time in minutes | |
CACHE_TIME = 30 | |
# Command that executed with magnet link | |
ADD_CMD = 'transmission-remote --add "%s"' | |
def to_human(size): | |
try: | |
size = float(size) | |
except ValueError: | |
return size | |
sufs = ['T', 'G', 'M', 'K', 'b'] | |
while size > 1024: | |
size /= 1024 | |
sufs.pop() | |
if size < 10: | |
return '%.1f%s' % (size, sufs.pop()) | |
return '%.0f%s' % (size, sufs.pop()) | |
def get_data(): | |
tmp = '/tmp/movs-' + hashlib.md5(URL).hexdigest() | |
if os.path.exists(tmp): | |
stat = os.stat(tmp) | |
from_mod = time.time() - stat.st_mtime | |
if from_mod < CACHE_TIME * 60: | |
return open(tmp, 'r').read() | |
result = requests.get(URL).content | |
open(tmp, 'w').write(result) | |
return result | |
def get_movs(limit=None): | |
p = feedparser.parse(get_data()) | |
entries = p.get('entries', []) | |
hids = [] | |
for entry in entries[:limit]: | |
imdbids = [] | |
if 'summary' in entry and 'imdb.com/title/tt' in entry.summary: | |
imdbids = re.findall('.*imdb.com/title/tt(\d{7}).*', entry.summary) | |
fullhid = hashlib.md5(entry.id).hexdigest() | |
for i in xrange(5, 27): | |
hid = fullhid[:i] | |
if hid not in hids: | |
hids.append(hid) | |
break | |
yield ( | |
hid, | |
fullhid, | |
entry.torrent_seeds, | |
entry.torrent_peers, | |
entry.title.encode('utf-8'), | |
entry.torrent_magneturi, | |
to_human(entry.torrent_contentlength), | |
imdbids, | |
) | |
def plain_magnet(mag): | |
if not mag.startswith('magnet:?'): | |
return mag | |
qs = urlparse.parse_qs(mag[8:]) | |
return 'magnet:?xt=' + qs.get('xt', [''])[0] | |
def add_magnet(magnet): | |
pin_pout = os.popen4(ADD_CMD % plain_magnet(magnet)) | |
print pin_pout[1].read() | |
def main(): | |
frmt = '%6s %-5s %-6s %-6s %s' | |
print frmt % ( | |
'ID', 'Size', 'Seeds', 'Peers', 'Title' | |
) | |
print '-'*80 | |
for hid, fullhid, seeds, peers, title, magnet, size, imdbid in get_movs(): | |
print frmt % ( | |
hid, size, seeds, peers, title | |
) | |
if sys.argv[1:]: | |
movs = get_movs() | |
for arg in sys.argv[1:]: | |
for hid, fullhid, seeds, peers, title, magnet, size, imdbid in movs: | |
if fullhid.startswith(arg): | |
print 'Adding %s (%s)...' % (title, size) | |
add_magnet(magnet) | |
break | |
sys.exit() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment