Ability to stop all torrents on video playback

This commit is contained in:
Artem Glebov 2013-04-26 10:18:21 +03:00
parent 7d1d517102
commit bb4b001fb6
7 changed files with 98 additions and 15 deletions

View file

@ -10,6 +10,9 @@
</requires> </requires>
<extension point="xbmc.python.script" <extension point="xbmc.python.script"
library="default.py" /> library="default.py" />
<extension point="xbmc.service"
library="resources/lib/player.py"
start="startup"/>
<extension point="xbmc.addon.metadata"> <extension point="xbmc.addon.metadata">
<summary lang="en">A client for the popular Transmission BitTorrent application</summary> <summary lang="en">A client for the popular Transmission BitTorrent application</summary>
<summary lang="hu">A népszerű Transmission Bittorrent alkalmazás vezérlése</summary> <summary lang="hu">A népszerű Transmission Bittorrent alkalmazás vezérlése</summary>

View file

@ -41,6 +41,7 @@
<string id="1002">Port</string> <string id="1002">Port</string>
<string id="1003">User</string> <string id="1003">User</string>
<string id="1004">Password</string> <string id="1004">Password</string>
<string id="1005">Stop all torrents on video playback</string>
<!-- Errors --> <!-- Errors -->
<string id="9000">An unexpected error occurred</string> <string id="9000">An unexpected error occurred</string>

View file

@ -46,6 +46,7 @@
<string id="1002">Порт</string> <string id="1002">Порт</string>
<string id="1003">Пользователь</string> <string id="1003">Пользователь</string>
<string id="1004">Пароль</string> <string id="1004">Пароль</string>
<string id="1005">Останавливать все закачки на время просмотра видео</string>
<!-- Errors --> <!-- Errors -->
<string id="9000">Неизвестная ошибка</string> <string id="9000">Неизвестная ошибка</string>

30
resources/lib/common.py Normal file
View file

@ -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)

View file

@ -9,6 +9,7 @@ import xbmcgui
from basictypes.bytes import Bytes from basictypes.bytes import Bytes
import transmissionrpc import transmissionrpc
import search import search
import common
_ = sys.modules[ "__main__" ].__language__ _ = sys.modules[ "__main__" ].__language__
__settings__ = sys.modules[ "__main__" ].__settings__ __settings__ = sys.modules[ "__main__" ].__settings__
@ -31,27 +32,16 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog):
self.list = {} self.list = {}
self.torrents = {} self.torrents = {}
self.timer = None 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): def set_settings(self, params):
__settings__.setSetting('rpc_host', params['address']) __settings__.setSetting('rpc_host', params['address'])
__settings__.setSetting('rpc_port', params['port']) __settings__.setSetting('rpc_port', params['port'])
__settings__.setSetting('rpc_user', params['user']) __settings__.setSetting('rpc_user', params['user'])
__settings__.setSetting('rpc_password', params['password']) __settings__.setSetting('rpc_password', params['password'])
def get_rpc_client(self):
params = self.get_settings()
return transmissionrpc.Client(**params)
def onInit(self): def onInit(self):
p = xbmcgui.DialogProgress() p = xbmcgui.DialogProgress()
p.create(_(0), _(1)) # 'Transmission', 'Connecting to Transmission' p.create(_(0), _(1)) # 'Transmission', 'Connecting to Transmission'
try: try:
self.transmission = self.get_rpc_client() self.transmission = common.get_rpc_client()
except: except:
p.close() p.close()
self.close() self.close()
@ -192,12 +182,12 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog):
self.close() self.close()
if (controlID == 118): if (controlID == 118):
# Settings button # Settings button
prev_settings = self.get_settings() prev_settings = common.get_settings()
__settings__.openSettings() __settings__.openSettings()
p = xbmcgui.DialogProgress() p = xbmcgui.DialogProgress()
p.create(_(0), _(1)) # 'Transmission', 'Connecting to Transmission' p.create(_(0), _(1)) # 'Transmission', 'Connecting to Transmission'
try: try:
self.transmission = self.get_rpc_client() self.transmission = common.get_rpc_client()
self.updateTorrents() self.updateTorrents()
p.close() p.close()
except: except:
@ -206,7 +196,7 @@ class TransmissionGUI(xbmcgui.WindowXMLDialog):
# restore settings # restore settings
self.set_settings(prev_settings) self.set_settings(prev_settings)
try: try:
self.transmission = self.get_rpc_client() self.transmission = common.get_rpc_client()
except err: except err:
xbmcgui.Dialog().ok(_(2), _(9001)) xbmcgui.Dialog().ok(_(2), _(9001))
self.close() self.close()

57
resources/lib/player.py Normal file
View file

@ -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);

View file

@ -5,4 +5,5 @@
<setting id="rpc_port" type="number" label="1002" default="9091" /> <setting id="rpc_port" type="number" label="1002" default="9091" />
<setting id="rpc_user" type="text" label="1003" default="" /> <setting id="rpc_user" type="text" label="1003" default="" />
<setting id="rpc_password" type="text" option="hidden" label="1004" default="" /> <setting id="rpc_password" type="text" option="hidden" label="1004" default="" />
<setting id="stop_all_on_playback" type="bool" label="1005" default="false" />
</settings> </settings>