Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
werwolfby committed Mar 15, 2016
2 parents eb85c6e + 550faeb commit fec7e9d
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 30 deletions.
30 changes: 20 additions & 10 deletions monitorrent/plugins/trackers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,25 @@ def add_topic(self, url, params):

def get_topics(self, ids):
with DBSession() as db:
topics = db.query(self.topic_class).filter(self.topic_class.status.in_((Status.Ok, Status.Error))).all()
topics = db.query(self.topic_class)\
.filter(self.topic_class.status.in_((Status.Ok, Status.Error)))\
.all()
db.expunge_all()
return topics

def save_topic(self, topic, last_update, status=Status.Ok):
if not isinstance(topic, self.topic_class):
raise Exception("Can't update topic of wrong class. Expected {0}, but was {1}"
.format(self.topic_class, topic.__class__))

with DBSession() as db:
db_serie = topic
if last_update is not None:
db_serie.last_update = last_update
db_serie.status = status
db.add(topic)
db.commit()

def get_topic(self, id):
with DBSession() as db:
topic = db.query(self.topic_class).filter(Topic.id == id).first()
Expand Down Expand Up @@ -178,10 +193,7 @@ def execute(self, ids, engine):
if hasattr(self, 'check_download'):
status = self.check_download(response)
if topic.status != status:
with DBSession() as db:
db.add(topic)
topic.status = status
db.commit()
self.save_topic(topic, None, status)
if status != Status.Ok:
engine.log.failed(u"Torrent status changed: {}".format(status))
continue
Expand All @@ -196,11 +208,9 @@ def execute(self, ids, engine):
if torrent.info_hash != old_hash:
engine.log.downloaded(u"Torrent <b>%s</b> was changed" % topic_name, torrent_content)
last_update = engine.add_torrent(filename, torrent, old_hash)
with DBSession() as db:
db.add(topic)
topic.hash = torrent.info_hash
topic.last_update = last_update
db.commit()
topic.hash = torrent.info_hash
topic.last_update = last_update
self.save_topic(topic, last_update, Status.Ok)
else:
engine.log.info(u"Torrent <b>%s</b> not changed" % topic_name)
except Exception as e:
Expand Down
27 changes: 13 additions & 14 deletions monitorrent/plugins/trackers/lostfilm.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,14 +517,15 @@ def execute(self, ids, engine):
try:
display_name = serie.display_name
episodes = self._prepare_request(serie)
status = Status.Ok
if isinstance(episodes, Response):
status = self.check_download(episodes)
if serie.status != status:
with DBSession() as db:
db.add(serie)
serie.status = status
db.commit()
engine.log.failed(u"Torrent status: %s" % status.__str__())

if serie.status != status:
self.save_topic(serie, None, status)

if status != Status.Ok:
engine.log.failed(u"Torrent status changed: {}".format(status))
continue

if episodes is None or len(episodes) == 0:
Expand All @@ -538,6 +539,8 @@ def execute(self, ids, engine):
if download_info is None:
engine.log.failed(u'Failed get quality "{0}" for series: {1}'
.format(serie.quality, cgi.escape(display_name)))
# Should fail to get quality be treated as NotFound?
self.save_topic(serie, None, Status.Error)
break

try:
Expand All @@ -548,6 +551,7 @@ def execute(self, ids, engine):
except Exception as e:
engine.log.failed(u"Failed to download from <b>{0}</b>.\nReason: {1}"
.format(download_info['download_url'], cgi.escape(unicode(e))))
self.save_topic(serie, None, Status.Error)
continue
if not filename:
filename = display_name
Expand All @@ -556,15 +560,10 @@ def execute(self, ids, engine):
engine.log.downloaded(u'Download new series: {0} ({1}, {2})'
.format(display_name, info[0], info[1]),
torrent_content)
serie.season = info[0]
serie.episode = info[1]
last_update = engine.add_torrent(filename, torrent, None)
with DBSession() as db:
db_serie = db.query(LostFilmTVSeries) \
.filter(LostFilmTVSeries.id == serie.id) \
.first()
db_serie.last_update = last_update
db_serie.season = info[0]
db_serie.episode = info[1]
db.commit()
self.save_topic(serie, last_update, Status.Ok)

except Exception as e:
engine.log.failed(u"Failed update <b>lostfilm</b> series: {0}.\nReason: {1}"
Expand Down
61 changes: 61 additions & 0 deletions monitorrent/tests/plugins/trackers/test_tracker_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ def download_func(request, **kwargs):
self.assertEqual(topic.status, Status.Ok)


@ddt
class TrackerPluginBaseTest(DbTestCase):
class MockTopic(Topic):
__tablename__ = "mocktopic_base_series"
Expand All @@ -254,6 +255,17 @@ class MockTopic(Topic):
'polymorphic_identity': 'base.mocktracker.com'
}

class WrongMockTopic(Topic):
__tablename__ = "wrong_mocktopic_base_series"

id = Column(Integer, ForeignKey('topics.id'), primary_key=True)
additional_attribute = Column(String, nullable=False)
hash = Column(String, nullable=True)

__mapper_args__ = {
'polymorphic_identity': 'wrong.base.mocktracker.com'
}

def setUp(self):
super(TrackerPluginBaseTest, self).setUp()

Expand Down Expand Up @@ -336,6 +348,55 @@ def test_get_topic(self):
# noinspection PyTypeChecker
self.assertIsNone(plugin.get_topic(fields['id'] + 1))

@unpack
@data({'last_update': datetime(2016, 3, 15, 18, 58, 12, tzinfo=pytz.utc), 'status': Status.Ok},
{'last_update': None, 'status': Status.Error})
def test_save_topic(self, last_update, status):
plugin = MockTrackerPlugin()
plugin.topic_private_fields = plugin.topic_private_fields + ['additional_attribute']
plugin.topic_class = self.MockTopic
original_url = 'http://base.mocktracker.org/torrent/1'
topic_last_update = datetime(2016, 3, 14, 18, 58, 12, tzinfo=pytz.utc)
fields = {
'url': original_url,
'display_name': 'Original Name / Translated Name / Info',
'additional_attribute': 'Text',
'type': 'base.mocktracker.com',
'status': Status.Ok,
'last_update': topic_last_update
}
with DBSession() as db:
topic = self.MockTopic(**fields)
db.add(topic)
db.commit()
fields['id'] = topic.id

topic = plugin.get_topics(None)[0]
plugin.save_topic(topic, last_update, status)

topic = plugin.get_topics(None)[0]
self.assertEqual(last_update or topic_last_update, topic.last_update)
self.assertEqual(status, topic.status)

def test_save_topic_failed(self):
plugin = MockTrackerPlugin()
plugin.topic_private_fields = plugin.topic_private_fields + ['additional_attribute']
plugin.topic_class = self.MockTopic
original_url = 'http://base.mocktracker.org/torrent/1'
topic_last_update = datetime(2016, 3, 14, 18, 58, 12, tzinfo=pytz.utc)
fields = {
'url': original_url,
'display_name': 'Original Name / Translated Name / Info',
'additional_attribute': 'Text',
'type': 'base.mocktracker.com',
'status': Status.Ok,
'last_update': topic_last_update
}

with self.assertRaises(Exception):
topic = self.WrongMockTopic(**fields)
plugin.save_topic(topic, None, Status.Ok)

def test_update_topic(self):
plugin = MockTrackerPlugin()
plugin.topic_private_fields = plugin.topic_private_fields + ['additional_attribute']
Expand Down
14 changes: 8 additions & 6 deletions src/directives/mtTorrentsListHeader/mt-torrents-list-header.html
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<div class="mt-torrents-list-header" ng-class="status" layout="row" layout-align="space-between center">
<div layout="column" layout-align="start center">
<a ng-if="execute" ng-href="{{execute ? ('#/logs/' + execute.id + '/details') : null}}" disabled><span><b>Last Executed</b>: {{execute.finish_time | date : 'dd.MM.yyyy HH:mm:ss'}} <i ng-if="!executing">({{relative_execute}})</i></span></a>
<span ng-if="!execute"><b>Last Executed</b>: <i>...loading...</i></span>
<div ng-if="!executing">
<a ng-if="execute" ng-href="{{execute ? ('#/logs/' + execute.id + '/details') : null}}" disabled><span><b>Last Executed</b>: {{execute.finish_time | date : 'HH:mm'}} <i ng-if="!executing">({{relative_execute}})</i></span></a>
<span ng-if="!execute"><b>Last Executed</b>: <i>...loading...</i></span>
</div>
<div layout="column" layout-align="start center" ng-if="executing">
<span><b>Executing</b>: <span ng-bind-html="latest_log_message.message"></span></span>
</div>
</div>
<div class="md-actions" layout="row" layout-align="end center">
<md-button ng-click="executeClicked($event)" class="md-icon-button" aria-label="Execute">
<md-icon md-svg-icon="content/icons/input.svg"></md-icon>
</md-button>
</div>
</div>
<div class="mt-torrents-list-header" layout="row" layout-align="space-between center" ng-if="executing">
<div layout="column" layout-align="start center">
<span ng-bind-html="latest_log_message.message"></span>
</div>
<div class="mt-torrents-list-header" layout="row" layout-align="space-between center">
</div>
<md-progress-linear md-mode="indeterminate" ng-if="executing"></md-progress-linear>
<md-divider></md-divider>
Expand Down

0 comments on commit fec7e9d

Please sign in to comment.