104 lines
3.3 KiB
Python
104 lines
3.3 KiB
Python
import bencodepy
|
|
import logging
|
|
import glob
|
|
from sh import find
|
|
import base64
|
|
import xmlrpc.client
|
|
import os.path
|
|
import time
|
|
import hashlib
|
|
import random
|
|
|
|
|
|
session_dir = 'session'
|
|
dl_dir = '/data/downloads'
|
|
|
|
logging.basicConfig(level=logging.INFO)
|
|
logging.getLogger('sh.command').setLevel(logging.WARNING)
|
|
|
|
rtorrent = xmlrpc.client.ServerProxy("http://localhost:8032/RPC2")
|
|
dl_list = rtorrent.download_list()
|
|
|
|
def parent_dir(path):
|
|
return os.path.abspath(os.path.join(path, os.pardir))
|
|
|
|
def get_label(path):
|
|
return path.split(os.path.sep)[3]
|
|
|
|
def compute_hash(torrent):
|
|
encoded_info = bencodepy.encode(torrent[b'info'])
|
|
return hashlib.sha1(encoded_info).hexdigest().upper()
|
|
|
|
def add_torrent(torrent, filename, path):
|
|
infohash = compute_hash(torrent)
|
|
if infohash in dl_list:
|
|
logging.info('Torrent already loaded, skipping')
|
|
return
|
|
|
|
with open(filename, 'rb') as fd:
|
|
data = xmlrpc.client.Binary(fd.read())
|
|
|
|
dir_path = parent_dir(path)
|
|
label = get_label(path)
|
|
logging.info('Adding torrent')
|
|
rtorrent.load.raw(
|
|
'',
|
|
data,
|
|
'd.custom1.set="%s"' % label,
|
|
'd.directory.set="%s"' % dir_path,
|
|
'd.check_hash='
|
|
)
|
|
logging.info('Checking torrent')
|
|
|
|
while True:
|
|
time.sleep(0.5)
|
|
if rtorrent.d.complete(infohash):
|
|
logging.info('Torrent complete, starting')
|
|
rtorrent.d.start(infohash)
|
|
break
|
|
# if not rtorrent.d.is_hash_checking(infohash):
|
|
# break
|
|
#if rtorrent.d.complete(infohash):
|
|
# logging.info('Torrent complete, starting')
|
|
# rtorrent.d.start(infohash)
|
|
#else:
|
|
# logging.info('Torrent not complete, skipping')
|
|
|
|
|
|
def main():
|
|
with open('conflicting.txt', 'w') as fd:
|
|
torrent_list = list(glob.glob(session_dir + '/*.torrent'))
|
|
random.shuffle(torrent_list)
|
|
for filename in torrent_list:
|
|
if filename[-7:] == 'torrent':
|
|
t = bencodepy.decode_from_file(filename)
|
|
logging.info('Processing torrent %s', filename)
|
|
announce = t.get(b'announce')
|
|
if b't411' in announce:
|
|
logging.info('Old torrent, skipping')
|
|
continue
|
|
info = t.get(b'info')
|
|
if info is None:
|
|
logging.info('Torrent has no info', filename)
|
|
continue
|
|
name = info.get(b'name').decode('utf-8')
|
|
cmd = find(dl_dir, '-maxdepth', 2, '-name', name)
|
|
matches = cmd.stdout.decode('utf-8').splitlines()
|
|
if matches:
|
|
logging.info('Found matches, %s', repr(matches))
|
|
if len(matches) == 1:
|
|
try:
|
|
add_torrent(t, filename, matches[0])
|
|
except Exception as e:
|
|
logging.error('Error while adding torrent %s', e)
|
|
else:
|
|
logging.info('Conflicting torrent, several matches found, writing to file for further inspection')
|
|
fd.write('%s\t%s\n' % (filename, repr(matches)))
|
|
else:
|
|
logging.info('No matches found for name "%s"', name)
|
|
#input()
|
|
time.sleep(1)
|
|
|
|
if __name__ == "__main__":
|
|
main()
|