Chrunos commited on
Commit
a988142
·
verified ·
1 Parent(s): a850b17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -79
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import sys
2
  import time
3
- from fastapi import FastAPI, BackgroundTasks, Request, HTTPException
 
4
  from fastapi.responses import FileResponse
5
  from fastapi.concurrency import run_in_threadpool
6
  import yt_dlp
@@ -16,12 +17,6 @@ import json
16
  from dotenv import load_dotenv
17
  import mimetypes
18
  import tempfile
19
- from mutagen.mp4 import MP4, MP4Cover, MP4FreeForm
20
- from mutagen.mp3 import MP3
21
- from mutagen.id3 import ID3, USLT, SYLT, Encoding, APIC, TIT2, TPE1, TALB, TPE2, TDRC, TCON, TRCK, COMM
22
- from mutagen.oggvorbis import OggVorbis
23
- from mutagen.oggopus import OggOpus
24
- from mutagen.flac import FLAC, Picture
25
  from PIL import Image
26
  from io import BytesIO
27
  from pathlib import Path
@@ -32,23 +27,9 @@ import logging
32
  import gc
33
 
34
  tmp_dir = tempfile.gettempdir()
35
- BASE_URL = "https://chrunos-multi.hf.space"
36
-
37
- MP4_TAGS_MAP = {
38
- "album": "\xa9alb",
39
- "album_artist": "aART",
40
- "artist": "\xa9ART",
41
- "composer": "\xa9wrt",
42
- "copyright": "cprt",
43
- "lyrics": "\xa9lyr",
44
- "comment": "desc",
45
- "media_type": "stik",
46
- "producer": "\xa9prd",
47
- "rating": "rtng",
48
- "release_date": "\xa9day",
49
- "title": "\xa9nam",
50
- "url": "\xa9url",
51
- }
52
  def env_to_cookies(env_content: str, output_file: str) -> None:
53
  """Convert environment variable content back to cookie file"""
54
  try:
@@ -115,12 +96,6 @@ def create_temp_cookie_file():
115
  load_dotenv()
116
  app = FastAPI()
117
 
118
- ydl_opts = {
119
- 'format': 'best',
120
- 'quiet': True,
121
- #'outtmpl': f'{VIDEO_DIR}/%(id)s.%(ext)s',
122
- 'max_filesize': 50 * 1024 * 1024
123
- }
124
 
125
 
126
  @app.get('/')
@@ -230,60 +205,25 @@ async def download_high_quality_video(request: Request):
230
 
231
 
232
 
233
- @app.post("/max")
234
- async def download_high_quality_video(request: Request):
235
- data = await request.json()
236
- video_url = data.get('url')
237
- quality = data.get('quality', '1080') # Default to 1080p if not specified
238
-
239
- # Check if the requested quality is above 1080p
240
- if int(quality) > 1080:
241
- error_message = "Quality above 1080p is for premium users. Please check the URL for more information."
242
- help_url = "https://chrunos.com/premium-shortcuts/" # Replace with your actual URL
243
- return {"error": error_message, "url": help_url}
244
-
245
- cookiefile = "firefox-cookies.txt"
246
- env_to_cookies_from_env("firefox-cookies.txt")
247
-
248
- timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
249
- output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')
250
 
251
- # Convert quality string to height
252
- height_map = {
253
- '480': 480,
254
- '720': 720,
255
- '1080': 1080
256
- }
257
- max_height = height_map.get(quality, 1080) # Use the quality variable correctly
258
 
259
- # Determine format string based on quality
260
- format_str = f'bestvideo[height<={max_height}][vcodec^=avc]+bestaudio/best'
261
-
262
- ydl_opts = {
263
- 'format': format_str,
264
- 'outtmpl': output_template,
265
- 'quiet': True,
266
- 'no_warnings': True,
267
- 'noprogress': True,
268
- 'merge_output_format': 'mp4',
269
- 'cookiefile': cookiefile
270
- }
271
-
272
- await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
273
-
274
- downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
275
- if not downloaded_files:
276
- return {"error": "Download failed"}
277
-
278
- downloaded_file = downloaded_files[0]
279
- encoded_filename = urllib.parse.quote(downloaded_file.name)
280
- download_url = f"{BASE_URL}/file/{encoded_filename}"
281
- gc.collect()
282
- return {"url": download_url}
283
 
 
 
 
 
 
 
 
284
 
285
  @app.post("/audio")
286
- async def download_audio(request: Request):
 
 
 
287
  data = await request.json()
288
  video_url = data.get('url')
289
  cookiefile = "firefox-cookies.txt"
 
1
  import sys
2
  import time
3
+ from fastapi import FastAPI, BackgroundTasks, Request, HTTPException, Security
4
+ from fastapi.security import APIKeyHeader
5
  from fastapi.responses import FileResponse
6
  from fastapi.concurrency import run_in_threadpool
7
  import yt_dlp
 
17
  from dotenv import load_dotenv
18
  import mimetypes
19
  import tempfile
 
 
 
 
 
 
20
  from PIL import Image
21
  from io import BytesIO
22
  from pathlib import Path
 
27
  import gc
28
 
29
  tmp_dir = tempfile.gettempdir()
30
+ BASE_URL = "https://chrunos-zam.hf.space"
31
+
32
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  def env_to_cookies(env_content: str, output_file: str) -> None:
34
  """Convert environment variable content back to cookie file"""
35
  try:
 
96
  load_dotenv()
97
  app = FastAPI()
98
 
 
 
 
 
 
 
99
 
100
 
101
  @app.get('/')
 
205
 
206
 
207
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
+ api_key_header = APIKeyHeader(name="X-API-Key")
 
 
 
 
 
 
210
 
211
+ # Store this securely in your environment variables
212
+ API_KEY = os.getenv("API_KEY")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
+ async def verify_api_key(api_key: str = Security(api_key_header)):
215
+ if api_key != API_KEY:
216
+ raise HTTPException(
217
+ status_code=403,
218
+ detail="Invalid API key"
219
+ )
220
+ return api_key
221
 
222
  @app.post("/audio")
223
+ async def download_audio(
224
+ request: Request,
225
+ api_key: str = Security(verify_api_key)
226
+ ):
227
  data = await request.json()
228
  video_url = data.get('url')
229
  cookiefile = "firefox-cookies.txt"