torrent seeding

This commit is contained in:
j 2011-07-03 18:21:27 +02:00
parent 4bd6c2c6f8
commit 2d4a5a4f40
4 changed files with 79 additions and 7 deletions

View file

@ -9,6 +9,7 @@ from glob import glob
import shutil import shutil
import uuid import uuid
import unicodedata import unicodedata
from urllib import quote
from django.db import models from django.db import models
from django.db.models import Sum from django.db.models import Sum
@ -17,6 +18,7 @@ from django.utils import simplejson as json
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from django.db.models.signals import pre_delete from django.db.models.signals import pre_delete
from django.contrib.sites.models import Site
import ox import ox
from ox.django import fields from ox.django import fields
@ -119,6 +121,9 @@ class Item(models.Model):
icon = models.ImageField(default=None, blank=True, icon = models.ImageField(default=None, blank=True,
upload_to=lambda i, x: i.path("icon.jpg")) upload_to=lambda i, x: i.path("icon.jpg"))
torrent = models.FileField(default=None, blank=True,
upload_to=lambda i, x: i.path('torrent.torrent'))
#stream related fields #stream related fields
stream_aspect = models.FloatField(default=4/3) stream_aspect = models.FloatField(default=4/3)
@ -614,6 +619,58 @@ class Item(models.Model):
return videos return videos
def make_torrent(self):
base = self.path('torrent')
base = os.path.abspath(os.path.join(settings.MEDIA_ROOT, base))
if os.path.exists(base):
shutil.rmtree(base)
os.makedirs(base)
base = self.path('torrent/%s' % self.get('title'))
base = os.path.abspath(os.path.join(settings.MEDIA_ROOT, base))
size = 0
duration = 0.0
if len(self.main_videos()) == 1:
url = "%s/torrent/%s.webm" % (self.get_absolute_url(),
quote(self.get('title').encode('utf-8')))
video = "%s.webm" % base
v = self.main_videos()[0]
os.symlink(v.video.path, video)
info = ox.avinfo(video)
size = info.get('size', 0)
duration = info.get('duration', 0.0)
else:
url = "%s/torrent/" % self.get_absolute_url()
part = 1
os.makedirs(base)
for v in self.main_videos():
video = "%s/%s.Part %d.webm" % (base, self.get('title'), part)
part += 1
os.symlink(v.video.path, video)
info = ox.avinfo(video)
size += info.get('size', 0)
duration += info.get('duration', 0.0)
video = base
torrent = '%s.torrent' % base
url = "http://%s%s" % (Site.objects.get_current().domain, url)
meta = {
'target': torrent,
'url-list': url,
}
if duration:
meta['playtime'] = ox.formatDuration(duration*1000)[:-4]
#slightly bigger torrent file but better for streaming
piece_size_pow2 = 15 #1 mbps -> 32KB pieces
if size / duration >= 1000000:
piece_size_pow2 = 16 #2 mbps -> 64KB pieces
meta['piece_size_pow2'] = piece_size_pow2
ox.torrent.createTorrent(video, settings.TRACKER_URL, meta)
self.torrent.name = self.path('torrent/%s.torrent' % self.get('title'))
self.save()
def update_streams(self): def update_streams(self):
files = {} files = {}
for f in self.main_videos(): for f in self.main_videos():

View file

@ -6,12 +6,9 @@ from django.conf.urls.defaults import *
urlpatterns = patterns("item.views", urlpatterns = patterns("item.views",
(r'^(?P<id>[A-Z0-9].*)/frame/(?P<size>\d+)/(?P<position>[0-9\.,]+).jpg$', 'frame'), (r'^(?P<id>[A-Z0-9].*)/frame/(?P<size>\d+)/(?P<position>[0-9\.,]+).jpg$', 'frame'),
(r'^(?P<id>[A-Z0-9].*)/(?P<oshash>[a-f0-9]+)/(?P<profile>.*\.webm)$', 'video'), (r'^(?P<id>[A-Z0-9].*)/(?P<oshash>[a-f0-9]+)/(?P<profile>.*\.(?P<format>webm|ogv|mp4))$', 'video'),
(r'^(?P<id>[A-Z0-9].*)/(?P<oshash>[a-f0-9]+)/(?P<profile>.*\.ogv)$', 'video'), (r'^(?P<id>[A-Z0-9][A-Za-z0-9]+)/torrent/(?P<filename>.*?)$', 'torrent'),
(r'^(?P<id>[A-Z0-9].*)/(?P<oshash>[a-f0-9]+)/(?P<profile>.*\.mp4)$', 'video'), (r'^(?P<id>[A-Z0-9].*)/(?P<profile>.*\.(?P<format>webm|ogv|mp4))$', 'video'),
(r'^(?P<id>[A-Z0-9].*)/(?P<profile>.*\.webm)$', 'video'),
(r'^(?P<id>[A-Z0-9].*)/(?P<profile>.*\.ogv)$', 'video'),
(r'^(?P<id>[A-Z0-9].*)/(?P<profile>.*\.mp4)$', 'video'),
(r'^(?P<id>[A-Z0-9].*)/poster\.(?P<size>\d+)\.jpg$', 'poster'), (r'^(?P<id>[A-Z0-9].*)/poster\.(?P<size>\d+)\.jpg$', 'poster'),
(r'^(?P<id>[A-Z0-9].*)/poster\.(?P<size>large)\.jpg$', 'poster'), (r'^(?P<id>[A-Z0-9].*)/poster\.(?P<size>large)\.jpg$', 'poster'),
(r'^(?P<id>[A-Z0-9].*)/poster\.jpg$', 'poster'), (r'^(?P<id>[A-Z0-9].*)/poster\.jpg$', 'poster'),

View file

@ -581,8 +581,24 @@ def timeline_overview(request, id, size):
timeline = '%s.%s.png' %(item.timeline_prefix, size) timeline = '%s.%s.png' %(item.timeline_prefix, size)
return HttpFileResponse(timeline, content_type='image/png') return HttpFileResponse(timeline, content_type='image/png')
def torrent(request, id, filename=None):
item = get_object_or_404(models.Item, itemId=id)
if not item.torrent:
raise Http404
if not filename or filename.endswith('.torrent'):
response = HttpFileResponse(item.torrent.path,
content_type='application/x-bittorrent')
filename = "%s.torrent" % item.get('title')
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
return response
while filename.startswith('/'):
filename = filename[1:]
filename = filename.replace('/../', '/')
filename = item.path('torrent/%s' % filename)
filename = os.path.abspath(os.path.join(settings.MEDIA_ROOT, filename))
return HttpFileResponse(filename)
def video(request, id, profile, oshash=None): def video(request, id, profile, oshash=None, format=None):
item = get_object_or_404(models.Item, itemId=id) item = get_object_or_404(models.Item, itemId=id)
if oshash: if oshash:
stream = get_object_or_404(item.files, oshash=oshash) stream = get_object_or_404(item.files, oshash=oshash)

View file

@ -172,6 +172,8 @@ DEFAULT_THEME = "classic"
VIDEO_PROFILE = '96p.webm' VIDEO_PROFILE = '96p.webm'
VIDEO_DERIVATIVES = [] VIDEO_DERIVATIVES = []
TRACKER_URL="http://url2torrent.net:6970/announce"
#0xdb #0xdb
''' '''
VIDEO_PROFILE = '96p.webm' VIDEO_PROFILE = '96p.webm'