summaryrefslogtreecommitdiff
path: root/tidal_dl_ng/api.py
blob: 4da23f1faf3c0f5c9a1bb7c9540a05415f766bdf (plain) (blame)
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import json

import requests

# See also
# https://github.com/yaronzz/Tidal-Media-Downloader/commit/1d5b8cd8f65fd1def45d6406778248249d6dfbdf
# https://github.com/yaronzz/Tidal-Media-Downloader/pull/840
# https://github.com/nathom/streamrip/tree/main/streamrip
# https://github.com/arnesongit/plugin.audio.tidal2/blob/e9429d601d0c303d775d05a19a66415b57479d87/resources/lib/tidal2/tidalapi/__init__.py#L86

# TODO: Implement this into `Download`: Session should randomize the usage.
__KEYS_JSON__ = """
{
    "version": "1.0.1",
    "keys": [
        // Invalid
        {
            "platform": "Fire TV",
            "formats": "Normal/High/HiFi(No Master)",
            "clientId": "OmDtrzFgyVVL6uW56OnFA2COiabqm",
            "clientSecret": "zxen1r3pO0hgtOC7j6twMo9UAqngGrmRiWpV7QC1zJ8=",
            "valid": "False",
            "from": "Fokka-Engineering (https://github.com/Fokka-Engineering/libopenTIDAL/blob/655528e26e4f3ee2c426c06ea5b8440cf27abc4a/README.md#example)"
        },
        // Only max MQA.
        {
            "platform": "Fire TV",
            "formats": "Master-Only(Else Error)",
            "clientId": "7m7Ap0JC9j1cOM3n",
            "clientSecret": "vRAdA108tlvkJpTsGZS8rGZ7xTlbJ0qaZ2K9saEzsgY=",
            "valid": "True",
            "from": "Dniel97 (https://github.com/Dniel97/RedSea/blob/4ba02b88cee33aeb735725cb854be6c66ff372d4/config/settings.example.py#L68)"
        },
        // Invalid
        {
            "platform": "Android TV",
            "formats": "Normal/High/HiFi(No Master)",
            "clientId": "Pzd0ExNVHkyZLiYN",
            "clientSecret": "W7X6UvBaho+XOi1MUeCX6ewv2zTdSOV3Y7qC3p3675I=",
            "valid": "False",
            "from": ""
        },
        // Invalid
        {
            "platform": "TV",
            "formats": "Normal/High/HiFi/Master",
            "clientId": "8SEZWa4J1NVC5U5Y",
            "clientSecret": "owUYDkxddz+9FpvGX24DlxECNtFEMBxipU0lBfrbq60=",
            "valid": "False",
            "from": "morguldir (https://github.com/morguldir/python-tidal/commit/50f1afcd2079efb2b4cf694ef5a7d67fdf619d09)"
        },
        // Invalid
        {
            "platform": "Android Auto",
            "formats": "Normal/High/HiFi/Master",
            "clientId": "zU4XHVVkc2tDPo4t",
            "clientSecret": "VJKhDFqJPqvsPVNBV6ukXTJmwlvbttP7wlMlrc72se4=",
            "valid": "True",
            "from": "1nikolas (https://github.com/yaronzz/Tidal-Media-Downloader/pull/840)"
        }
    ]
}
"""
__API_KEYS__ = json.loads(__KEYS_JSON__)
__ERROR_KEY__ = (
    {
        "platform": "None",
        "formats": "",
        "clientId": "",
        "clientSecret": "",
        "valid": "False",
    },
)

from tidal_dl_ng.constants import REQUESTS_TIMEOUT_SEC


def getNum():
    return len(__API_KEYS__["keys"])


def getItem(index: int):
    if index < 0 or index >= len(__API_KEYS__["keys"]):
        return __ERROR_KEY__
    return __API_KEYS__["keys"][index]


def isItemValid(index: int):
    item = getItem(index)
    return item["valid"] == "True"


def getItems():
    return __API_KEYS__["keys"]


def getLimitIndexs():
    array = []
    for i in range(len(__API_KEYS__["keys"])):
        array.append(str(i))
    return array


def getVersion():
    return __API_KEYS__["version"]


# Load from gist
try:
    respond = requests.get(
        "https://api.github.com/gists/48d01f5a24b4b7b37f19443977c22cd6", timeout=REQUESTS_TIMEOUT_SEC
    )
    if respond.status_code == 200:
        content = respond.json()["files"]["tidal-api-key.json"]["content"]
        __API_KEYS__ = json.loads(content)
except Exception as e:
    # TODO: Implement proper logging.
    print(e)
    pass