def _gen_text_links(db, formats, show): debug("Generating stream text for show {}".format(show)) links = db.get_links(show=show) link_texts = list() for link in links: site = db.get_link_site(id=link.site) if site.enabled: link_handler = services.get_link_handler(site) text = safe_format(formats["link"], site_name=site.name, link=link_handler.get_link(link)) link_texts.append(text) return "\n".join(link_texts)
def match_show_streams(config, db, update_db=True): info("Matching streams to shows") streams = db.get_streams(unmatched=True) if len(streams) == 0: debug(" No unmatched streams") return # Check each link site for site in db.get_link_sites(): debug(" Checking service: {}".format(site.key)) handler = services.get_link_handler(site) # Check remaining streams for stream in list( streams): # Iterate over copy of stream list allow removals debug(" Checking stream: {}".format(stream.name)) raw_shows = handler.find_show(stream.name, useragent=config.useragent) if len(raw_shows) == 1: # Show info found raw_show = raw_shows.pop() debug(" Found show: {}".format(raw_show.name)) # Search stored names for show matches shows = db.search_show_ids_by_names(raw_show.name, *raw_show.more_names) if len(shows) == 1: # All the planets are aligned # Connect the stream and show and save the used name show_id = shows.pop() if update_db: db.update_stream(stream, show=show_id, active=True) db.add_show_names(stream.name, id=show_id, commit=False) streams.remove(stream) elif len(shows) == 0: warning(" No shows known") else: warning(" Multiple shows known") elif len(raw_shows) == 0: warning(" No shows found") else: warning(" Multiple shows found") if update_db: db.commit()
def match_show_streams(config, db, update_db=True): info("Matching streams to shows") streams = db.get_streams(unmatched=True) if len(streams) == 0: debug(" No unmatched streams") return # Check each link site for site in db.get_link_sites(): debug(" Checking service: {}".format(site.key)) handler = services.get_link_handler(site) # Check remaining streams for stream in list(streams): # Iterate over copy of stream list allow removals debug(" Checking stream: {}".format(stream.name)) raw_shows = handler.find_show(stream.name, useragent=config.useragent) if len(raw_shows) == 1: # Show info found raw_show = raw_shows.pop() debug(" Found show: {}".format(raw_show.name)) # Search stored names for show matches shows = db.search_show_ids_by_names(raw_show.name, *raw_show.more_names) if len(shows) == 1: # All the planets are aligned # Connect the stream and show and save the used name show_id = shows.pop() if update_db: db.update_stream(stream, show=show_id, active=True) db.add_show_names(stream.name, id=show_id, commit=False) streams.remove(stream) elif len(shows) == 0: warning(" No shows known") else: warning(" Multiple shows known") elif len(raw_shows) == 0: warning(" No shows found") else: warning(" Multiple shows found") if update_db: db.commit()
def _gen_text_links(db, formats, show): debug("Generating stream text for show {}".format(show)) links = db.get_links(show=show) link_texts = list() link_texts_bottom = list() # for links that come last, e.g. official and subreddit for link in links: site = db.get_link_site(id=link.site) if site.enabled: link_handler = services.get_link_handler(site) if site.key == "subreddit": text = safe_format(formats["link_reddit"], link=link_handler.get_link(link)) else: text = safe_format(formats["link"], site_name=site.name, link=link_handler.get_link(link)) if site.key == "subreddit" or site.key == "official": link_texts_bottom.append(text) else: link_texts.append(text) return "\n".join(link_texts) + '\n' + '\n'.join(link_texts_bottom)
def _edit_with_file(db, edit_file): import yaml info("Parsing show edit file \"{}\"".format(edit_file)) try: with open(edit_file, "r", encoding="UTF-8") as f: parsed = list(yaml.full_load_all(f)) except yaml.YAMLError: exception("Failed to parse edit file") return debug(" num shows={}".format(len(parsed))) for doc in parsed: name = doc["title"] stype = str_to_showtype(doc.get("type", "tv")) # convert to enum? length = doc.get("length", 0) has_source = doc.get("has_source", False) is_nsfw = doc.get("is_nsfw", False) info("Adding show \"{}\" ({})".format(name, stype)) debug(" has_source={}".format(has_source)) debug(" is_nsfw={}".format(is_nsfw)) if stype == ShowType.UNKNOWN: error("Invalid show type \"{}\"".format(stype)) return False show = UnprocessedShow(None, None, name, [], stype, length, has_source, is_nsfw) found_ids = db.search_show_ids_by_names(name, exact=True) debug("Found ids: {found_ids}") if len(found_ids) == 0: show_id = db.add_show(show, commit=False) elif len(found_ids) == 1: show_id = found_ids.pop() db.update_show(show_id, show, commit=False) else: error("More than one ID found for show") return False # Info if "info" in doc: infos = doc["info"] for info_key in infos: url = infos[info_key] if not url: continue debug(" Info {}: {}".format(info_key, url)) info_handler = services.get_link_handler(key=info_key) if info_handler: info_id = info_handler.extract_show_id(url) debug(" id={}".format(info_id)) if not db.has_link(info_key, info_id): show.site_key = info_key show.show_key = info_id db.add_link(show, show_id, commit=False) else: error(" Info handler not installed") # Streams if "streams" in doc: streams = doc["streams"] for service_key in streams: url = streams[service_key] if not url: continue remote_offset = 0 try: roi = url.rfind("|") if roi > 0: if roi + 1 < len(url): remote_offset = int(url[roi + 1:]) url = url[:roi] except: exception( "Improperly formatted stream URL \"{}\"".format(url)) continue info(" Stream {}: {}".format(service_key, url)) service_id = service_key.split('|')[0] stream_handler = services.get_service_handler(key=service_id) if stream_handler: show_key = stream_handler.extract_show_key(url) debug(" id={}".format(show_key)) if not db.has_stream(service_id, show_key): s = UnprocessedStream(service_id, show_key, None, "", remote_offset, 0) db.add_stream(s, show_id, commit=False) else: service = db.get_service(key=service_id) s = db.get_stream(service_tuple=(service, show_key)) db.update_stream(s, show_key=show_key, remote_offset=remote_offset, commit=False) elif "|" in service_key: # Lite stream service, service_name = service_key.split("|", maxsplit=1) db.add_lite_stream(show_id, service, service_name, url) else: error(" Stream handler not installed") # Aliases if "alias" in doc: aliases = doc["alias"] for alias in aliases: db.add_alias(show_id, alias) info( f"Added {len(aliases)} alias{'es' if len(aliases) > 1 else ''}" ) return True
def _edit_with_file(db, edit_file): import yaml info("Parsing show edit file \"{}\"".format(edit_file)) try: with open(edit_file, "r", encoding="UTF-8") as f: parsed = list(yaml.load_all(f)) except yaml.YAMLError: exception("Failed to parse edit file") return debug(" num shows={}".format(len(parsed))) for doc in parsed: name = doc["title"] stype = str_to_showtype(doc["type"]) # convert to enum? length = doc["length"] if "length" in doc else 0 has_source = doc["has_source"] info("Adding show \"{}\" ({})".format(name, stype)) debug(" has_source={}".format(has_source)) if stype == ShowType.UNKNOWN: error("Invalid show type \"{}\"".format(stype)) return False show = UnprocessedShow(None, None, name, [], stype, length, has_source) found_ids = db.search_show_ids_by_names(name, exact=True) if len(found_ids) == 0: show_id = db.add_show(show, commit=False) elif len(found_ids) == 1: show_id = found_ids.pop() db.update_show(show_id, show, commit=False) else: error("More than one ID found for show") return False # Info if "info" in doc: infos = doc["info"] for info_key in infos: url = infos[info_key] if not url: continue debug(" Info {}: {}".format(info_key, url)) info_handler = services.get_link_handler(key=info_key) if info_handler: info_id = info_handler.extract_show_id(url) debug(" id={}".format(info_id)) if not db.has_link(info_key, info_id): show.site_key = info_key show.show_key = info_id db.add_link(show, show_id, commit=False) else: error(" Info handler not installed") # Streams if "streams" in doc: streams = doc["streams"] for service_key in streams: url = streams[service_key] if not url: continue remote_offset = 0 try: roi = url.rfind("|") if roi > 0: if roi+1 < len(url): remote_offset = int(url[roi+1:]) url = url[:roi] except: exception("Improperly formatted stream URL \"{}\"".format(url)) continue info(" Stream {}: {}".format(service_key, url)) stream_handler = services.get_service_handler(key=service_key) if stream_handler: show_key = stream_handler.extract_show_key(url) debug(" id={}".format(show_key)) if not db.has_stream(service_key, show_key): s = UnprocessedStream(service_key, show_key, None, "", remote_offset, 0) db.add_stream(s, show_id, commit=False) else: service = db.get_service(key=service_key) s = db.get_stream(service_tuple=(service, show_key)) db.update_stream(s, show=show_id, show_key=show_key, remote_offset=remote_offset, commit=False) else: error(" Stream handler not installed") return True