def _check_show_lengths(config, db, update_db=True): shows = db.get_shows(missing_length=True) for show in shows: info("Updating episode count of {} ({})".format(show.name, show.id)) length = None # Check all info handlers for an episode count # Some may not implement get_episode_count and return None for handler in services.get_link_handlers().values(): info(" Checking {} ({})".format(handler.name, handler.key)) # Get show link to site represented by the handler site = db.get_link_site(key=handler.key) link = db.get_link(show, site) if link is None: error("Failed to create link") continue # Validate length new_length = handler.get_episode_count(show, link, useragent=config.useragent) if new_length is not None: debug(" Lists length: {}".format(new_length)) if length is not None and new_length != length: warning(" Conflict between lengths {} and {}".format(new_length, length)) length = new_length # Length found, update database if length is not None: info("New episode count: {}".format(length)) if update_db: db.set_show_episode_count(show, length) else: warning("Debug enabled, not updating database")
def _check_new_episode_scores(config, db, update_db): info("Checking for new episode scores") shows = db.get_shows(enabled=True) for show in shows: latest_episode = db.get_latest_episode(show) if latest_episode is not None: info("For show {} ({}), episode {}".format(show.name, show.id, latest_episode .number)) scores = db.get_episode_scores(show, latest_episode) # Check if any scores have been found rather than checking for each service if len(scores) == 0: for handler in services.get_link_handlers().values(): info(" Checking {} ({})".format(handler.name, handler.key)) # Get show link to site represented by the handler site = db.get_link_site(key=handler.key) link = db.get_link(show, site) if link is None: error("Failed to create link") continue new_score = handler.get_show_score(show, link, useragent=config.useragent) if new_score is not None: info(" Score: {}".format(new_score)) db.add_episode_score(show, latest_episode, site, new_score, commit=False) if update_db: db.commit() else: info(" Already has scores, ignoring")
def _get_primary_source_shows(config): debug("Retrieving primary show list") link_handlers = services.get_link_handlers() service_handlers = services.get_service_handlers() site_key = config.discovery_primary_source if site_key not in link_handlers: warning("Primary source site handler for {} not installed".format(site_key)) return site_handler = link_handlers.get(site_key) shows = [] for raw_show in site_handler.get_seasonal_shows(useragent=config.useragent): if raw_show.show_type is not ShowType.UNKNOWN and raw_show.show_type not in config.new_show_types: debug(" Show isn't an allowed type ({})".format(raw_show.show_type)) debug(" name={}".format(raw_show.name)) continue debug("New show: {}".format(raw_show.name)) d = OrderedDict([ ("title", raw_show.name), ("type", raw_show.show_type.name.lower()), ("has_source", raw_show.has_source), ("info", OrderedDict([(i, "") for i in sorted(link_handlers.keys()) if i in config.discovery_secondary_sources])), ("streams", OrderedDict([(s, "") for s in sorted(service_handlers.keys()) if not service_handlers[s].is_generic and s in config.discovery_stream_sources])) ]) shows.append(d) return shows
def _get_new_season_shows(config, db): # Only checks link sites because their names are preferred # Names on stream sites are unpredictable and many times in english handlers = services.get_link_handlers() for site in db.get_link_sites(): if site.key not in handlers: warning("Link site handler for {} not installed".format(site.key)) continue handler = handlers.get(site.key) info(" Checking {} ({})".format(handler.name, handler.key)) raw_shows = handler.get_seasonal_shows(useragent=config.useragent) for raw_show in raw_shows: yield raw_show
def main(config, args, extra_args): from logging import debug, info, warning, error, exception # Set things up db = database.living_in(config.database) if not db: error("Cannot continue running without a database") return services.setup_services(config) # Run the requested module try: debug("Running module {}".format(config.module)) if config.module == "setup": info("Setting up database") db.setup_tables() info("Registering services") db.register_services(services.get_service_handlers()) db.register_link_sites(services.get_link_handlers()) db.register_poll_sites(services.get_poll_handlers()) elif config.module == "edit": info("Editing database") import module_edit as m m.main(config, db, *extra_args) elif config.module == "episode": info("Finding new episodes") import module_find_episodes as m m.main(config, db, debug=config.debug) elif config.module == "find": info("Finding new shows") import module_find_shows as m if args.output[0] == "db": m.main(config, db, False) elif args.output[0] == "yaml": f = extra_args[0] if len(extra_args) > 0 else "find_output.yaml" m.main(config, db, True, output_file=f) elif config.module == "update": info("Updating shows") import module_update_shows as m m.main(config, db) else: warning("This should never happen or you broke it!") except: exception("Unknown exception or error") db.rollback() db.close()
def main(config, args, extra_args): from logging import debug, info, warning, error, exception # Set things up db = database.living_in(config.database) if not db: error("Cannot continue running without a database") return services.setup_services(config) # Run the requested module try: debug("Running module {}".format(config.module)) if config.module == "setup": info("Setting up database") db.setup_tables() info("Registering services") db.register_services(services.get_service_handlers()) db.register_link_sites(services.get_link_handlers()) elif config.module == "edit": info("Editing database") import module_edit as m m.main(config, db, *extra_args) elif config.module == "episode": info("Finding new episodes") import module_find_episodes as m m.main(config, db, debug=config.debug) elif config.module == "find": info("Finding new shows") import module_find_shows as m if args.output[0] == "db": m.main(config, db, False) elif args.output[0] == "yaml": f = extra_args[0] if len(extra_args) > 0 else "find_output.yaml" m.main(config, db, True, output_file=f) elif config.module == "update": info("Updating shows") import module_update_shows as m m.main(config, db) else: warning("This should never happen or you broke it!") except: exception("Unknown exception or error") db.rollback() db.close()
def _check_show_lengths(config, db, update_db=True): info("Checking show lengths") shows = db.get_shows(missing_length=True) for show in shows: info("Updating episode count of {} ({})".format(show.name, show.id)) length = None # Check all info handlers for an episode count # Some may not implement get_episode_count and return None for handler in services.get_link_handlers().values(): info(" Checking {} ({})".format(handler.name, handler.key)) # Get show link to site represented by the handler site = db.get_link_site(key=handler.key) link = db.get_link(show, site) if link is None: error("Failed to create link") continue # Validate length new_length = handler.get_episode_count(show, link, useragent=config.useragent) if new_length is not None: debug(" Lists length: {}".format(new_length)) if length is not None and new_length != length: warning(" Conflict between lengths {} and {}".format( new_length, length)) length = new_length # Length found, update database if length is not None: info("New episode count: {}".format(length)) if update_db: db.set_show_episode_count(show, length) else: warning("Debug enabled, not updating database")
def _check_new_episode_scores(config, db, update_db): info("Checking for new episode scores") shows = db.get_shows(enabled=True) for show in shows: latest_episode = db.get_latest_episode(show) if latest_episode is not None: info("For show {} ({}), episode {}".format(show.name, show.id, latest_episode.number)) scores = db.get_episode_scores(show, latest_episode) # Check if any scores have been found rather than checking for each service if len(scores) == 0: for handler in services.get_link_handlers().values(): info(" Checking {} ({})".format(handler.name, handler.key)) # Get show link to site represented by the handler site = db.get_link_site(key=handler.key) link = db.get_link(show, site) if link is None: error("Failed to create link") continue new_score = handler.get_show_score( show, link, useragent=config.useragent) if new_score is not None: info(" Score: {}".format(new_score)) db.add_episode_score(show, latest_episode, site, new_score, commit=False) if update_db: db.commit() else: info(" Already has scores, ignoring")
def _get_primary_source_shows(config): debug("Retrieving primary show list") link_handlers = services.get_link_handlers() service_handlers = services.get_service_handlers() site_key = config.discovery_primary_source if site_key not in link_handlers: warning("Primary source site handler for {} not installed".format( site_key)) return site_handler = link_handlers.get(site_key) shows = [] for raw_show in site_handler.get_seasonal_shows( useragent=config.useragent): if raw_show.show_type is not ShowType.UNKNOWN and raw_show.show_type not in config.new_show_types: debug(" Show isn't an allowed type ({})".format( raw_show.show_type)) debug(" name={}".format(raw_show.name)) continue debug("New show: {}".format(raw_show.name)) d = OrderedDict([ ("title", raw_show.name), ("type", raw_show.show_type.name.lower()), ("has_source", raw_show.has_source), ("info", OrderedDict([(i, "") for i in sorted(link_handlers.keys()) if i in config.discovery_secondary_sources])), ("streams", OrderedDict([(s, "") for s in sorted(service_handlers.keys()) if not service_handlers[s].is_generic and s in config.discovery_stream_sources])) ]) shows.append(d) return shows