def get_language(path): from aesop import isocodes for suffix in path.suffixes: suffix = suffix[1:] try: isoname = isocodes.isoname(suffix.title()) except KeyError: pass else: return isoname if len(suffix) not in {2, 3}: continue suffix = suffix.lower() if len(suffix) == 2: try: suffix = isocodes.iso2to3(suffix) except KeyError: continue try: isocodes.nicename(suffix) except KeyError: pass else: return suffix
def event_listener(self): listener = events.listener("new-client", "subtitle-downloaded", "available-subtitles") while True: event = yield from listener.wait() if event is None: break if event.type == "subtitle-downloaded": if self.player.client.path == event.video_path: log.debug("Adding downloaded {} as {}", event.path, event.language) self.player.subtitle_downloads.pop(event.language) self.player.load_srt_subtitle(event.path, event.language) elif event.type == "new-client": asyncio.async(self.player.broadcast_all_properties()) elif event.type == "available-subtitles": current_languages = set(s.get("lang", "Unknown Language") for s in self.player.subtitles()) for lang in event.languages: if lang in current_languages: continue nicename = isocodes.nicename(lang) if isocodes.exists(lang) else "{} (Unknown)".format(lang) self.player.subtitle_downloads[lang] = "{} (Download)".format(nicename) asyncio.async(self.player.broadcast_available_subtitles())
def broadcast_available_audio(self): audio_streams = [ dict(value=str(track["id"]), display=isocodes.nicename(track.get("lang", "unk"))) for track in self.client.audio_tracks() ] if len(audio_streams) <= 1: audio_streams = None else: audio_streams = sorted(audio_streams, key=itemgetter("display")) yield from broadcast_player_property("available_audio", audio_streams)
def download_subtitles(path, language): nicename = isocodes.nicename(language) suffix = '.{}.srt'.format(nicename) subpath = pathlib.Path(path).with_suffix(suffix) if subpath.is_file(): log.info("{} already exists, not downloading", subpath) return subtitles = yield from from_opensubtitles(path, requested_language=language) for subtitle in subtitles: log.info("Attempting to download {}", subtitle.url) resp = yield from aiohttp.request('GET', subtitle.url) if resp.status != 200: log.info("{} for {}, ignoring", resp.status, subtitle.url) continue try: data = yield from resp.read_and_close() except Exception: log.exception("Error downloading {}", subtitle.url) continue try: data = gzip.decompress(data) except Exception: log.exception("Error decompressing {}", subtitle.url) continue # FIXME: If any subtitles already exist, calculate hash and compare to # what was stated - if it's the same, skip it as we're going to replace # with the next subtitle. try: with subpath.open('wb') as f: f.write(data) except Exception: log.exception("Could not write to {}", path) continue log.info("Downloaded {} subtitle for {} to {}", nicename, path, subpath) return subpath
def broadcast_available_subtitles(self): subtitles = [ dict(value=str(track["id"]), display=isocodes.nicename(track.get("lang", "unk"))) for track in self.client.subtitles() ] if self.subtitle_downloads: subtitles.extend( [ dict(value="download_{}".format(slang), display=nicename) for (slang, nicename) in self.subtitle_downloads.items() ] ) if not subtitles: subtitles = None else: subtitles = sorted(subtitles, key=itemgetter("display")) yield from broadcast_player_property("available_subtitles", subtitles)