def from_file(file_path): if file_path.find(".") < 0: file_path += ".ini" parsed = WhitespaceFriendlyConfigParser() success = parsed.read(file_path) if len(success) == 0: print("Failed to load config file") return None config = Config() if "data" in parsed: sec = parsed["data"] config.database = sec.get("database", None) if "connection" in parsed: sec = parsed["connection"] config.useragent = sec.get("useragent", None) config.ratelimit = sec.getfloat("ratelimit", 1.0) if "reddit" in parsed: sec = parsed["reddit"] config.subreddit = sec.get("subreddit", None) config.r_username = sec.get("username", None) config.r_password = sec.get("password", None) config.r_oauth_key = sec.get("oauth_key", None) config.r_oauth_secret = sec.get("oauth_secret", None) if "options" in parsed: sec = parsed["options"] config.debug = sec.getboolean("debug", False) from data.models import str_to_showtype config.new_show_types.extend(map(lambda s: str_to_showtype(s.strip()), sec.get("new_show_types", "").split(" "))) config.record_scores = sec.getboolean("record_scores", False) if "options.discovery" in parsed: sec = parsed["options.discovery"] config.discovery_primary_source = sec.get("primary_source", None) config.discovery_secondary_sources = sec.get("secondary_sources", "").split(" ") config.discovery_stream_sources = sec.get("stream_sources", "").split(" ") if "post" in parsed: sec = parsed["post"] config.post_title = sec.get("title", None) config.post_title_postfix_final = sec.get("title_postfix_final", None) config.post_body = sec.get("body", None) for key in sec: if key.startswith("format_") and len(key) > 7: config.post_formats[key[7:]] = sec[key] # Services for key in parsed: if key.startswith("service."): service = key[8:] config.services[service] = parsed[key] return config
def from_file(file_path): config = Config() parsed = WhitespaceFriendlyConfigParser() success = parsed.read(file_path) if len(success) == 0: print("Failed to load config file") return config if "data" in parsed: sec = parsed["data"] config.database = sec.get("database", None) if "connection" in parsed: sec = parsed["connection"] config.useragent = sec.get("useragent", None) config.ratelimit = sec.getfloat("ratelimit", 1.0) if "reddit" in parsed: sec = parsed["reddit"] config.subreddit = sec.get("subreddit", None) config.r_username = sec.get("username", None) config.r_password = sec.get("password", None) config.r_oauth_key = sec.get("oauth_key", None) config.r_oauth_secret = sec.get("oauth_secret", None) #TODO: make dynamic if "service.mal" in parsed: sec = parsed["service.mal"] config.services["mal"] = {"username": sec.get("username", None), "password": sec.get("password", None)} if "service.anidb" in parsed: sec = parsed["service.anidb"] config.services["anidb"] = {"client": sec.get("client", None)} if "service.nyaa" in parsed: sec = parsed["service.nyaa"] config.services["nyaa"] = {"domain": sec.get("domain", None)} if "options" in parsed: sec = parsed["options"] config.debug = sec.getboolean("debug", False) from data.models import str_to_showtype config.new_show_types.extend(map(lambda s: str_to_showtype(s.strip()), sec.get("new_show_types", "").split(","))) if "post" in parsed: sec = parsed["post"] config.post_title = sec.get("title", None) config.post_title_postfix_final = sec.get("title_postfix_final", None) config.post_body = sec.get("body", None) for key in sec: if key.startswith("format_") and len(key) > 7: config.post_formats[key[7:]] = sec[key] return config
def from_file(file_path): config = Config() parsed = configparser.ConfigParser() success = parsed.read(file_path) if len(success) == 0: print("Failed to load config file") return config if "data" in parsed: sec = parsed["data"] config.database = sec.get("database", None) if "connection" in parsed: sec = parsed["connection"] config.useragent = sec.get("useragent", None) config.ratelimit = sec.getfloat("ratelimit", 1.0) if "reddit" in parsed: sec = parsed["reddit"] config.subreddit = sec.get("subreddit", None) config.r_username = sec.get("username", None) config.r_password = sec.get("password", None) config.r_oauth_key = sec.get("oauth_key", None) config.r_oauth_secret = sec.get("oauth_secret", None) #TODO: make dynamic if "service.mal" in parsed: sec = parsed["service.mal"] config.services["mal"] = {"username": sec.get("username", None), "password": sec.get("password", None)} if "service.anidb" in parsed: sec = parsed["service.anidb"] config.services["anidb"] = {"client": sec.get("client", None)} if "service.nyaa" in parsed: sec = parsed["service.nyaa"] config.services["nyaa"] = {"domain": sec.get("domain", None)} if "options" in parsed: sec = parsed["options"] config.debug = sec["debug"] from data.models import str_to_showtype config.new_show_types.extend(map(lambda s: str_to_showtype(s), sec.get("new_show_types", "").split(" "))) if "post" in parsed: sec = parsed["post"] config.post_title = sec.get("title", None) config.post_body = sec.get("body", None) for key in sec: if key.startswith("format_") and len(key) > 7: config.post_formats[key[7:]] = sec[key] return config
def from_file(file_path): config = Config() parsed = configparser.ConfigParser() success = parsed.read(file_path) if len(success) == 0: print("Failed to load config file") return config if "data" in parsed: sec = parsed["data"] config.database = sec.get("database", None) if "connection" in parsed: sec = parsed["connection"] config.useragent = sec.get("useragent", None) config.ratelimit = sec.getfloat("ratelimit", 1.0) if "reddit" in parsed: sec = parsed["reddit"] config.subreddit = sec.get("subreddit", None) config.r_username = sec.get("username", None) config.r_password = sec.get("password", None) config.r_oauth_key = sec.get("oauth_key", None) config.r_oauth_secret = sec.get("oauth_secret", None) # Dynamically load service configuration service_prefix = "service." for section_name in [sec for sec in parsed.sections() if sec.startswith(service_prefix)]: sec = parsed[section_name] service_key = section_name[len(service_prefix):] service_config = config.services[service_key] = {} for key in sec.keys(): service_config[key] = sec.get(key) if "options" in parsed: sec = parsed["options"] config.debug = sec["debug"] from data.models import str_to_showtype config.new_show_types.extend(map(lambda s: str_to_showtype(s), sec.get("new_show_types", "").split(" "))) if "post" in parsed: sec = parsed["post"] config.post_title = sec.get("title", None) config.post_body = sec.get("body", None) for key in sec: if key.startswith("format_") and len(key) > 7: config.post_formats[key[7:]] = sec[key] return config
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