Ejemplo n.º 1
0
 def getFileName(filepath):
     filename = filepath
     if ek.ek(os.path.isfile, filename):
         filename = ek.ek(os.path.basename, filename)
     if filename.endswith((".avi", ".wmv", ".mov", ".mp4", ".mpeg", ".mpg", ".mkv")):
         filename = filename.rsplit(".", 1)[0]
     return filename
Ejemplo n.º 2
0
 def hashFile(self, name):
     """This hash function receives the filename and returns the hash code"""
     readsize = 64 * 1024
     with ek.ek(open, name, 'rb') as f:
         size = ek.ek(os.path.getsize, name)
         data = f.read(readsize)
         f.seek(-readsize, os.SEEK_END)
         data += f.read(readsize)
     return hashlib.md5(data).hexdigest()
Ejemplo n.º 3
0
 def list(self, filenames, languages):
     """Main method to call when you want to list subtitles """
     # as self.multi_filename_queries is false, we won't have multiple filenames in the list so pick the only one
     # once multi-filename queries are implemented, set multi_filename_queries to true and manage a list of multiple filenames here
     filepath = filenames[0]
     if ek.ek(os.path.isfile, filepath):
         filehash = self.hashFile(filepath)
         size = ek.ek(os.path.getsize, filepath)
         return self.query(moviehash=filehash, languages=languages, bytesize=size, filepath=filepath)
     else:
         return self.query(languages=languages, filepath=filepath)
Ejemplo n.º 4
0
 def download(self, subtitle):
     """Main method to call when you want to download a subtitle """
     subtitleFilename = subtitle["filename"].rsplit(".", 1)[0] + self.getExtension(subtitle)
     self.downloadFile(subtitle["link"], subtitleFilename + ".gz")
     f = ek.ek(gzip.open, subtitleFilename + ".gz")
     dump = ek.ek(open, subtitleFilename, "wb")
     dump.write(f.read())
     self.adjustPermissions(subtitleFilename)
     dump.close()
     f.close()
     ek.ek(os.remove, subtitleFilename + ".gz")
     return subtitleFilename
Ejemplo n.º 5
0
 def downloadFile(self, url, filename):
     """Downloads the given url to the given filename"""
     req = urllib2.Request(url, headers={'Referer': url, 'User-Agent': 'Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3)'})
     f = urllib2.urlopen(req)
     dump = ek.ek(open, filename, "wb")
     dump.write(f.read())
     dump.close()
     f.close()
Ejemplo n.º 6
0
 def list(self, filenames, languages):
     """Main method to call when you want to list subtitles"""
     # as self.multi_filename_queries is false, we won't have multiple filenames in the list so pick the only one
     # once multi-filename queries are implemented, set multi_filename_queries to true and manage a list of multiple filenames here
     filepath = filenames[0]
     if not ek.ek(os.path.isfile, filepath):
         return []
     return self.query(self.hashFile(filepath), languages)
Ejemplo n.º 7
0
 def __init__(self, config_dict):
     super(BierDopje, self).__init__(self._plugin_languages, config_dict)
     #http://api.bierdopje.com/23459DC262C0A742/GetShowByName/30+Rock
     #http://api.bierdopje.com/23459DC262C0A742/GetAllSubsFor/94/5/1/en (30 rock, season 5, episode 1)
     if not config_dict or not config_dict['cache_dir']:
         raise Exception('Cache directory is mandatory for this plugin')
     self.showid_cache = ek.ek(os.path.join, config_dict['cache_dir'], "bierdopje_showid.cache")
     with self.lock:
         if not ek.ek(os.path.exists, self.showid_cache):
             if not ek.ek(os.path.exists, ek.ek(os.path.dirname, self.showid_cache)):
                 raise Exception("Cache directory doesn't exists")
             f = open(self.showid_cache, 'w')
             pickle.dump({}, f)
             f.close()
         f = open(self.showid_cache, 'r')
         self.showids = pickle.load(f)
         self.logger.debug(u"Reading showids from cache: %s" % self.showids)
         f.close()
Ejemplo n.º 8
0
 def downloadFile(self, url, filename, data=None):
     """Downloads the given url to the given filename"""
     try:
         self.logger.info(u"Downloading %s" % url)
         req = urllib2.Request(url, headers={"Referer": url, "User-Agent": self.user_agent})
         f = urllib2.urlopen(req, data=data)
         dump = ek.ek(open, filename, "wb")
         dump.write(f.read())
         self.adjustPermissions(filename)
         dump.close()
         f.close()
         self.logger.debug(u"Download finished for file %s. Size: %s" % (filename, ek.ek(os.path.getsize, filename)))
     except urllib2.HTTPError, e:
         self.logger.error(u"HTTP Error:", e.code, url)
Ejemplo n.º 9
0
 def hashFile(self, filename):
     """Hash a file like OpenSubtitles"""
     longlongformat = "q"  # long long
     bytesize = struct.calcsize(longlongformat)
     f = ek.ek(open, filename, "rb")
     filesize = ek.ek(os.path.getsize, filename)
     hash = filesize
     if filesize < 65536 * 2:
         self.logger.error(u"File %s is too small (SizeError < 2**16)" % filename)
         return []
     for x in range(65536 / bytesize):
         buffer = f.read(bytesize)
         (l_value,) = struct.unpack(longlongformat, buffer)
         hash += l_value
         hash = hash & 0xFFFFFFFFFFFFFFFF  # to remain as 64bit number
     f.seek(max(0, filesize - 65536), 0)
     for x in range(65536 / bytesize):
         buffer = f.read(bytesize)
         (l_value,) = struct.unpack(longlongformat, buffer)
         hash += l_value
         hash = hash & 0xFFFFFFFFFFFFFFFF
     f.close()
     returnedhash = "%016x" % hash
     return returnedhash
Ejemplo n.º 10
0
 def adjustPermissions(self, filepath):
     if self.config_dict and "files_mode" in self.config_dict and self.config_dict["files_mode"] != -1:
         ek.ek(os.chmod, filepath, self.config_dict["files_mode"])
Ejemplo n.º 11
0
 def download(self, subtitle):
     """Main method to call when you want to download a subtitle"""
     subpage = subtitle["page"]
     page = urllib2.urlopen(subpage)
     soup = BeautifulSoup(page)
     dlhref = soup.find("div", {"class": "download"}).find("a")["href"]
     subtitle["link"] = self.site_url + dlhref.split('"')[7]
     format = "zip"
     archivefilename = subtitle["filename"].rsplit(".", 1)[0] + '.' + format
     self.downloadFile(subtitle["link"], archivefilename)
     subtitlefilename = None
     if zipfile.is_zipfile(archivefilename):
         self.logger.debug(u"Unzipping file " + archivefilename)
         zf = zipfile.ZipFile(archivefilename, "r")
         for el in zf.infolist():
             extension = el.orig_filename.rsplit(".", 1)[1]
             if extension in ("srt", "sub", "txt"):
                 subtitlefilename = srtbasefilename + "." + extension
                 outfile = ek.ek(open, subtitlefilename, "wb")
                 outfile.write(zf.read(el.orig_filename))
                 outfile.flush()
                 self.adjustPermissions(subtitlefilename)
                 outfile.close()
             else:
                 self.logger.info(u"File %s does not seem to be valid " % el.orig_filename)
         # Deleting the zip file
         zf.close()
         ek.ek(os.remove, archivefilename)
         return subtitlefilename
     elif archivefilename.endswith('.rar'):
         self.logger.warn(u'Rar is not really supported yet. Trying to call unrar')
         import subprocess
         try:
             args = ['unrar', 'lb', archivefilename]
             output = subprocess.Popen(args, stdout=subprocess.PIPE).communicate()[0]
             for el in output.splitlines():
                 extension = el.rsplit(".", 1)[1]
                 if extension in ("srt", "sub"):
                     args = ['unrar', 'e', archivefilename, el, ek.ek(os.path.dirname, archivefilename)]
                     subprocess.Popen(args)
                     tmpsubtitlefilename = ek.ek(os.path.join, ek.ek(os.path.dirname, archivefilename), el)
                     subtitlefilename = ek.ek(os.path.join, ek.ek(os.path.dirname, archivefilename), srtbasefilename + "." + extension)
                     if ek.ek(os.path.exists, tmpsubtitlefilename):
                         # rename it to match the file
                         ek.ek(os.rename, tmpsubtitlefilename, subtitlefilename)
                         # exit
                     return subtitlefilename
         except OSError, e:
             self.logger.error(u"Execution failed: %s" % e)
             return None