cleanups
[enigma2-plugins.git] / mediadownloader / src / plugin.py
1 #
2 # To be used as simple Downloading Application by other Plugins
3 #
4
5 from Components.config import config, ConfigSubsection, ConfigLocations
6 from Tools.Directories import resolveFilename, SCOPE_HDD
7
8 # SCOPE_HDD is not really what we want but the best we can get :-)
9 config.plugins.mediadownloader = ConfigSubsection()
10 config.plugins.mediadownloader.bookmarks = ConfigLocations(default = [resolveFilename(SCOPE_HDD)])
11
12 # TODO: support custom bookmark element?
13
14 # Download a single File
15 def download_file(session, url, to = None, askOpen = False, callback = None, \
16         **kwargs):
17         """Provides a simple downloader Application"""
18
19         from Components.Scanner import ScanFile
20         file = ScanFile(url, autodetect = False)
21
22         from MediaDownloader import MediaDownloader
23         session.open(MediaDownloader, file, askOpen, to, callback)
24
25 # Item chosen
26 def filescan_chosen(session, item):
27         if item:
28                 from MediaDownloader import MediaDownloader
29
30                 session.open(MediaDownloader, item[1], askOpen = True)
31
32 # Open as FileScanner
33 def filescan_open(items, session, **kwargs):
34         """Download a file from a given List"""
35
36         Len = len(items)
37         if Len > 1:
38                 from Screens.ChoiceBox import ChoiceBox
39                 from Tools.BoundFunction import boundFunction
40
41                 # Create human-readable filenames
42                 choices = [
43                         (
44                                 item.path[item.path.rfind("/")+1:].replace('%20', ' ').\
45                                         replace('%5F', '_').replace('%2D', '-'),
46                                 item
47                         )
48                                 for item in items
49                 ]
50
51                 # And let the user choose one
52                 session.openWithCallback(
53                         boundFunction(filescan_chosen, session),
54                         ChoiceBox,
55                         _("Which file do you want to download?"),
56                         choices
57                 )
58         elif Len:
59                 from MediaDownloader import MediaDownloader
60
61                 session.open(MediaDownloader, items[0], askOpen = True)
62
63 # Return Scanner provided by this Plugin
64 def filescan(**kwargs):
65         from Components.Scanner import Scanner, ScanPath
66
67         # Overwrite checkFile to detect remote files
68         class RemoteScanner(Scanner):
69                 def checkFile(self, file):
70                         return file.path.startswith(("http://", "https://", "ftp://"))
71
72         return [
73                 RemoteScanner(
74                         mimetypes = None,
75                         paths_to_scan =
76                                 [
77                                         ScanPath(path = "", with_subdirs = False),
78                                 ],
79                         name = "Download",
80                         description = "Download...",
81                         openfnc = filescan_open,
82                 )
83         ]
84
85 def Plugins(**kwargs):
86         from Plugins.Plugin import PluginDescriptor
87
88         return [
89                 PluginDescriptor(
90                         name="MediaDownloader",
91                         where = PluginDescriptor.WHERE_FILESCAN,
92                         fnc = filescan
93                 )
94         ]