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