tecuts commited on
Commit
0694f9d
·
verified ·
1 Parent(s): b7c7178

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -9
app.py CHANGED
@@ -127,33 +127,55 @@ def extract_video_id(url):
127
  @app.get("/yt-audio")
128
  async def yt_audio(url: str):
129
  try:
130
- # 设置请求头
131
  videoId = extract_video_id(url)
 
 
 
 
132
  headers = {
133
- "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.2 Safari/605.1.15",
134
- "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
135
- "Referer": "https://www.youtube.com/"
136
  }
137
 
138
- body = f'{'context':{'client':{'clientName':'IOS','clientVersion':'19.14.3','deviceModel':'iPhone15,4'}},'videoId':{videoId}}'
 
 
 
 
 
 
 
 
 
 
139
 
140
  # 发送请求
 
141
  response = requests.post(
142
  url,
143
  timeout=20,
144
- json=body_json,
145
  headers=headers
146
  )
147
  response.raise_for_status()
148
 
149
- logging.info(response)
150
-
151
- return response
152
 
153
  except requests.RequestException as e:
 
154
  return {"error": f"Request error: {str(e)}"}
155
  except json.JSONDecodeError as e:
 
156
  return {"error": f"JSON decoding error: {str(e)}"}
 
 
 
 
 
 
157
 
158
  # Middleware to set the correct MIME type
159
  @app.middleware("http")
 
127
  @app.get("/yt-audio")
128
  async def yt_audio(url: str):
129
  try:
130
+ # 提取视频 ID
131
  videoId = extract_video_id(url)
132
+ if videoId is None:
133
+ raise ValueError("Video ID extraction failed")
134
+
135
+ # 设置请求头
136
  headers = {
137
+ "X-Goog-Authuser": "0",
138
+ "X-Origin": "https://www.youtube.com",
139
+ "X-goog-Visitor-Id": "CgtMWlVXOTFCcE5Edyj9p4i9BjIKCgJVUxIEGgAgZA%3D%3D"
140
  }
141
 
142
+ # 构造请求体
143
+ body = {
144
+ "context": {
145
+ "client": {
146
+ "clientName": "IOS",
147
+ "clientVersion": "19.14.3",
148
+ "deviceModel": "iPhone15,4"
149
+ }
150
+ },
151
+ "videoId": videoId
152
+ }
153
 
154
  # 发送请求
155
+ logging.info(f"Sending request to URL: {url} with video ID: {videoId}")
156
  response = requests.post(
157
  url,
158
  timeout=20,
159
+ json=body,
160
  headers=headers
161
  )
162
  response.raise_for_status()
163
 
164
+ logging.info(f"Received response from URL: {url}, status code: {response.status_code}")
165
+ return response.json()
 
166
 
167
  except requests.RequestException as e:
168
+ logging.error(f"Request error: {str(e)}")
169
  return {"error": f"Request error: {str(e)}"}
170
  except json.JSONDecodeError as e:
171
+ logging.error(f"JSON decoding error: {str(e)}")
172
  return {"error": f"JSON decoding error: {str(e)}"}
173
+ except ValueError as e:
174
+ logging.error(f"Value error: {str(e)}")
175
+ return {"error": f"Value error: {str(e)}"}
176
+ except Exception as e:
177
+ logging.error(f"Unexpected error: {str(e)}")
178
+ return {"error": f"Unexpected error: {str(e)}"}
179
 
180
  # Middleware to set the correct MIME type
181
  @app.middleware("http")