aboutsummaryrefslogtreecommitdiff
path: root/code/utils/content_get.py
blob: 1b724e595234f22c7d8ac937be4678dd94be5f0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import requests


def get_content(
    query: str,
    service: str,
    host: str,
    headers: str,
):
    """
    Fetch the top 5 results from the service given a query

    Args:
        query (str): The query to search for
        service (str): The service to search in
        host (str): The host URL
        headers (str): The headers for the request

    Returns:
        list: A list containing content_info dict
        str: NO RESULTS
        str: ALREADY ADDED
    """
    query = query.strip().replace(" ", "%20")
    # Search for matching content
    results = requests.get(
        f"{host}/api/v3/{'movie' if service == 'radarr' else 'series'}/lookup?term={query}",
        headers=headers,
    ).json()

    if len(results) == 0:
        return "NO RESULTS"
    # If already added to library
    if results[0]["added"] != "0001-01-01T05:51:00Z":
        return "ALREADY ADDED"

    # Add info for top results
    content_info = []
    for i in range(min(5, len(results))):
        content_info.append(
            {
                "title": results[i]["title"],
                "year": results[i]["year"],
                "contentId": results[i][
                    f"{'tmdbId' if service == 'radarr' else 'tvdbId'}"
                ],
                "description": results[i]["overview"],
            }
        )

        # Add remotePoster field, set None if not available
        try:
            content_info[i]["remotePoster"] = results[i]["images"][0][
                "remoteUrl"
            ]
        except IndexError:
            content_info[i]["remotePoster"] = None

    return content_info