-
Notifications
You must be signed in to change notification settings - Fork 0
/
periscope.py
228 lines (194 loc) · 8.79 KB
/
periscope.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/usr/bin/python
# -*- coding: utf-8 -*-
# This file is part of periscope.
# Copyright (c) 2008-2011 Patrick Dessalle <patrick@dessalle.be>
#
# periscope is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# periscope is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with periscope; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import getopt
import sys
import os
import threading
import logging
from Queue import Queue
import traceback
import ConfigParser
log = logging.getLogger(__name__)
try:
import xdg.BaseDirectory as bd
is_local = True
except ImportError:
is_local = False
import plugins
import version
import locale
SUPPORTED_FORMATS = 'video/x-msvideo', 'video/quicktime', 'video/x-matroska', 'video/mp4'
VERSION = version.VERSION
class Periscope:
''' Main Periscope class'''
def __init__(self, cache_folder=None):
self.config = ConfigParser.SafeConfigParser({"lang": "", "plugins" : "" })
self.config_file = os.path.join(cache_folder, "config")
self.cache_path = cache_folder
if not os.path.exists(self.config_file):
folder = os.path.dirname(self.config_file)
if not os.path.exists(folder):
log.info("Creating folder %s" %folder)
os.mkdir(folder)
log.info("Creating config file")
configfile = open(self.config_file, "w")
self.config.write(configfile)
configfile.close()
else:
#Load it
self.config.read(self.config_file)
self.pluginNames = self.get_preferedPlugins()
self._preferedLanguages = None
def get_preferedLanguages(self):
''' Get the prefered language from the config file '''
configLang = self.config.get("DEFAULT", "lang")
log.info("lang read from config: " + configLang)
if configLang == "":
try :
l = [locale.getdefaultlocale()[0][:2]]
except :
return None
else:
return map(lambda x : x.strip(), configLang.split(","))
def set_preferedLanguages(self, langs):
''' Update the config file to set the prefered language '''
self.config.set("DEFAULT", "lang", ",".join(langs))
configfile = open(self.config_file, "w")
self.config.write(configfile)
configfile.close()
def get_preferedPlugins(self):
''' Get the prefered plugins from the config file '''
configPlugins = self.config.get("DEFAULT", "plugins")
if not configPlugins or configPlugins.strip() == "":
return self.listExistingPlugins()
else :
log.info("plugins read from config : " + configPlugins)
return map(lambda x : x.strip(), configPlugins.split(","))
def set_preferedPlugins(self, newPlugins):
''' Update the config file to set the prefered plugins) '''
self.config.set("DEFAULT", "plugins", ",".join(newPlugins))
configfile = open(self.config_file, "w")
self.config.write(configfile)
configfile.close()
# Getter/setter for the property preferedLanguages
preferedLanguages = property(get_preferedLanguages, set_preferedLanguages)
preferedPlugins = property(get_preferedPlugins, set_preferedPlugins)
def deactivatePlugin(self, pluginName):
''' Remove a plugin from the list '''
self.pluginNames -= pluginName
self.set_preferedPlugins(self.pluginNames)
def activatePlugin(self, pluginName):
''' Activate a plugin '''
if pluginName not in self.listExistingPlugins():
raise ImportError("No plugin with the name %s exists" %pluginName)
self.pluginNames += pluginName
self.set_preferedPlugins(self.pluginNames)
def listActivePlugins(self):
''' Return all active plugins '''
return self.pluginNames
def listExistingPlugins(self):
''' List all possible plugins from the plugin folder '''
return map(lambda x : x.__name__, plugins.SubtitleDatabase.SubtitleDB.__subclasses__())
def listSubtitles(self, filename, langs=None):
'''Searches subtitles within the active plugins and returns all found matching subtitles ordered by language then by plugin.'''
#if not os.path.isfile(filename):
#raise InvalidFileException(filename, "does not exist")
log.info("Searching subtitles for %s with langs %s" %(filename, langs))
subtitles = []
q = Queue()
for name in self.pluginNames:
try :
plugin = getattr(plugins, name)(self.config, self.cache_path)
log.info("Searching on %s " %plugin.__class__.__name__)
thread = threading.Thread(target=plugin.searchInThread, args=(q, filename, langs))
thread.start()
except ImportError :
log.error("Plugin %s is not a valid plugin name. Skipping it.")
# Get data from the queue and wait till we have a result
for name in self.pluginNames:
subs = q.get(True)
if subs and len(subs) > 0:
if not langs:
subtitles += subs
else:
for sub in subs:
if sub["lang"] in langs:
subtitles += [sub] # Add an array with just that sub
if len(subtitles) == 0:
return []
return subtitles
def selectBestSubtitle(self, subtitles, langs=None):
'''Searches subtitles from plugins and returns the best subtitles from all candidates'''
if not subtitles:
return None
if not langs: # No preferred language => return the first
return subtitles[0]
subtitles = self.__orderSubtitles__(subtitles)
for l in langs:
if subtitles.has_key(l) and len(subtitles[l]):
return subtitles[l][0]
return None #Could not find subtitles
def downloadSubtitle(self, filename, langs=None):
''' Takes a filename and a language and creates ONE subtitle through plugins'''
subtitles = self.listSubtitles(filename, langs)
if subtitles:
log.debug("All subtitles: ")
log.debug(subtitles)
return self.attemptDownloadSubtitle(subtitles, langs)
else:
return None
def attemptDownloadSubtitle(self, subtitles, langs):
subtitle = self.selectBestSubtitle(subtitles, langs)
if subtitle:
log.info("Trying to download subtitle: %s" %subtitle['link'])
#Download the subtitle
try:
subpath = subtitle["plugin"].createFile(subtitle)
if subpath:
subtitle["subtitlepath"] = subpath
return subtitle
else:
# throw exception to remove it
raise Exception("Not downloaded")
except Exception as inst:
# Could not download that subtitle, remove it
log.warn("Subtitle %s could not be downloaded, trying the next on the list" %subtitle['link'])
log.error(inst)
subtitles.remove(subtitle)
return self.attemptDownloadSubtitle(subtitles, langs)
else :
log.error("No subtitles could be chosen.")
return None
def guessFileData(self, filename):
subdb = plugins.SubtitleDatabase.SubtitleDB(None)
return subdb.guessFileData(filename)
def __orderSubtitles__(self, subs):
'''reorders the subtitles according to the languages then the website'''
try:
from collections import defaultdict
subtitles = defaultdict(list) #Order matters (order of plugin and result from plugins)
for s in subs:
subtitles[s["lang"]].append(s)
return subtitles
except ImportError, e: #Don't use Python 2.5
subtitles = {}
for s in subs:
# return subtitles[s["lang"]], if it does not exist, set it to [] and return it, then append the subtitle
subtitles.setdefault(s["lang"], []).append(s)
return subtitles