htt://example.com/scrape.php?info_hash=aaaaaaaaaaaaaaaaaaaa
# -*- coding: utf-8 -*- import urllib2 from urllib import urlencode from binascii import b2a_hex as bta, a2b_hex as atb from os import remove from shutil import move from lxml.html import document_fromstring as doc from bencode import bdecode, bencode from httplib2 Http http = Http() username = 'username' password = 'password' ut_port = '12345' # web- uTorrent'. ut_username = 'utusername' ut_password = 'utpassword' site = 'http://example.com/' scrape_body = site + 'scrape.php?info_hash=' # URL scrape-. login_url = site + 'takelogin.php' torrent_body = site + 'download.php?id={0}&name={0}.torrent' announce = site + 'announce.php?' # URL . webui_url = 'http://127.0.0.1:{0}/gui/'.format(ut_port) webui_token = webui_url + 'token.html' # .torrent . settings.dat, dir_torrent_files. torrent_path = 'c:/utorrent/torrent/' # . autoload_path = 'c:/utorrent/autoload/' # uTorrent'a ( resume.dat) sys_torrent_path = 'c:/users/myname/appdata/utorrent/' def authentication(username, password): data = {'username': username, 'password': password} headers = {'Content-type': 'application/x-www-form-urlencoded', 'User-agent':'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.6'} resp, login = http.request(login_url, 'POST', headers=headers, body=urlencode(data)) # , cookiekeys = ['uid', 'pass', 'PHPSESSID', 'pass_hash', 'session_id'] split_resp = resp['set-cookie'].split(' ') lst = [] # . for split_res in split_resp: if split_res.split('=')[0] in cookiekeys: lst.append(split_res) cookie = ' '.join(lst) return {'Cookie': cookie} def torrentDict(torr_path): #torr_path - resume.dat . Dict = {} with open(u'{0}resume.dat'.format(torr_path), 'rb') as resume: t = bdecode(resume.read()) for name in t: if name != '.fileguard' and name != 'rec': for tracker in t[name]['trackers']: if isinstance(tracker, str) and tracker.startswith(announce): Dict[name.split('\\')[-1]] = bta(t[name]['info']) return Dict
uthead = {'User-Agent':'uTorrent/2210(21304)'} # uTorrent'. main_dict = torrentDict(sys_torrent_path) for key in main_dict: lst = [] for i in range(0, len(main_dict[key]), 2): lst.append('%{0}'.format(main_dict[key][i:i+2].upper())) scrp_str = ''.join(lst) # , . resp, scrp = http.request('{0}{1}'.format(scrape_body, scrp_str), 'GET', headers=uthead)
d5:filesd20: aaaaaaaaaaaaaaaaaaaa d8:completei 5 e10:downloadedi 50 e10:incompletei 10 eeee
20 characters “a” is a hash of distribution, 5 are siders, 10 are leechers and 50 have finished downloading.d5:filesdee
# "for key in Dict:". with open('{0}{1}'.format(torrent_path, key), 'rb') as torrent_file: torrent = bdecode(torrent_file.read()) t_id = torrent['comment'][36:] # . brhead = authentication(username, password) resp, torrent = http.request(torrent_body.format(t_id), headers=brhead) with open('{0}.torrent'.format(t_id),'wb') as torrent_file: torrent_file.write(torrent) # .torrent . remove('{0}{1}'.format(torrent_path, key)) move('{0}.torrent'.format(t_id), '{0}{1}.torrent'.format(autoload_path, t_id)) # . . authkey, token = uTWebUI(ut_username, ut_password) webuiActions(main_dict[key], 'remove', authkey, token)
def uTWebUI(ut_name, ut_passw): # cookie token. passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm() passmgr.add_password(None, webui_token, ut_name, ut_passw) authhandler = urllib2.HTTPBasicAuthHandler(passmgr) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) req = urllib2.Request(webui_token) tkp = urllib2.urlopen(req) page = tkp.read() token = doc(page).xpath('//text()')[0] passw = req.unredirected_hdrs['Authorization'] return passw, token def webuiActions(torrent_hash, action, password, token): head = {'Authorization': password} if action == 'remove': # . action_req = '?token={0}&action=remove&hash={1}'.format(token, torrent_hash) r, act = http.request(webui_url+action_req, headers=head)
Source: https://habr.com/ru/post/135874/
All Articles