From 0510588b286c4f42583d5ac4d995dc36cf46b256 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 24 Jun 2010 15:11:34 +0200 Subject: [PATCH] add transmission interface --- pandora/settings.py | 6 ++ pandora/templates/index.html | 2 +- pandora/torrent/__init__.py | 0 pandora/torrent/management/__init__.py | 0 .../torrent/management/commands/__init__.py | 0 .../torrent/management/commands/seeding.py | 22 ++++++ pandora/torrent/models.py | 3 + pandora/torrent/tests.py | 23 ++++++ pandora/torrent/transmission.py | 73 +++++++++++++++++++ pandora/torrent/views.py | 1 + requirements.txt | 1 + 11 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 pandora/torrent/__init__.py create mode 100644 pandora/torrent/management/__init__.py create mode 100644 pandora/torrent/management/commands/__init__.py create mode 100644 pandora/torrent/management/commands/seeding.py create mode 100644 pandora/torrent/models.py create mode 100644 pandora/torrent/tests.py create mode 100644 pandora/torrent/transmission.py create mode 100644 pandora/torrent/views.py diff --git a/pandora/settings.py b/pandora/settings.py index b1f0d9d9..7179038c 100644 --- a/pandora/settings.py +++ b/pandora/settings.py @@ -110,6 +110,7 @@ INSTALLED_APPS = ( 'app', 'backend', 'oxuser', + 'torrent', ) AUTH_PROFILE_MODULE = 'oxuser.UserProfile' @@ -124,6 +125,11 @@ VIDEO_ENCODING = { 'high': {'profile': 'padma'} } +TRANSMISSON_HOST='localhost' +TRANSMISSON_PORT=9091 +TRANSMISSON_USER='transmission' +TRANSMISSON_PASSWORD='transmission' + #overwrite default settings with local settings try: from local_settings import * diff --git a/pandora/templates/index.html b/pandora/templates/index.html index a6e74070..6fc6e92b 100644 --- a/pandora/templates/index.html +++ b/pandora/templates/index.html @@ -9,7 +9,7 @@ - + diff --git a/pandora/torrent/__init__.py b/pandora/torrent/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pandora/torrent/management/__init__.py b/pandora/torrent/management/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pandora/torrent/management/commands/__init__.py b/pandora/torrent/management/commands/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/pandora/torrent/management/commands/seeding.py b/pandora/torrent/management/commands/seeding.py new file mode 100644 index 00000000..7f1cb753 --- /dev/null +++ b/pandora/torrent/management/commands/seeding.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +# vi:si:et:sw=4:sts=4:ts=4 + +import os +from os.path import join, dirname, basename, splitext, exists + +from django.core.management.base import BaseCommand, CommandError +from django.conf import settings + +from ... import daemon + + +class Command(BaseCommand): + """ + listen to rabbitmq and execute encoding tasks. + """ + help = 'listen to rabbitmq and execute encoding tasks.' + args = '' + + def handle(self, **options): + transmission.startDaemon() + diff --git a/pandora/torrent/models.py b/pandora/torrent/models.py new file mode 100644 index 00000000..71a83623 --- /dev/null +++ b/pandora/torrent/models.py @@ -0,0 +1,3 @@ +from django.db import models + +# Create your models here. diff --git a/pandora/torrent/tests.py b/pandora/torrent/tests.py new file mode 100644 index 00000000..2247054b --- /dev/null +++ b/pandora/torrent/tests.py @@ -0,0 +1,23 @@ +""" +This file demonstrates two different styles of tests (one doctest and one +unittest). These will both pass when you run "manage.py test". + +Replace these with more appropriate tests for your application. +""" + +from django.test import TestCase + +class SimpleTest(TestCase): + def test_basic_addition(self): + """ + Tests that 1 + 1 always equals 2. + """ + self.failUnlessEqual(1 + 1, 2) + +__test__ = {"doctest": """ +Another way to test that 1 + 1 is equal to 2. + +>>> 1 + 1 == 2 +True +"""} + diff --git a/pandora/torrent/transmission.py b/pandora/torrent/transmission.py new file mode 100644 index 00000000..a58ea0df --- /dev/null +++ b/pandora/torrent/transmission.py @@ -0,0 +1,73 @@ +# -*- 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 +import oxlib.torrent +import transmissionrpc + +def connect(): + return transmissionrpc.Client(settings.TRANSMISSON_HOST, + port=settings.TRANSMISSON_PORT, + user=settings.TRANSMISSON_USER, + password=settings.TRANSMISSON_PASSWORD) + +def remove(info_hash): + if settings.DEBUG: + print 'remove', info_hash + if info_hash: + try: + tc = connect() + tc.remove(info_hash.lower()) + except: + if DEBUG: + import traceback + traceback.print_exc() + +def add(torrent_file): + download_dir = os.path.dirname(torrent_file) + with open(torrent_file) as f: + torrent_data = base64.b64encode(f.read()) + info_hash = oxlib.torrent.getInfoHash(torrent_file) + 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() + +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 + +def start_daemon(): + try: + tc = connect() + except: + Popen(['transmission-daemon', + '-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) + diff --git a/pandora/torrent/views.py b/pandora/torrent/views.py new file mode 100644 index 00000000..60f00ef0 --- /dev/null +++ b/pandora/torrent/views.py @@ -0,0 +1 @@ +# Create your views here. diff --git a/requirements.txt b/requirements.txt index f0aeabd0..82d18888 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,3 +11,4 @@ carrot django_extensions -e bzr+http://firefogg.org/dev/python-firefogg/#egg=python-firefogg -e git+git://github.com/dcramer/django-devserver#egg=django_devserver +transmissionrpc