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