emission: initial checkin to this repository
[enigma2-plugins.git] / emission / src / plugin.py
1 # -*- coding: utf-8 -*-
2
3 from Plugins.Plugin import PluginDescriptor
4 from Components.config import config, ConfigSubsection, ConfigText, \
5         ConfigNumber, ConfigYesNo, ConfigSelection
6
7 from . import EmissionOverview
8 from EmissionOverview import LIST_TYPE_ALL, SORT_TYPE_ADDED
9
10 config.plugins.emission = ConfigSubsection()
11 config.plugins.emission.hostname = ConfigText(default = "localhost", fixed_size = False)
12 config.plugins.emission.username = ConfigText(default = "", fixed_size = False)
13 config.plugins.emission.password = ConfigText(default = "", fixed_size = False)
14 config.plugins.emission.port = ConfigNumber(default = 9091)
15 config.plugins.emission.autodownload_from_simplerss = ConfigYesNo(default = False)
16 # XXX: this is not compatible to the previous version and WILL break if not the default was used
17 config.plugins.emission.last_tab = ConfigNumber(default = LIST_TYPE_ALL)
18 config.plugins.emission.last_sort = ConfigNumber(default = SORT_TYPE_ADDED)
19
20 # by default sockets (and therefore urllib2 which transmissionrpc uses)
21 # block so we set a default timeout of 10s for all sockets...
22 # not nice, but does its job :-)
23 import socket
24 socket.setdefaulttimeout(10)
25
26 from transmissionrpc import Client, TransmissionError
27
28 NOTIFICATIONID = 'EmissionAutodownloadError'
29
30 def simplerss_update_callback(id = None):
31         try:
32                 from Plugins.Extensions.SimpleRSS.plugin import rssPoller
33         except ImportError:
34                 pass # should not happen since the poller has to be active for us to be called :-)
35         else:
36                 errors = 0
37                 # we only check the "new items" feed currently since we do not keep track of the files we downloaded
38                 if id is None:
39                         client = None
40                         for item in rssPoller.newItemFeed.history:
41                                 for file in item[3]:
42                                         if file.mimetype == "application/x-bittorrent":
43                                                 if client is None:
44                                                         client = Client(
45                                                                 address = config.plugins.emission.hostname.value,
46                                                                 port = config.plugins.emission.port.value,
47                                                                 user = config.plugins.emission.username.value,
48                                                                 password = config.plugins.emission.password.value
49                                                         )
50                                                 try:
51                                                         # XXX: we might want to run this in the background cause this might block...
52                                                         client.add_url(file.path)
53                                                 except TransmissionError:
54                                                         errors += 1
55
56                 # Inform the user if an error occured
57                 # XXX: we could also either open a notification per torrent (success/failure)
58                 #      or create a custom screen which gives this information in a
59                 #      more understandable and less annyoing way (e.g. a list of torrent
60                 #      names with an X or hook symbol next to it...)
61                 if errors > 0:
62                         from Tools.Notifications import AddPopup
63                         from Screens.MessageBox import MessageBox
64
65                         AddPopup(
66                                 _("Failed to add %d torrents.") % (errors),
67                                 MessageBox.TYPE_WARNING,
68                                 5,
69                                 NOTIFICATIONID
70                         )
71
72 def simplerss_handle_callback(el):
73         try:
74                 from Plugins.Extensions.SimpleRSS.RSSPoller import update_callbacks
75         except ImportError:
76                 # Notify the user about a too old or missing SimpleRSS instalation when
77                 # enabling this feature
78                 if not el.value:
79                         return
80
81                 from Tools.Notifications import AddPopup
82                 from Screens.MessageBox import MessageBox
83                 try:
84                         import Plugins.Extensions.SimpleRSS.plugin
85                         AddPopup(
86                                 _("Your version if SimpleRSS is too old to support this feature.\nIf possible update your SimpleRSS installation."),
87                                 MessageBox.TYPE_ERROR,
88                                 5
89                         )
90                 except ImportError:
91                         AddPopup(
92                                 _("This feature requires SimpleRSS to be installed.\nYou can do so with the Plugin Installer, see the User Manual for further explanation."),
93                                 MessageBox.TYPE_ERROR,
94                                 5
95                         )
96
97         else:
98                 if el.value:
99                         if simplerss_update_callback not in update_callbacks:
100                                 update_callbacks.append(simplerss_update_callback)
101                 elif simplerss_update_callback in update_callbacks:
102                         update_callbacks.remove(simplerss_update_callback)
103
104 config.plugins.emission.autodownload_from_simplerss.addNotifier(simplerss_handle_callback, immediate_feedback = False)
105
106 def main(session, **kwargs):
107         #reload(EmissionOverview)
108         session.open(
109                 EmissionOverview.EmissionOverview
110         )
111
112 def filescan_open(item, session, **kwargs):
113         client = Client(
114                 address = config.plugins.emission.hostname.value,
115                 port = config.plugins.emission.port.value,
116                 user = config.plugins.emission.username.value,
117                 password = config.plugins.emission.password.value
118         )
119
120         added = 0
121         errors = 0
122
123         # XXX: a list of failed/added torrents would be nice, see comment in simplerss_update_callback
124         for each in item:
125                 try:
126                         if client.add_url(each):
127                                 added += 1
128                 except TransmissionError:
129                         errors += 1
130
131         from Screens.MessageBox import MessageBox
132
133         session.open(
134                 MessageBox,
135                 _("%d Torrents(s) were scheduled for download, %d failed.") % (added, errors),
136                 type = MessageBox.TYPE_INFO,
137                 timeout = 5
138         )
139
140 from mimetypes import add_type
141 add_type("application/x-bittorrent", ".tor")
142 add_type("application/x-bittorrent", ".torrent")
143
144 def filescan(**kwargs):
145         from Components.Scanner import Scanner, ScanPath
146
147         return [
148                 Scanner(
149                         mimetypes = ("application/x-bittorrent",),
150                         paths_to_scan =
151                                 (
152                                         ScanPath(path = "", with_subdirs = False),
153                                 ),
154                         name = "BitTorrrent Download",
155                         description = _("Download torrent..."),
156                         openfnc = filescan_open,
157                 )
158         ]
159
160 def Plugins(**kwargs):
161         return [
162                 PluginDescriptor(name = "eMission", description = _("enigma2 frontend to transmission-daemon"), where = PluginDescriptor.WHERE_PLUGINMENU, fnc = main, needsRestart = False),
163                 PluginDescriptor(name = "eMission...", where = PluginDescriptor.WHERE_EXTENSIONSMENU, fnc = main, needsRestart = False),
164                 PluginDescriptor(where = PluginDescriptor.WHERE_FILESCAN, fnc = filescan, needsRestart = False),
165         ]
166