diff --git a/u2helper/.gitignore b/u2helper/.gitignore new file mode 100644 index 0000000..505cfd0 --- /dev/null +++ b/u2helper/.gitignore @@ -0,0 +1,4 @@ +config.json +seeding.json +*.pyc +.idea diff --git a/u2helper/README.md b/u2helper/README.md new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/u2helper/README.md @@ -0,0 +1 @@ + diff --git a/u2helper/config.json.example b/u2helper/config.json.example new file mode 100644 index 0000000..f2a06af --- /dev/null +++ b/u2helper/config.json.example @@ -0,0 +1,10 @@ +{ + "download_dir": "/mnt/usb/download", + "target_dir_parent": "/mnt/usb", + "transmission_url": "https://your.doamin.name/transmission/rpc", + "transmission_user": "YOUR USERNAME", + "transmission_password": "YOUR PASSWORD", + "uid": "123456", + "nexusphp_u2": "nexusphp_u2 FROM U2 COOKIES", + "__cfduid": "__cfduid FROM U2 COOKIES" +} \ No newline at end of file diff --git a/u2helper/transmission.py b/u2helper/transmission.py new file mode 100644 index 0000000..bc8e362 --- /dev/null +++ b/u2helper/transmission.py @@ -0,0 +1,68 @@ +# coding=utf-8 + +import time +import requests +import json +import base64 + +from bs4 import BeautifulSoup + +__author__ = 'ty' + +config = json.load(open('config.json')) + +download_dir = config["download_dir"] +url = config["transmission_url"] + +authorization = base64.b64encode(config["transmission_user"] + ":" + + config["transmission_password"]) + +target_dir_parent = config["target_dir_parent"] + +target_dir = {"Lossless Music": target_dir_parent + u"/音乐/", + "BDISO": target_dir_parent + u"/动漫/", + "BDrip": target_dir_parent + u"/动漫/", + u"外挂结构": target_dir_parent + u"/字幕/", + "DVDISO": target_dir_parent + u"/动漫/"} + +headers = {'X-Transmission-Session-Id': '', + "Authorization": "Basic " + authorization} + +list_payload = '''{"method": "torrent-get", "arguments": { +"fields": ["id", "name", "percentDone","status","downloadDir"]}}''' + +r = requests.post(url, headers=headers, data=list_payload, verify=False) + +soup = BeautifulSoup(r.text) +code = soup.find("code") +headers['X-Transmission-Session-Id'] = code.text.split(': ')[1] + +r = requests.post(url, headers=headers, data=list_payload, verify=False) + +result = json.JSONDecoder().decode(r.text) + +# print json.dumps(result, indent=2, ensure_ascii=False) + +with open("seeding.json") as data_file: + seedings = json.load(data_file) + +for torrent in result["arguments"]["torrents"]: + if torrent["downloadDir"] == download_dir and torrent["percentDone"] == 1: + print torrent["name"] + + for seeding in seedings["torrents"]: + if seeding["folder"] == torrent["name"]: + if seeding["catalog"] == "Lossless Music" or seeding["catalog"] == u"外挂结构": + location_payload = '''{"method": "torrent-set-location", "arguments": {"move": true, "location": "''' + \ + target_dir[seeding["catalog"].encode('utf8')] + '''", "ids": [''' + \ + str(torrent["id"]) + ''']}}''' + else: + location_payload = '''{"method": "torrent-set-location", "arguments": {"move": true, "location": "''' + \ + target_dir[seeding["catalog"]] + \ + seeding["name"].encode('utf8') + '''", "ids": [''' + \ + str(torrent["id"]) + ''']}}''' + print location_payload + r = requests.post(url, headers=headers, data=location_payload, verify=False) + print r.text + time.sleep(1) + break diff --git a/u2helper/u2.py b/u2helper/u2.py new file mode 100644 index 0000000..a4660f9 --- /dev/null +++ b/u2helper/u2.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python +import time +import io + +from bs4 import BeautifulSoup +from bs4 import Tag + +from u2torrent import U2Torrent + +import requests +import itertools +import json + +__author__ = 'ty' + +config = json.load(open('config.json')) + +url = 'https://u2.dmhy.org/getusertorrentlistajax.php?userid='+config["uid"]+'&type=seeding' + +info_url = 'https://u2.dmhy.org/torrent_info.php?id=' + +cookies = dict( + nexusphp_u2=config["nexusphp_u2"], + __cfduid=config["__cfduid"]) + +r = requests.get(url, cookies=cookies) + +soup = BeautifulSoup(r.text) + +td_list = soup.find_all('td', {'class': 'rowfollow nowrap'}) + +table_list = soup.find_all('table', {'class': 'torrentname'}) + +torrents_dict = {} +torrents = [] + +count = 0 + +for td, table in itertools.izip(td_list, table_list): + + catalog = "" + for s in td.contents[0].contents: + if isinstance(s, Tag): + catalog += " " + else: + catalog = catalog + s + u2torrent = U2Torrent() + + u2torrent.catalog = catalog + u2torrent.title = table.find('b').string + u2torrent.description = table.find('span').string + + u2torrent.id = int(table.find('a').get('href').split('&')[0].split('=')[1]) + + info_r = requests.get(info_url + str(u2torrent.id), cookies=cookies) + + info_soup = BeautifulSoup(info_r.text) + + info_name = info_soup.find("span", {'class': 'title'}, text="[name]").parent.find("span", {'class': 'value'}) + u2torrent.folder = info_name.text + + u2torrent.name = u2torrent.title.split('][')[0].replace('[', '') + + print u2torrent.name + " : " + u2torrent.folder + + torrents.append(json.JSONDecoder().decode(u2torrent.json())) + count += 1 + time.sleep(3) + break + +torrents_dict["count"] = count +torrents_dict["torrents"] = torrents + +with io.open('seeding.json', 'w', encoding='utf-8') as f: + f.write(unicode(json.dumps(torrents_dict, indent=2, ensure_ascii=False))) diff --git a/u2helper/u2torrent.py b/u2helper/u2torrent.py new file mode 100644 index 0000000..45f0149 --- /dev/null +++ b/u2helper/u2torrent.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python + +__author__ = 'ty' + +import json + +class U2Torrent: + title = "" + id = 0 + catalog = "" + description = "" + name = "" + folder = "" + + def __init__(self): + pass + + def json(self): + return json.dumps(self, default=lambda o: o.__dict__, ensure_ascii=False).encode('utf8')