def series_modal(request): content_discovery = ContentDiscovery() content_manager = ContentManager() report_modal = request.GET.get("report_modal", False) # Get the ID from the URL tmdb_id = request.GET.get("tmdb_id", None) tvdb_id = request.GET.get("tvdb_id", None) # Determine the TVDB ID if tvdb_id: pass elif tmdb_id: tvdb_id = content_discovery.get_external_ids(tmdb_id, "tv")["tvdb_id"] # Check if the show is already within Sonarr's collection requested_show = content_manager.get(tvdb_id=tvdb_id) # If it doesn't already exists, add then add it if requested_show is None: sonarr_params = obtain_sonarr_parameters(content_discovery, content_manager, tmdb_id, tvdb_id) requested_show = content_manager.add( tvdb_id=tvdb_id, quality_profile_id=sonarr_params["sonarr_profile_id"], root_dir=sonarr_params["sonarr_root"], series_type=sonarr_params["series_type"], season_folders=sonarr_params["season_folders"], ) # Keep refreshing until we get the series from Sonarr series = content_manager.get(tvdb_id=tvdb_id, obtain_season_info=True, force_update_cache=True) if series is None: series_fetch_retries = 0 while series is None: if series_fetch_retries > MAX_SERIES_FETCH_RETRIES: break series_fetch_retries = series_fetch_retries + 1 series = content_manager.get(tvdb_id=tvdb_id, obtain_season_info=True, force_update_cache=True) log.handler( "Sonarr did not have the series information! Conreq is waiting...", log.INFO, __logger, ) context = generate_context({ "seasons": series["seasons"], "tvdb_id": tvdb_id, "report_modal": report_modal }) template = loader.get_template("modal/series_selection.html") return HttpResponse(template.render(context, request))
def series_modal(user, tmdb_id=None, tvdb_id=None): # Validate login status if user.is_authenticated: content_discovery = ContentDiscovery() content_manager = ContentManager() # Determine the TVDB ID if tvdb_id is not None: pass elif tmdb_id is not None: tvdb_id = content_discovery.get_external_ids(tmdb_id, "tv")["tvdb_id"] # Check if the show is already within Sonarr's collection requested_show = content_manager.get(tvdb_id=tvdb_id) # If it doesn't already exists, add then add it if requested_show is None: sonarr_params = obtain_sonarr_parameters(content_discovery, content_manager, tmdb_id, tvdb_id) requested_show = content_manager.add( tvdb_id=tvdb_id, quality_profile_id=sonarr_params["sonarr_profile_id"], root_dir=sonarr_params["sonarr_root"], series_type=sonarr_params["series_type"], season_folders=sonarr_params["season_folders"], ) # Keep refreshing until we get the series from Sonarr series = content_manager.get(tvdb_id=tvdb_id, obtain_season_info=True) if series is None: series_fetch_retries = 0 while series is None: if series_fetch_retries > MAX_SERIES_FETCH_RETRIES: break series_fetch_retries = series_fetch_retries + 1 sleep(0.5) series = content_manager.get(tvdb_id=tvdb_id, obtain_season_info=True, force_update_cache=True) log.handler( "Retrying content fetch!", log.INFO, __logger, ) context = generate_context({"seasons": series["seasons"]}) return render_to_string("modal/series_selection.html", context)
def set_single_availability(card): """Sets the availabily on a single card.""" content_manager = ContentManager() content_discovery = ContentDiscovery() try: # Compute the availability of a Sonarr card if card.__contains__("tvdbId"): content = content_manager.get(tvdb_id=card["tvdbId"]) if content is not None: card["availability"] = content["availability"] # Compute the availability of a Radarr card elif card.__contains__("tmdbId"): content = content_manager.get(tmdb_id=card["tmdbId"]) if content is not None: card["availability"] = content["availability"] # Compute the availability of TV show elif card.__contains__("name"): external_id = content_discovery.get_external_ids(card["id"], "tv") content = content_manager.get(tvdb_id=external_id["tvdb_id"]) if content is not None: card["availability"] = content["availability"] # Compute the availability of movie elif card.__contains__("title"): content = content_manager.get(tmdb_id=card["id"]) if content is not None: card["availability"] = content["availability"] # Something unexpected was passed in else: log.handler( "Card did not contain content_type, title, or name!", log.WARNING, _logger, ) return card except: log.handler( "Could not determine the availability of card!\n" + str(card), log.ERROR, _logger, ) return card
def set_single_conreq_status(card): content_manager = ContentManager() content_discovery = ContentDiscovery() try: # Compute conreq status of a Sonarr card if card.__contains__("tvdbId"): content = content_manager.get(tvdb_id=card["tvdbId"]) if content is not None: card["conreqStatus"] = content["conreqStatus"] # Compute conreq status of a Radarr card elif card.__contains__("tmdbId"): content = content_manager.get(tmdb_id=card["tmdbId"]) if content is not None: card["conreqStatus"] = content["conreqStatus"] # Compute conreq status of TV show elif card.__contains__("name"): external_id = content_discovery.get_external_ids(card["id"], "tv") content = content_manager.get(tvdb_id=external_id["tvdb_id"]) if content is not None: card["conreqStatus"] = content["conreqStatus"] # Compute conreq status of movie elif card.__contains__("title"): content = content_manager.get(tmdb_id=card["id"]) if content is not None: card["conreqStatus"] = content["conreqStatus"] # Something unexpected was passed in else: log.handler( "Card did not contain contentType, title, or name!", log.WARNING, __logger, ) return card except: log.handler( "Could not determine Conreq Status of card!\n" + card, log.ERROR, __logger, ) return card
def request_content(request): # User submitted a new request if request.method == "POST": request_parameters = json.loads(request.body.decode("utf-8")) log.handler( "Request received: " + str(request_parameters), log.INFO, __logger, ) content_manager = ContentManager() content_discovery = ContentDiscovery() # TV show was requested if request_parameters["content_type"] == "tv": # Obtain the TVDB ID if needed tvdb_id = None tmdb_id = None if request_parameters.__contains__("tvdb_id"): tvdb_id = request_parameters["tvdb_id"] if request_parameters.__contains__("tmdb_id"): tmdb_id = request_parameters["tmdb_id"] if not tvdb_id and tmdb_id: tvdb_id = content_discovery.get_external_ids(tmdb_id, "tv")["tvdb_id"] # Request the show by the TVDB ID if tvdb_id: # Check if the show is already within Sonarr's collection show = content_manager.get(tvdb_id=tvdb_id) # If it doesn't already exists, add then request it if show is None: sonarr_params = obtain_sonarr_parameters( content_discovery, content_manager, tmdb_id, tvdb_id) show = content_manager.add( tvdb_id=tvdb_id, quality_profile_id=sonarr_params["sonarr_profile_id"], root_dir=sonarr_params["sonarr_root"], series_type=sonarr_params["series_type"], season_folders=sonarr_params["season_folders"], ) # Save and request if tmdb_id: add_unique_to_db( UserRequest, content_id=tmdb_id, source="tmdb", content_type="tv", requested_by=request.user, ) else: add_unique_to_db( UserRequest, content_id=tvdb_id, source="tvdb", content_type="tv", requested_by=request.user, ) content_manager.request( sonarr_id=show["id"], seasons=request_parameters["seasons"], episode_ids=request_parameters["episode_ids"], ) log.handler( request.user.username + " requested TV series " + show["title"], log.INFO, __logger, ) # Movie was requested elif request_parameters["content_type"] == "movie": tmdb_id = request_parameters["tmdb_id"] radarr_params = obtain_radarr_parameters(content_discovery, content_manager, tmdb_id) # Check if the movie is already within Radarr's collection movie = content_manager.get(tmdb_id=tmdb_id) # If it doesn't already exists, add then request it if movie is None: movie = content_manager.add( tmdb_id=tmdb_id, quality_profile_id=radarr_params["radarr_profile_id"], root_dir=radarr_params["radarr_root"], ) # Save and request add_unique_to_db( UserRequest, content_id=tmdb_id, source="tmdb", content_type="movie", requested_by=request.user, ) content_manager.request(radarr_id=movie["id"]) log.handler( request.user.username + " requested movie " + movie["title"], log.INFO, __logger, ) return JsonResponse({}) return HttpResponseForbidden()
def all_requests(request): template = loader.get_template("viewport/requests.html") content_discovery = ContentDiscovery() content_manager = ContentManager() user_requests = UserRequest.objects.all().order_by("id").reverse() all_cards = [] request_dict = {} for entry in user_requests.values( "content_id", "source", "content_type", "requested_by__username", ): # Fetch TMDB entry if entry["source"] == "tmdb" and request_is_unique( entry, request_dict): card = content_discovery.get_by_tmdb_id( tmdb_id=entry["content_id"], content_type=entry["content_type"], obtain_extras=False, ) if card is not None: card["tmdbCard"] = True card["requested_by"] = entry["requested_by__username"] all_cards.append(card) # Fetch TVDB entry if entry["source"] == "tvdb" and request_is_unique( entry, request_dict): # Attempt to convert card to TMDB conversion = content_discovery.get_by_tvdb_id( tvdb_id=entry["content_id"]) # Conversion found if conversion.__contains__( "tv_results") and conversion["tv_results"]: card = conversion["tv_results"][0] card["tmdbCard"] = True card["requested_by"] = entry["requested_by__username"] all_cards.append(card) # Convert all requests to use this new ID old_requests = UserRequest.objects.filter( content_id=entry["content_id"], source="tvdb") old_requests.update(content_id=card["id"], source="tmdb") # Fallback to checking sonarr's database else: card = content_manager.get(tvdb_id=entry["content_id"]) # Determine if the card has a known poster image if isinstance(card, dict): card["contentType"] = entry["content_type"] if card.__contains__("images"): remote_poster = is_key_value_in_list("coverType", "poster", card["images"], return_item=True) if remote_poster: card["remotePoster"] = remote_poster["remoteUrl"] card["requested_by"] = entry["requested_by__username"] all_cards.append(card) if card is None and not request_is_unique(entry, request_dict): log.handler( entry["content_type"] + " from " + entry["source"] + " with ID " + entry["content_id"] + " no longer exists!", log.WARNING, __logger, ) # Set the availability content_discovery.determine_id_validity({"results": all_cards}) set_many_availability(all_cards) context = generate_context({"all_cards": all_cards}) return HttpResponse(template.render(context, request))
def generate_issue_cards(reported_issues): """Retruns a list of cards""" content_discovery = ContentDiscovery() content_manager = ContentManager() all_cards = [] for entry in reported_issues.values( "id", "reported_by__username", "content_id", "source", "resolved", "content_type", "issues", "seasons", "episodes", ): # Fetch TMDB entry if entry["source"] == "tmdb": card = content_discovery.get_by_tmdb_id( tmdb_id=entry["content_id"], content_type=entry["content_type"], obtain_extras=False, ) if card is not None: all_cards.append({**card, **entry}) # Fetch TVDB entry if entry["source"] == "tvdb": # Attempt to convert card to TMDB conversion = content_discovery.get_by_tvdb_id(tvdb_id=entry["content_id"]) # Conversion found if conversion.__contains__("tv_results") and conversion["tv_results"]: card = conversion["tv_results"][0] all_cards.append({**card, **entry}) # Convert all requests to use this new ID old_requests = ReportedIssue.objects.filter( content_id=entry["content_id"], source="tvdb" ) old_requests.update(content_id=card["id"], source="tmdb") log.handler( entry["content_type"] + " from " + entry["source"] + " with ID " + entry["content_id"] + " has been converted to TMDB", log.INFO, _logger, ) # Fallback to checking sonarr's database else: card = content_manager.get(tvdb_id=entry["content_id"]) all_cards.append({**card, **entry}) if card is None: log.handler( entry["content_type"] + " from " + entry["source"] + " with ID " + entry["content_id"] + " no longer exists!", log.WARNING, _logger, ) return all_cards
def my_issues(request): # Get the parameters from the URL content_discovery = ContentDiscovery() content_manager = ContentManager() reported_issues = ( ReportedIssue.objects.filter(reported_by=request.user).order_by("id").reverse() ) all_cards = [] for entry in reported_issues.values( "reported_by__username", "content_id", "source", "resolved", "content_type", "issues", "seasons", "episodes", ): # Fetch TMDB entry if entry["source"] == "tmdb": card = content_discovery.get_by_tmdb_id( tmdb_id=entry["content_id"], content_type=entry["content_type"], obtain_extras=False, ) if card is not None: card["tmdbCard"] = True all_cards.append({**card, **entry}) # Fetch TVDB entry if entry["source"] == "tvdb": # Attempt to convert card to TMDB conversion = content_discovery.get_by_tvdb_id(tvdb_id=entry["content_id"]) # Conversion found if conversion.__contains__("tv_results") and conversion["tv_results"]: card = conversion["tv_results"][0] card["tmdbCard"] = True all_cards.append({**card, **entry}) # Convert all requests to use this new ID old_requests = ReportedIssue.objects.filter( content_id=entry["content_id"], source="tvdb" ) old_requests.update(content_id=card["id"], source="tmdb") # Fallback to checking sonarr's database else: card = content_manager.get(tvdb_id=entry["content_id"]) all_cards.append({**card, **entry}) if card is None: log.handler( entry["content_type"] + " from " + entry["source"] + " with ID " + entry["content_id"] + " no longer exists!", log.WARNING, __logger, ) context = generate_context({"all_cards": all_cards}) template = loader.get_template("viewport/reported_issues.html") return HttpResponse(template.render(context, request))