1# -*- coding: utf-8 -*- 2 3# This file is part of periscope. 4# 5# periscope is free software; you can redistribute it and/or modify 6# it under the terms of the GNU Lesser General Public License as published by 7# the Free Software Foundation; either version 2 of the License, or 8# (at your option) any later version. 9# 10# periscope is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU Lesser General Public License for more details. 14# 15# You should have received a copy of the GNU Lesser General Public License 16# along with periscope; if not, write to the Free Software 17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 18 19import os, urllib2, urllib, xml.dom.minidom, logging, traceback 20import ConfigParser 21 22try: 23 import xdg.BaseDirectory as bd 24 is_local = True 25except ImportError: 26 is_local = False 27 28import SubtitleDatabase 29 30SS_LANGUAGES = {"en": "English", 31 "sv": "Swedish", 32 "da": "Danish", 33 "fi":"Finnish", 34 "no": "Norwegian", 35 "fr" : "French", 36 "es" : "Spanish", 37 "is" : "Icelandic"} 38 39class SubtitleSource(SubtitleDatabase.SubtitleDB): 40 url = "http://www.subtitlesource.org/" 41 site_name = "SubtitleSource" 42 43 def __init__(self, config, cache_folder_path): 44 super(SubtitleSource, self).__init__(SS_LANGUAGES) 45 key = config.get("SubtitleSource", "key") # You need to ask for it 46 if not key: 47 log.error("No key in the config file for SubtitleSource") 48 return 49 #http://www.subtitlesource.org/api/KEY/3.0/xmlsearch/Heroes.S03E09.HDTV.XviD-LOL/all/0 50 #http://www.subtitlesource.org/api/KEY/3.0/xmlsearch/heroes/swedish/0 51 52 self.host = "http://www.subtitlesource.org/api/%s/3.0/xmlsearch" %key 53 54 def process(self, filepath, langs): 55 ''' main method to call on the plugin, pass the filename and the wished 56 languages and it will query the subtitles source ''' 57 if not key: 58 log.info("No key in the config file for SubtitleSource : skip") 59 return [] 60 fname = self.getFileName(filepath) 61 try: 62 subs = self.query(fname, langs) 63 if not subs and fname.rfind(".[") > 0: 64 # Try to remove the [VTV] or [EZTV] at the end of the file 65 teamless_filename = fname[0 : fname.rfind(".[")] 66 subs = self.query(teamless_filename, langs) 67 return subs 68 else: 69 return subs 70 except Exception, e: 71 logging.error("Error raised by plugin %s: %s" %(self.__class__.__name__, e)) 72 traceback.print_exc() 73 return [] 74 75 def query(self, token, langs=None): 76 ''' makes a query on subtitlessource and returns info (link, lang) about found subtitles''' 77 logging.debug("local file is : %s " % token) 78 sublinks = [] 79 80 if not langs: # langs is empty of None 81 languages = ["all"] 82 else: # parse each lang to generate the equivalent lang 83 languages = [SS_LANGUAGES[l] for l in langs if l in SS_LANGUAGES.keys()] 84 85 # Get the CD part of this 86 metaData = self.guessFileData(token) 87 multipart = metaData.get('part', None) 88 part = metaData.get('part') 89 if not part : # part will return None if not found using the regex 90 part = 1 91 92 for lang in languages: 93 searchurl = "%s/%s/%s/0" %(self.host, urllib.quote(token), lang) 94 logging.debug("dl'ing %s" %searchurl) 95 page = urllib2.urlopen(searchurl, timeout=5) 96 xmltree = xml.dom.minidom.parse(page) 97 subs = xmltree.getElementsByTagName("sub") 98 99 for sub in subs: 100 sublang = self.getLG(self.getValue(sub, "language")) 101 if langs and not sublang in langs: 102 continue # The language of this sub is not wanted => Skip 103 if multipart and not int(self.getValue(sub, 'cd')) > 1: 104 continue # The subtitle is not a multipart 105 dllink = "http://www.subtitlesource.org/download/text/%s/%s" %(self.getValue(sub, "id"), part) 106 logging.debug("Link added: %s (%s)" %(dllink,sublang)) 107 result = {} 108 result["release"] = self.getValue(sub, "releasename") 109 result["link"] = dllink 110 result["page"] = dllink 111 result["lang"] = sublang 112 releaseMetaData = self.guessFileData(result['release']) 113 teams = set(metaData['teams']) 114 srtTeams = set(releaseMetaData['teams']) 115 logging.debug("Analyzing : %s " % result['release']) 116 logging.debug("local file has : %s " % metaData['teams']) 117 logging.debug("remote sub has : %s " % releaseMetaData['teams']) 118 #logging.debug("%s in %s ? %s - %s" %(releaseMetaData['teams'], metaData['teams'], teams.issubset(srtTeams), srtTeams.issubset(teams))) 119 if result['release'].startswith(token) or (releaseMetaData['name'] == metaData['name'] and releaseMetaData['type'] == metaData['type'] and (teams.issubset(srtTeams) or srtTeams.issubset(teams))): 120 sublinks.append(result) 121 return sublinks 122 123 124 def createFile(self, subtitle): 125 '''pass the URL of the sub and the file it matches, will unzip it 126 and return the path to the created file''' 127 suburl = subtitle["link"] 128 videofilename = subtitle["filename"] 129 srtfilename = videofilename.rsplit(".", 1)[0] + '.srt' 130 self.downloadFile(suburl, srtfilename) 131 return srtfilename 132 133 def getValue(self, sub, tagName): 134 for node in sub.childNodes: 135 if node.nodeType == node.ELEMENT_NODE and node.tagName == tagName: 136 return node.childNodes[0].nodeValue 137