From bb4b001fb656bd40b2816aa7055ce0e23e6dd32c Mon Sep 17 00:00:00 2001 From: Artem Glebov Date: Fri, 26 Apr 2013 10:18:21 +0300 Subject: [PATCH] Ability to stop all torrents on video playback --- addon.xml | 3 ++ resources/language/English/strings.xml | 1 + resources/language/Russian/strings.xml | 1 + resources/lib/common.py | 30 ++++++++++++++ resources/lib/gui.py | 20 +++------ resources/lib/player.py | 57 ++++++++++++++++++++++++++ resources/settings.xml | 1 + 7 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 resources/lib/common.py create mode 100644 resources/lib/player.py diff --git a/addon.xml b/addon.xml index 43de605..15b419c 100644 --- a/addon.xml +++ b/addon.xml @@ -10,6 +10,9 @@ + A client for the popular Transmission BitTorrent application A népszerű Transmission Bittorrent alkalmazás vezérlése diff --git a/resources/language/English/strings.xml b/resources/language/English/strings.xml index 224e873..8212a20 100644 --- a/resources/language/English/strings.xml +++ b/resources/language/English/strings.xml @@ -41,6 +41,7 @@ Port User Password + Stop all torrents on video playback An unexpected error occurred diff --git a/resources/language/Russian/strings.xml b/resources/language/Russian/strings.xml index 3261868..3b97bfd 100644 --- a/resources/language/Russian/strings.xml +++ b/resources/language/Russian/strings.xml @@ -46,6 +46,7 @@ Порт Пользователь Пароль + Останавливать все закачки на время просмотра видео Неизвестная ошибка diff --git a/resources/lib/common.py b/resources/lib/common.py new file mode 100644 index 0000000..f73b51d --- /dev/null +++ b/resources/lib/common.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2013 Artem Glebov + +import sys +import transmissionrpc + +__settings__ = sys.modules[ "__main__" ].__settings__ + +def get_settings(): + params = { + 'address': __settings__.getSetting('rpc_host'), + 'port': __settings__.getSetting('rpc_port'), + 'user': __settings__.getSetting('rpc_user'), + 'password': __settings__.getSetting('rpc_password'), + 'stop_all_on_playback': __settings__.getSetting('stop_all_on_playback') + } + return params + +def get_params(): + params = { + 'address': __settings__.getSetting('rpc_host'), + 'port': __settings__.getSetting('rpc_port'), + 'user': __settings__.getSetting('rpc_user'), + 'password': __settings__.getSetting('rpc_password'), + } + return params + +def get_rpc_client(): + params = get_params() + return transmissionrpc.Client(**params) diff --git a/resources/lib/gui.py b/resources/lib/gui.py index c2a3cb7..1431c5d 100644 --- a/resources/lib/gui.py +++ b/resources/lib/gui.py @@ -9,6 +9,7 @@ import xbmcgui from basictypes.bytes import Bytes import transmissionrpc import search +import common _ = sys.modules[ "__main__" ].__language__ __settings__ = sys.modules[ "__main__" ].__settings__ @@ -31,27 +32,16 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog): self.list = {} self.torrents = {} self.timer = None - def get_settings(self): - params = { - 'address': __settings__.getSetting('rpc_host'), - 'port': __settings__.getSetting('rpc_port'), - 'user': __settings__.getSetting('rpc_user'), - 'password': __settings__.getSetting('rpc_password') - } - return params def set_settings(self, params): __settings__.setSetting('rpc_host', params['address']) __settings__.setSetting('rpc_port', params['port']) __settings__.setSetting('rpc_user', params['user']) __settings__.setSetting('rpc_password', params['password']) - def get_rpc_client(self): - params = self.get_settings() - return transmissionrpc.Client(**params) def onInit(self): p = xbmcgui.DialogProgress() p.create(_(0), _(1)) # 'Transmission', 'Connecting to Transmission' try: - self.transmission = self.get_rpc_client() + self.transmission = common.get_rpc_client() except: p.close() self.close() @@ -192,12 +182,12 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog): self.close() if (controlID == 118): # Settings button - prev_settings = self.get_settings() + prev_settings = common.get_settings() __settings__.openSettings() p = xbmcgui.DialogProgress() p.create(_(0), _(1)) # 'Transmission', 'Connecting to Transmission' try: - self.transmission = self.get_rpc_client() + self.transmission = common.get_rpc_client() self.updateTorrents() p.close() except: @@ -206,7 +196,7 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog): # restore settings self.set_settings(prev_settings) try: - self.transmission = self.get_rpc_client() + self.transmission = common.get_rpc_client() except err: xbmcgui.Dialog().ok(_(2), _(9001)) self.close() diff --git a/resources/lib/player.py b/resources/lib/player.py new file mode 100644 index 0000000..c53b102 --- /dev/null +++ b/resources/lib/player.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Copyright (c) 2013 Paul Price, Artem Glebov + +import os +import sys +import xbmc, xbmcaddon, xbmcgui +import transmissionrpc + +__settings__ = xbmcaddon.Addon(id='script.transmission') + +BASE_RESOURCE_PATH = xbmc.translatePath( os.path.join( __settings__.getAddonInfo('path'), 'resources', 'lib' ) ) +sys.path.append (BASE_RESOURCE_PATH) + +import common + +class SubstitutePlayer(xbmc.Player): + def __init__(self): + xbmc.Player.__init__(self) + self.prev_settings = {} + self.refreshSettings() + + def onPlayBackStarted(self): + self.refreshSettings() + if self.active and xbmc.Player().isPlayingVideo(): + self.stopAllTorrents() + + def onPlayBackStopped(self): + self.refreshSettings() + if self.active: + self.startAllTorrents() + + def startAllTorrents(self): + if self.transmission: + torrents = self.transmission.list() + for tid, torrent in torrents.iteritems(): + self.transmission.start(tid) + + def stopAllTorrents(self): + if self.transmission: + torrents = self.transmission.list() + for tid, torrent in torrents.iteritems(): + self.transmission.stop(tid) + + def refreshSettings(self): + settings = common.get_settings() + if settings != self.prev_settings: + self.active = (settings['stop_all_on_playback'] == 'true') + try: + self.transmission = common.get_rpc_client() + except: + self.transmission = None + self.prev_settings = settings + +player = SubstitutePlayer() + +while (not xbmc.abortRequested): + xbmc.sleep(5000); diff --git a/resources/settings.xml b/resources/settings.xml index db97707..2d2053d 100644 --- a/resources/settings.xml +++ b/resources/settings.xml @@ -5,4 +5,5 @@ +