Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Automatically add renren meiju magnet to deluge
import os
import re
import time
import httplib2
from deluge_client import DelugeRPCClient
base_url = 'http://--CENSORED-BY-GFW--'
headers = {
'User-Agent':'Mozilla/5.0 xxxxx',
'Accept-Language':'zh-CN,zh;q=0.8',
'Accept-Charset':'utf-8;q=0.7,*;q=0.7',
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Connection': 'keep-alive',
'Accept-Encoding':'gzip,deflate',
}
blre = ''
wlre = ''
def load_filter():
global blre
global wlre
wl = []
bl = []
for l in open('subscription.txt').readlines():
l = l.strip()
if l.startswith('!'):
bl.append(l[1:])
else:
wl.append(l)
blre = '(?:' + '|'.join(bl) + ')'
wlre = '(?:' + '|'.join(wl) + ')'
def is_match(title):
rtw = [p for p in re.findall(wlre, title, re.I) if p]
rtb = [p for p in re.findall(blre, title, re.I) if p]
if rtw and not rtb:
return True
else:
return False
def get_local_auth():
xdg_config = os.path.expanduser(os.environ.get("XDG_CONFIG_HOME",
"~/.config"))
config_home = os.path.join(xdg_config, "deluge")
auth_file = os.path.join(config_home, "auth")
username = password = ""
with open(auth_file) as fd:
for line in fd:
if line.startswith("#"):
continue
auth = line.split(":")
if len(auth) >= 2 and auth[0] == "localclient":
username, password = auth[0], auth[1]
break
return username, password
def get_client():
auth = get_local_auth()
client = DelugeRPCClient('127.0.0.1', 58846, auth[0], auth[1])
client.connect()
return client
def check_subscribe(pid):
url = '%s/%s.html' % (base_url, pid)
resp, ct = ht.request(url, headers = headers)
if not ct or 'tid error' in ct:
return '---'
title = re.findall("h2>([^<]+)<", ct)[0]
magnet = re.findall("a href=\"(magnet[^\"]+)\"", ct)[0]
if is_match(title):
return title, magnet
def prt(s):
s='[%s] %s' % (time.strftime('%m-%d %X',time.localtime(time.time())), s)
print(s)
open('hehe_meiju.txt','a').write(s+'\n')
load_filter()
ht = httplib2.Http()
last = int(open('last.txt').read().strip() or '0')
client = get_client()
miss_time = 0
while True:
last += 1
prt("==Start %s==" % last)
ret = check_subscribe(last)
if ret == '---':
miss_time += 1
if miss_time == 3:
break
continue
if ret:
prt("Add new magnet:%s, %s" % (ret[0], ret[1]))
print(client.call('core.add_torrent_magnet', ret[1], {'auto_managed': False, 'stop_at_ratio':True, 'stop_ratio': 0.001}))
open('last.txt','w').write(str(last))
Big.Bang.+HDTV
Broke.Girls.+HDTV
Marvels.Guardians.of.the.Galaxy.+WEB
Marvels.Agents.+HDTV
Minority.Report.+HDTV
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment