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 main(): listener = events.listener('download-subtitle', 'list-subtitles') while True: event = yield from listener.wait() if event is None: break if event.type == 'list-subtitles': subtitles = yield from from_opensubtitles(event.path) languages = sorted(set(s.lang for s in subtitles)) log.info("Available languages for {}: {}", event.path, languages) yield from events.broadcast('available-subtitles', path=event.path, languages=languages) else: result = yield from download_subtitles(event.path, event.language) if result: yield from events.broadcast('subtitle-downloaded', path=str(result), video_path=event.path, language=event.language)