Skip to content

Commit

Permalink
[TASK] Simplified custom torrent parser
Browse files Browse the repository at this point in the history
The custom torrent parser that was in use had extra methods that were
not needed and the code was more complex than necessary. Removed the
extra methods and the inner class, and created simple Bdecode class
that handles parsing a proper torrent file.

Added test cases to make sure custom parser is completely tested
and follows the entire torrent spec.
  • Loading branch information
shad7 committed Jun 15, 2015
1 parent f8d40d5 commit 4c438c5
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 382 deletions.
93 changes: 0 additions & 93 deletions etc/seedbox/logging.cfg

This file was deleted.

15 changes: 13 additions & 2 deletions seedbox/tests/torrent/test_loader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import absolute_import

import glob
import os
import tempfile
Expand Down Expand Up @@ -115,6 +113,19 @@ def test_torrent_downloading(self):
result = loader._is_torrent_downloading(mediafiles)
self.assertTrue(result)

def test_filter_media(self):

self.CONF.set_override('media_paths',
[self.base_dir],
group='torrent')

media1 = tempfile.NamedTemporaryFile(suffix='.mp4',
dir=self.base_dir)

media_list = loader._filter_media('1234', [(media1.name, 75000001)])
self.assertEqual(len(media_list), 1)
self.assertEqual(media_list[0].filename, os.path.basename(media1.name))

def test_get_file_path(self):

self.CONF.set_override('media_paths',
Expand Down
28 changes: 10 additions & 18 deletions seedbox/tests/torrent/test_parser.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from __future__ import absolute_import

import glob
import os

Expand All @@ -21,10 +19,7 @@ def test_parse(self):
for tfile in tfiles:
torrent = parser.TorrentParser(tfile)
self.assertIsNotNone(torrent)
self.assertIsNotNone(torrent.get_tracker_url())
self.assertIsNotNone(torrent.get_creation_date())
self.assertIsNotNone(torrent.get_creation_date(str()))
self.assertTrue(len(torrent.get_files_details()) > 0)
self.assertTrue(len(torrent.get_file_details()) > 0)

def test_custom_parser1(self):
tfile = os.path.join(torrent_path, 'bencode-bad-1.torrent')
Expand Down Expand Up @@ -56,19 +51,16 @@ def test_large_torrent(self):
torrent = parser.TorrentParser(tfile)
self.assertIsNotNone(torrent)

def test_client_name(self):
tfile = os.path.join(torrent_path, 'other-5.torrent')
torrent = parser.TorrentParser(tfile)
self.assertIsNotNone(torrent.get_client_name())

tfile = os.path.join(torrent_path, 'other-9.torrent')
torrent = parser.TorrentParser(tfile)
self.assertIsNone(torrent.get_client_name())

def test_missing_file(self):

with testtools.ExpectedException(ValueError):
parser.TorrentParser(str())

with testtools.ExpectedException(IOError):
parser.TorrentParser('dummy/file/does/not/exist')

def test_parse_negative_integer(self):
self.assertEqual(parser.Bdecode.parse(b'i-123e'), -123)

def test_parse_invalid_integer(self):
self.assertRaises(parser.ParsingError, parser.Bdecode.parse, b'i123ae')

def test_parse_invalid_str(self):
self.assertRaises(parser.ParsingError, parser.Bdecode.parse, b'0:ae')
2 changes: 1 addition & 1 deletion seedbox/torrent/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def load_torrents(dbapi):
LOG.exception('Torrent Parsing Error: [%s]', torrent_file)
continue

media_items = torparser.get_files_details()
media_items = torparser.get_file_details()
LOG.debug('Total files in torrent %d', len(media_items))

# determine if any of the files are still inprogress of
Expand Down
Loading

0 comments on commit 4c438c5

Please sign in to comment.