diff options
author | Dan McGee <dan@archlinux.org> | 2013-01-19 20:08:06 +0100 |
---|---|---|
committer | Dan McGee <dan@archlinux.org> | 2013-01-19 20:08:06 +0100 |
commit | 71c0c7453a5bc28b6e6576fe4f1351139f33ade5 (patch) | |
tree | 084d2b13072c539bc28651acf3a6679ff2f09ebe /releng | |
parent | 1a7e5d22f1c4e948c624d26b4d8c1ed30189acfe (diff) | |
download | archweb-71c0c7453a5bc28b6e6576fe4f1351139f33ade5.tar.gz archweb-71c0c7453a5bc28b6e6576fe4f1351139f33ade5.tar.xz |
Implement torrent data parsing and extraction via bencode
This allows uploading of the actual torrent file itself into the webapp
and then pulling the relevant pieces of information out of it.
Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'releng')
-rw-r--r-- | releng/models.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/releng/models.py b/releng/models.py index 8bc54de..c7d2696 100644 --- a/releng/models.py +++ b/releng/models.py @@ -1,4 +1,9 @@ +from base64 import b64decode +from bencode import bdecode, bencode +from datetime import datetime +import hashlib import markdown +from pytz import utc from django.core.urlresolvers import reverse from django.db import models @@ -150,6 +155,34 @@ class Release(models.Model): return mark_safe(markdown.markdown( self.info, safe_mode=True, enable_attributes=False)) + def torrent(self): + try: + data = b64decode(self.torrent_data) + except TypeError: + return None + data = bdecode(data) + # transform the data into a template-friendly dict + info = data.get('info', {}) + metadata = { + 'comment': data.get('comment', None), + 'created_by': data.get('created by', None), + 'creation_date': None, + 'announce': data.get('announce', None), + 'file_name': info.get('name', None), + 'file_length': info.get('length', None), + 'piece_count': len(info.get('pieces', '')) / 20, + 'piece_length': info.get('piece length', None), + 'url_list': data.get('url-list', []), + 'info_hash': None, + } + if 'creation date' in data: + created= datetime.utcfromtimestamp(data['creation date']) + metadata['creation_date'] = created.replace(tzinfo=utc) + if info: + metadata['info_hash'] = hashlib.sha1(bencode(info)).hexdigest() + + return metadata + for model in (Iso, Test, Release): pre_save.connect(set_created_field, sender=model, |