File size: 8,324 Bytes
b10caf3
 
 
 
d7b5213
 
 
 
b10caf3
 
 
e61f73c
 
 
 
 
 
 
 
 
 
 
 
 
b10caf3
d7b5213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdc1f07
d7b5213
 
 
b10caf3
 
 
 
d7b5213
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e61f73c
 
d7b5213
e61f73c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b10caf3
 
 
 
 
 
 
 
 
 
d7b5213
 
 
 
 
 
b10caf3
 
 
 
 
 
 
 
 
d7b5213
b10caf3
 
 
 
 
 
 
d7b5213
 
e61f73c
b10caf3
d7b5213
e61f73c
 
 
 
 
 
d7b5213
 
 
 
 
 
 
 
 
 
 
 
 
 
b10caf3
 
e61f73c
 
 
 
 
 
d7b5213
 
 
 
 
 
 
 
 
 
 
 
 
e61f73c
d7b5213
 
 
e61f73c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b10caf3
 
 
d7b5213
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import base64
import json
import re
import xml.etree.ElementTree as ET
import asyncio
from typing import Optional, List, Any, Dict
from fastapi import FastAPI, HTTPException, Query, Response, Request
from fastapi.responses import JSONResponse, StreamingResponse
import httpx
import os

from contextlib import asynccontextmanager

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Initialize global client
    app.state.client = httpx.AsyncClient(follow_redirects=True, timeout=30.0)
    yield
    # Clean up
    await app.state.client.aclose()

app = FastAPI(title="Tidal Stream Processor", lifespan=lifespan)

# ... (keep AVAILABLE_APIS, MAX_RETRIES, etc. unchanged)

# Global API list with rotation logic
AVAILABLE_APIS = [
    "https://tidal-api.binimum.org",
    "https://tidal.kinoplus.online",
    "https://triton.squid.wtf",
    "https://vogel.qqdl.site",
    "https://maus.qqdl.site",
    "https://hund.qqdl.site",
    "https://katze.qqdl.site",
    "https://wolf.qqdl.site",
    "https://hifi-one.spotisaver.net",
    "https://hifi-two.spotisaver.net",
    "https://eu-central.monochrome.tf",
    "https://us-west.monochrome.tf",
    "https://api.monochrome.tf",
    "https://monochrome-api.samidy.com",
    "https://api.zarz.moe/v1/tidal",
    "https://tidal.squid.wtf",
    "https://tidal.afkarxyz.qzz.io"
]

MAX_RETRIES = 2
RETRY_DELAY = 0.5
TIMEOUT = 15.0
HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36"
}

async def fetch_api_with_retry(client: httpx.AsyncClient, api: str, track_id: int, quality: str) -> Dict[str, Any]:
    current_delay = RETRY_DELAY
    base_url = api.rstrip('/')
    url = f"{base_url}/track/"
    params = {"id": track_id, "quality": quality}

    for attempt in range(MAX_RETRIES + 1):
        if attempt > 0:
            await asyncio.sleep(current_delay)
            current_delay *= 2
        try:
            resp = await client.get(url, params=params, headers=HEADERS, timeout=TIMEOUT)
            if resp.status_code == 429:
                continue
            if resp.status_code >= 500:
                continue
            if resp.status_code != 200:
                continue
            body = resp.json()
            if (isinstance(body, list) and len(body) > 0) or (isinstance(body, dict) and (body.get("data") or body.get("manifest"))):
                return body
        except Exception:
            continue
    raise Exception(f"API {api} failed")

async def get_tidal_data_sequential(client: httpx.AsyncClient, track_id: int, quality: str) -> Dict[str, Any]:
    """顺序请求 API,失败的移动到末尾,成功的保持在开头"""
    global AVAILABLE_APIS
    
    # 我们创建一个副本进行迭代,因为我们会修改原列表
    apis_to_try = list(AVAILABLE_APIS)
    errors = []
    
    for api in apis_to_try:
        try:
            result = await fetch_api_with_retry(client, api, track_id, quality)
            return result
        except Exception as e:
            errors.append(f"{api}: {e}")
            
            # 将失败的 API 移动到列表末尾
            if api in AVAILABLE_APIS:
                AVAILABLE_APIS.remove(api)
                AVAILABLE_APIS.append(api)
    
    raise HTTPException(status_code=500, detail=f"All Tidal APIs failed: {'; '.join(errors[:3])}")

def parse_dash_manifest(xml_str: str):
    xml_str = re.sub(r'\sxmlns="[^"]+"', '', xml_str, count=1)
    try:
        root = ET.fromstring(xml_str)
    except ET.ParseError:
        return None, None, 0
    seg_temp = root.find(".//SegmentTemplate")
    if seg_temp is None:
        return None, None, 0
    init_val = seg_temp.get("initialization")
    media_val = seg_temp.get("media")
    if not init_val or not media_val:
        return None, None, 0
    init_url = init_val.replace("&", "&")
    media_template = media_val.replace("&", "&")
    segment_timeline = seg_temp.find("SegmentTimeline")
    total_segments = 0
    if segment_timeline is not None:
        for s in segment_timeline.findall("S"):
            repeat = int(s.get("r", 0))
            total_segments += (repeat + 1)
    return init_url, media_template, total_segments

def generate_m3u8(init_url: str, media_template: str, count: int) -> str:
    m3u8 = ["#EXTM3U", "#EXT-X-VERSION:7", "#EXT-X-TARGETDURATION:5", "#EXT-X-MEDIA-SEQUENCE:1", f'#EXT-X-MAP:URI="{init_url}"']
    for i in range(1, count + 1):
        segment_url = media_template.replace("$Number$", str(i))
        m3u8.append("#EXTINF:4.0,")
        m3u8.append(segment_url)
    m3u8.append("#EXT-X-ENDLIST")
    return "\n".join(m3u8)

@app.get("/")
async def root():
    return {"message": "Tidal Stream Processor is running", "endpoints": {"fetch": "/fetch?id={track_id}&quality={quality}", "playlist": "/playlist.m3u8?id={track_id}&quality={quality}"}}

@app.get("/fetch")
async def fetch_tidal_media(
    request: Request,
    track_id: int = Query(..., alias="id"), 
    quality: str = "LOSSLESS"
):
    body = await get_tidal_data_sequential(request.app.state.client, track_id, quality)
    if isinstance(body, list) and len(body) > 0:
        direct_url = body[0].get("OriginalTrackUrl")
        if direct_url:
            return {"type": "direct", "url": direct_url}
    manifest_b64 = body.get("data", {}).get("manifest") or body.get("manifest")
    if not manifest_b64:
        raise HTTPException(status_code=404, detail="Manifest not found")
    manifest_str = base64.b64decode(manifest_b64).decode("utf-8")
    if manifest_str.strip().startswith("{"):
        bts_data = json.loads(manifest_str)
        if bts_data.get("encryptionType") == "NONE" and bts_data.get("urls"):
            return {"type": "direct", "url": bts_data["urls"][0]}
        return {"type": "error", "detail": "Encrypted BTS not supported"}
    return {"type": "m3u8", "manifest_url": f"/playlist.m3u8?id={track_id}&quality={quality}"}

@app.get("/playlist.m3u8")
async def get_m3u8_playlist(
    request: Request,
    track_id: int = Query(..., alias="id"), 
    quality: str = "LOSSLESS"
):
    body = await get_tidal_data_sequential(request.app.state.client, track_id, quality)
    manifest_b64 = body.get("data", {}).get("manifest") or body.get("manifest")
    if not manifest_b64:
         raise HTTPException(status_code=404, detail="Manifest not found")
    manifest_str = base64.b64decode(manifest_b64).decode("utf-8")
    init_url, media_template, count = parse_dash_manifest(manifest_str)
    if not init_url:
        raise HTTPException(status_code=400, detail="Could not parse DASH manifest")
    return Response(content=generate_m3u8(init_url, media_template, count), media_type="application/vnd.apple.mpegurl")

@app.api_route("/proxy", methods=["GET", "HEAD", "OPTIONS"])
async def proxy(request: Request):
    if request.method == "OPTIONS":
        return Response(headers={"Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, HEAD, OPTIONS", "Access-Control-Allow-Headers": "*"})
    
    url = request.query_params.get("url")
    if not url:
        return Response("Missing url", status_code=400)
    
    client = request.app.state.client
    
    # Send request with stream=True to avoid consuming the body prematurely
    req = client.build_request(
        request.method, 
        url, 
        headers={
            "User-Agent": request.headers.get("User-Agent", "Mozilla/5.0"),
            "Accept": request.headers.get("Accept", "*/*"),
            "Referer": request.headers.get("Referer", "")
        }
    )
    resp = await client.send(req, stream=True)
    
    response_headers = dict(resp.headers)
    response_headers["Access-Control-Allow-Origin"] = "*"
    response_headers.pop("X-Frame-Options", None)
    
    # We use a generator to ensure the response is closed properly after streaming is complete
    async def iterate_and_close():
        try:
            async for chunk in resp.aiter_raw():
                yield chunk
        finally:
            await resp.aclose()

    return StreamingResponse(
        iterate_and_close(), 
        status_code=resp.status_code, 
        headers=response_headers
    )

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))