cablegates/pandora/torrent/transmission.py

79 lines
1.9 KiB
Python
Raw Normal View History

2010-06-24 13:11:34 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import with_statement
import os
import time
import base64
from subprocess import Popen
from django.conf import settings
2010-07-07 22:46:41 +00:00
import ox.torrent
2010-06-24 13:11:34 +00:00
import transmissionrpc
2011-01-01 11:44:42 +00:00
2010-06-24 13:11:34 +00:00
def connect():
return transmissionrpc.Client(settings.TRANSMISSON_HOST,
port=settings.TRANSMISSON_PORT,
user=settings.TRANSMISSON_USER,
password=settings.TRANSMISSON_PASSWORD)
2011-01-01 11:44:42 +00:00
2010-06-24 13:11:34 +00:00
def remove(info_hash):
if settings.DEBUG:
print 'remove', info_hash
if info_hash:
try:
tc = connect()
tc.remove(info_hash.lower())
except:
2011-01-01 11:44:42 +00:00
if settings.DEBUG:
2010-06-24 13:11:34 +00:00
import traceback
traceback.print_exc()
2011-01-01 11:44:42 +00:00
2010-06-24 13:11:34 +00:00
def add(torrent_file):
download_dir = os.path.dirname(torrent_file)
with open(torrent_file) as f:
torrent_data = base64.b64encode(f.read())
2010-07-07 22:46:41 +00:00
info_hash = ox.torrent.getInfoHash(torrent_file)
2010-06-24 13:11:34 +00:00
try:
tc = connect()
if not is_seeding(info_hash):
tc.add(torrent_data, download_dir=download_dir)
except:
if settings.DEBUG:
import traceback
traceback.print_exc()
2011-01-01 11:44:42 +00:00
2010-06-24 13:11:34 +00:00
def is_seeding(info_hash):
info_hash = info_hash.lower()
try:
tc = connect()
torrents = tc.info(info_hash)
except:
torrents = False
if settings.DEBUG:
import traceback
traceback.print_exc()
if torrents:
return True
return False
2011-01-01 11:44:42 +00:00
2010-06-24 13:11:34 +00:00
def start_daemon():
try:
tc = connect()
except:
Popen(['transmission-daemon',
2010-12-01 15:25:22 +00:00
'-M',
2010-06-24 13:11:34 +00:00
'-a', '127.0.0.1',
'-r', '127.0.0.1',
'-p', str(settings.TRANSMISSON_PORT),
'--auth',
'-u', settings.TRANSMISSON_USER,
'-v', settings.TRANSMISSON_PASSWORD,
'-w', settings.MEDIA_ROOT,
])
time.sleep(1)