File size: 32,302 Bytes
05fb587
 
db647ae
a988142
05fb587
 
 
 
 
 
575abdb
05fb587
 
 
 
 
 
 
 
 
 
 
 
575abdb
406d2c0
ad4a15d
b0b8979
149b99d
9c06b1d
c60a310
 
ea78b40
cbd5822
e78636c
05fb587
 
a988142
 
 
05fb587
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10e71aa
05fb587
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b467a4
222a7c1
0651ec2
222a7c1
59c77bf
222a7c1
 
0651ec2
 
3d3b80f
8fe48f4
0651ec2
222a7c1
59c77bf
 
 
 
 
 
059f051
0651ec2
 
59c77bf
 
 
8fe48f4
0651ec2
f2946ca
59c77bf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0651ec2
 
f8061b4
59c77bf
 
 
3913791
59c77bf
 
 
 
 
3913791
0651ec2
 
 
 
 
 
 
 
4b467a4
3913791
0651ec2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3913791
59c77bf
 
 
 
 
 
 
 
 
888ac00
0651ec2
7cc734b
0651ec2
 
222a7c1
 
 
05fb587
 
 
149b99d
7b39c90
149b99d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7b39c90
 
 
 
149b99d
 
 
 
7b39c90
 
 
 
 
 
149b99d
 
 
 
dae75f6
149b99d
 
575abdb
 
 
149b99d
 
 
575abdb
 
7b39c90
c60a310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2722593
 
 
c60a310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ece0416
c60a310
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9c06b1d
8e49823
f82f9f8
7ee85db
 
f82f9f8
7ee85db
f82f9f8
7ee85db
 
0cf8fb7
8e49823
0cf8fb7
8e49823
0cf8fb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7ee85db
0cf8fb7
7ee85db
8e49823
 
 
7ee85db
0cf8fb7
 
f82f9f8
7ee85db
947de95
f82f9f8
7ee85db
 
f82f9f8
7ee85db
f82f9f8
7ee85db
 
947de95
 
 
 
 
 
 
 
 
 
 
 
 
 
7ee85db
 
 
947de95
 
 
7ee85db
 
 
947de95
e41b1ff
 
8e49823
 
 
947de95
f82f9f8
7c5ed7b
7ee85db
7c5ed7b
8e49823
7c5ed7b
8e49823
2a22fbf
8e49823
 
 
2a22fbf
8e49823
 
 
 
 
fe4a943
8e49823
fe4a943
 
 
8e49823
 
fe4a943
 
 
 
 
 
8e49823
fe4a943
 
 
 
 
8e49823
2a22fbf
fe4a943
 
8e49823
2a22fbf
8e49823
 
 
 
 
7ee85db
8e49823
7ee85db
 
 
8e49823
194b841
 
d681d0b
802eb28
 
194b841
 
 
 
6cfdeb9
802eb28
 
194b841
821679d
dfc3dd7
802eb28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
771847a
fc79f05
 
 
 
 
 
 
 
 
 
 
 
 
 
b6f4c25
802eb28
 
 
 
3a9444a
 
 
 
a8a1ac8
 
e983159
3a9444a
 
4e5f4ac
a053aae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a780585
 
4e5f4ac
 
 
e5340c7
4e5f4ac
 
51d6520
4e5f4ac
3a9444a
9c06b1d
0c42700
05fb587
575abdb
 
149b99d
3b99235
149b99d
 
 
 
 
 
 
9c06b1d
 
05fb587
6c6037a
05fb587
056546b
 
171f368
6c6037a
48477a1
056546b
575abdb
dcec7dc
 
575abdb
 
 
05fb587
 
575abdb
05fb587
d6de3a2
05fb587
575abdb
 
dcec7dc
 
575abdb
 
 
 
dcec7dc
575abdb
 
 
05fb587
 
575abdb
05fb587
 
 
 
9c06b1d
05fb587
 
9c06b1d
0f2113c
 
c60a310
b288cd0
c60a310
 
 
 
0f2113c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
575abdb
 
 
 
05fb587
 
 
 
a988142
2bae5b1
a988142
 
05fb587
6cfdeb9
a988142
 
 
 
 
 
 
2bae5b1
05fb587
a988142
4e5f4ac
 
a988142
d36bf90
 
 
 
 
 
 
 
 
 
 
05fb587
 
4e5f4ac
 
05fb587
 
d6de3a2
05fb587
 
 
 
 
 
 
4e5f4ac
3b118ef
 
 
 
 
05fb587
 
8e883aa
4e5f4ac
0d3bda2
4e5f4ac
 
 
 
 
 
 
 
05fb587
4e5f4ac
 
 
 
 
 
 
 
d36bf90
ad4a15d
 
 
f6fc6c9
ad4a15d
 
014c24c
 
 
ad4a15d
 
 
 
 
 
 
 
 
 
 
 
 
d6de3a2
ad4a15d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b0b8979
ad4a15d
 
05fb587
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
import sys
import time
from fastapi import FastAPI, BackgroundTasks, Request, HTTPException, Security
from fastapi.security import APIKeyHeader
from fastapi.responses import FileResponse
from fastapi.concurrency import run_in_threadpool
import yt_dlp
import ffmpeg
import urllib.parse
import os
from datetime import datetime, timedelta
import schedule
import requests
import uvicorn
import subprocess
import json
from dotenv import load_dotenv
import mimetypes
import tempfile
from PIL import Image
from io import BytesIO
from pathlib import Path
from fastapi.staticfiles import StaticFiles
from collections import defaultdict
from starlette.responses import JSONResponse
import logging
import gc
from typing import Dict, Any
import re
import asyncio
import cloudscraper
import httpx
from bs4 import BeautifulSoup
from pydantic import BaseModel

tmp_dir = tempfile.gettempdir()
BASE_URL = "https://chrunos-zam.hf.space"


def env_to_cookies(env_content: str, output_file: str) -> None:
    """Convert environment variable content back to cookie file"""
    try:
        # Extract content from env format
        if '="' not in env_content:
            raise ValueError("Invalid env content format")
            
        content = env_content.split('="', 1)[1].strip('"')
        
        # Replace escaped newlines with actual newlines
        cookie_content = content.replace('\\n', '\n')
        
        # Write to cookie file
        with open(output_file, 'w') as f:
            f.write(cookie_content)
            
    except Exception as e:
        raise ValueError(f"Error converting to cookie file: {str(e)}")

def save_to_env_file(env_content: str, env_file: str = '.env') -> None:
    """Save environment variable content to .env file"""
    try:
        with open(env_file, 'w') as f:
            f.write(env_content)
        #print(f"Successfully saved to {env_file}")
    except Exception as e:
        raise ValueError(f"Error saving to env file: {str(e)}")

def env_to_cookies_from_env(output_file: str) -> None:
    """Convert environment variable from .env file to cookie file"""
    try:
        load_dotenv()  # Load from .env file
        env_content = os.getenv('FIREFOX_COOKIES')
        #print(f"Printing env content: \n{env_content}")
        if not env_content:
            raise ValueError("FIREFOX_COOKIES not found in .env file")
            
        env_to_cookies(f'FIREFOX_COOKIES="{env_content}"', output_file)
    except Exception as e:
        raise ValueError(f"Error converting to cookie file: {str(e)}")

def get_cookies():
    """Get cookies from environment variable"""
    load_dotenv()
    cookie_content = os.getenv('FIREFOX_COOKIES')
    #print(cookie_content)
    if not cookie_content:
        raise ValueError("FIREFOX_COOKIES environment variable not set")
    return cookie_content

def create_temp_cookie_file():
    """Create temporary cookie file from environment variable"""
    temp_cookie = tempfile.NamedTemporaryFile(mode='w+', delete=False, suffix='.txt')
    try:
        cookie_content = get_cookies()
        # Replace escaped newlines with actual newlines
        cookie_content = cookie_content.replace('\\n', '\n')
        temp_cookie.write()
        temp_cookie.flush()
        return Path(temp_cookie.name)
    finally:
        temp_cookie.close()

load_dotenv()
app = FastAPI()



@app.get('/')
def main():
    return "API Is Running. If you want to use this API, contact Cody from chrunos.com"

@app.get("/get_video_url")
async def get_video_url(youtube_url: str):
    try:
        cookiefile = "firefox-cookies.txt"
        env_to_cookies_from_env("firefox-cookies.txt")
        # Add cookies
        ydl_opts["cookiefile"] =  "firefox-cookies.txt" #create_temp_cookie_file() 
    
        
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(youtube_url, download=False)
            return info
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))




@app.get("/script")
async def get_transcript(youtube_url: str, language: str = None):
    try:
        # Set up yt-dlp options
        ydl_opts = {
            'skip_download': True,
            'writesubtitles': True,
            'writeautomaticsub': True,
            'outtmpl': '%(id)s.%(ext)s',
            'noplaylist': True,
            'cookiefile': "firefox-cookies.txt"
        }
        
        # If a language is specified, only download that language
        # Otherwise, we'll first get video info to determine the original language
        if language:
            ydl_opts['subtitleslangs'] = [language]
        
        env_to_cookies_from_env("firefox-cookies.txt")
        logger.info(f"Current directory files (before): {os.listdir('.')}")
        
        # First, get video info without downloading anything
        with yt_dlp.YoutubeDL({**ydl_opts, 'skip_download': True, 'writesubtitles': False, 'writeautomaticsub': False}) as ydl:
            info = ydl.extract_info(youtube_url, download=False)
            video_id = info['id']
            logger.info(f"Video ID: {video_id}")
            
            # If no language specified, try to use the original language
            if not language:
                # Try to determine the original language if available in the info
                if 'subtitles' in info and info['subtitles']:
                    # Use the first available subtitle language
                    available_languages = list(info['subtitles'].keys())
                    if available_languages:
                        language = available_languages[0]
                        logger.info(f"Using detected language: {language}")
                        ydl_opts['subtitleslangs'] = [language]
                else:
                    # Fall back to 'en' if can't determine
                    language = 'en'
                    ydl_opts['subtitleslangs'] = [language]
        
        # Now download the subtitle in the selected language
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            ydl.extract_info(youtube_url, download=True)
            
            # Check actual downloaded files
            logger.info(f"Current directory files (after extraction): {os.listdir('.')}")
            
            # Look for the subtitle file with the specified language
            subtitle_files = [f for f in os.listdir('.')
                             if f.startswith(video_id) and any(ext in f for ext in ['.vtt', '.srt', '.ttml', '.json3'])]
            
            # If specific language requested, filter for that language
            if language:
                lang_subtitle_files = [f for f in subtitle_files if language in f]
                if lang_subtitle_files:
                    subtitle_files = lang_subtitle_files
            
            logger.info(f"Potential subtitle files: {subtitle_files}")
            
            if subtitle_files:
                # Process the first found subtitle file
                subtitle_file = subtitle_files[0]
                logger.info(f"Processing subtitle file: {subtitle_file}")
                
                with open(subtitle_file, 'r', encoding='utf-8') as f:
                    content = f.read()
                
                # Add format-specific parsing
                if subtitle_file.endswith('.json3'):
                    import json
                    subs = json.loads(content)
                    text = ' '.join([e['segs'][0]['utf8'] for e in subs['events'] if e.get('segs')])
                elif subtitle_file.endswith('.vtt'):
                    text = ' '.join(line.strip() for line in content.split('\n') 
                           if not line.startswith('WEBVTT') 
                           and '-->' not in line 
                           and not line.strip().isdigit()
                           and line.strip())
                elif subtitle_file.endswith('.srt'):
                    # Simple SRT parsing - skip timestamps and numbers
                    lines = []
                    for line in content.split('\n'):
                        if not line.strip().isdigit() and '-->' not in line and line.strip():
                            lines.append(line.strip())
                    text = ' '.join(lines)
                else:
                    text = f"Unsupported format: {subtitle_file}"
                
                # Clean up files to avoid cluttering the directory
                for f in subtitle_files:
                    try:
                        os.remove(f)
                    except:
                        logger.warning(f"Could not remove file: {f}")
                
                detected_language = subtitle_file.split('.')[-2] if '.' in subtitle_file else "unknown"
                return {"transcript": text, "language": detected_language}
            
            return {"transcript": f"No subtitle files found for {video_id}", "language": "none"}
    except Exception as e:
        logger.error(f"Error: {str(e)}", exc_info=True)
        raise HTTPException(status_code=500, detail=str(e))



# Define a global temporary download directory
global_download_dir = tempfile.mkdtemp()



class RateLimiter:
    def __init__(self, max_requests: int, time_window: timedelta):
        self.max_requests = max_requests
        self.time_window = time_window
        self.requests: Dict[str, list] = defaultdict(list)
    
    def _cleanup_old_requests(self, user_ip: str) -> None:
        """Remove requests that are outside the time window."""
        current_time = time.time()
        self.requests[user_ip] = [
            timestamp for timestamp in self.requests[user_ip]
            if current_time - timestamp < self.time_window.total_seconds()
        ]
    
    def is_rate_limited(self, user_ip: str) -> bool:
        """Check if the user has exceeded their rate limit."""
        self._cleanup_old_requests(user_ip)
        
        # Get current count after cleanup
        current_count = len(self.requests[user_ip])
        
        # Add current request timestamp (incrementing the count)
        current_time = time.time()
        self.requests[user_ip].append(current_time)
        
        # Check if user has exceeded the maximum requests
        return (current_count + 1) > self.max_requests
    
    def get_current_count(self, user_ip: str) -> int:
        """Get the current request count for an IP."""
        self._cleanup_old_requests(user_ip)
        return len(self.requests[user_ip])


# Initialize rate limiter with 100 requests per day
rate_limiter = RateLimiter(
    max_requests=20,
    time_window=timedelta(days=1)
)

def get_user_ip(request: Request) -> str:
    """Helper function to get user's IP address."""
    forwarded = request.headers.get("X-Forwarded-For")
    if forwarded:
        return forwarded.split(",")[0]
    return request.client.host


class ApiRotator:
    def __init__(self, apis):
        self.apis = apis
        self.last_successful_index = None

    def get_prioritized_apis(self):
        if self.last_successful_index is not None:
            # Move the last successful API to the front
            rotated_apis = (
                [self.apis[self.last_successful_index]] + 
                self.apis[:self.last_successful_index] + 
                self.apis[self.last_successful_index+1:]
            )
            return rotated_apis
        return self.apis

    def update_last_successful(self, index):
        self.last_successful_index = index

# In your function:
api_rotator = ApiRotator([
    "https://dwnld.nichind.dev",
    "https://cobalt-api.kwiatekmiki.com",
    "https://yt.edd1e.xyz/",
    "https://cobalt-api.ayo.tf",
    "https://cblt.fariz.dev"
])



async def get_track_download_url(video_url: str) -> str:
    apis = api_rotator.get_prioritized_apis()
    session = cloudscraper.create_scraper()  # Requires cloudscraper package
    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
    }

    for i, api_url in enumerate(apis):
        try:
            logger.info(f"Attempting to get download URL from: {api_url}")
            y_url = video_url
            response = session.post(
                api_url,
                timeout=20,
                json={"url": y_url, "videoQuality": "720", "filenameStyle": "pretty"},
                headers=headers
            )
            logger.info(f"Response status: {response.status_code}")
            logger.info(f"Response content: {response.content}")

            if response.headers.get('content-type', '').startswith('application/json'):
                json_response = response.json()
                error_code = json_response.get("error", {}).get("code", "")
                
                if error_code == "error.api.content.video.unavailable":
                    logger.warning(f"Video unavailable error from {api_url}")
                    break  # Only break for specific error
                
                if "url" in json_response:
                    api_rotator.update_last_successful(i)
                    return json_response["url"]
                
        except Exception as e:
            logger.error(f"Failed with {api_url}: {str(e)}")
            continue
    
    logger.error(f"No download URL found")
    return {"error": "Download URL not found"}


def jio_search(query: str, quality: str) -> str:
    try:
        # Construct the API URL
        api_url = f"https://saavn.dev/api/search/songs?query={query}"
        session = cloudscraper.create_scraper()
        # Make the API request
        response = session.get(api_url)
        # Check if the request was successful
        response.raise_for_status()
        # Get the data from the response
        data = response.json().get("data")
        if not data:
            logger.error("No data found in the response.")
            raise HTTPException(status_code=404, detail="No data found for the given query.")
        # Get the song results
        song_results = data.get("results")
        if not song_results or len(song_results) == 0:
            logger.error("No song results found in the response.")
            raise HTTPException(status_code=404, detail="No song results found for the given query.")
        # Iterate through each song result
        for song in song_results:
            download_urls = song.get("downloadUrl")
            if download_urls:
                for download_url in download_urls:
                    if download_url.get("quality") == quality:
                        return download_url.get("url")
        logger.error(f"No download URL found for quality {quality} in the search results for query {query}.")
        raise HTTPException(status_code=404, detail=f"No download URL found for quality {quality} in the search results for query {query}.")
    except cloudscraper.exceptions.CloudflareChallengeError as cf_err:
        logger.error(f"Cloudflare challenge error while searching for {query}: {cf_err}")
        raise HTTPException(status_code=503, detail="Cloudflare challenge failed")
    except HTTPException:
        # Re - raise the HTTPException if it's already raised
        raise
    except Exception as e:
        logger.error(f"Error while searching for {query}: {e}")
        raise HTTPException(status_code=500, detail=f"An error occurred while searching: {str(e)}")

        
def jio_fetch(url: str, quality: str) -> str:
    try:
        # Construct the API URL
        api_url = f"https://saavn.dev/api/songs?link={url}"
        session = cloudscraper.create_scraper()
        # Make the API request
        response = session.get(api_url)
        # Check if the request was successful
        response.raise_for_status()
        data = response.json()
        song_data = data.get("data")
        if not song_data or len(song_data) == 0:
            logger.error("No data found in the response.")
            raise HTTPException(status_code=404, detail="No data found for the given URL.")
        download_urls = song_data[0].get("downloadUrl")
        if not download_urls:
            logger.error("No download URLs found in the response.")
            raise HTTPException(status_code=404, detail="No download URLs found for the given song.")
        for download_url in download_urls:
            if download_url.get("quality") == quality:
                return download_url.get("url")
        logger.error(f"No download URL found for quality {quality}.")
        raise HTTPException(status_code=404, detail=f"No download URL found for quality {quality}.")
    except cloudscraper.exceptions.CloudflareChallengeError as cf_err:
        logger.error(f"Cloudflare challenge error while fetching {url}: {cf_err}")
        raise HTTPException(status_code=503, detail="Cloudflare challenge failed")
    except HTTPException:
        # Re - raise the HTTPException if it's already raised
        raise
    except Exception as e:
        logger.error(f"Error while fetching {url}: {e}")
        raise HTTPException(status_code=500, detail=f"An error occurred while fetching: {str(e)}")
        
# Define the request model
class JioDownloadRequest(BaseModel):
    url: str = None
    query: str = None
    quality: str = None
    
@app.post("/jio_dl")
async def jio_download(request: JioDownloadRequest):
    try:
        url = request.url
        query = request.query
        quality = request.quality
        if url and quality:
            logger.info(f'input url: {url}')
            download_url = jio_fetch(url, quality)
            return {"download_url": download_url}
        elif query:
            logger.info(f'input query: {query}')
            download_url = jio_search(query, quality)
            return {"download_url": download_url}
        else:
            logger.error("Missing 'url' and 'quality' or 'query' in request data.")
            raise HTTPException(status_code=400, detail="Missing 'url' and 'quality' or 'query' in request data")
    except HTTPException:
        # Re - raise the HTTPException if it's already raised
        raise
    except Exception as e:
        logger.error(f"Error in jio_download: {e}")
        raise HTTPException(status_code=500, detail=f"An error occurred during the operation: {str(e)}")
        

@app.post("/jio_dls")
async def jio_download(request: JioDownloadRequest):
    try:
        url = request.url
        quality = request.quality
        query = request.query
        if quality == '320kbps':
            return {
                "error": "Quality 320kbps is for Premium users only",
                "premium": "https://chrunos.com/premium-shortcuts/"
            }
        if url and quality:
            logger.info(f'input url: {url}')
            download_url = jio_fetch(url, quality)
            return {"download_url": download_url}
        elif query:
            logger.info(f'input query: {query}')
            download_url = jio_search(query, quality)
            return {"download_url": download_url}
        else:
            logger.error("Missing 'url' and 'quality' or 'query' in request data.")
            raise HTTPException(status_code=400, detail="Missing 'url' and 'quality' or 'query' in request data")
    except HTTPException:
        # Re - raise the HTTPException if it's already raised
        raise
    except Exception as e:
        logger.error(f"Error in jio_download: {e}")
        raise HTTPException(status_code=500, detail=f"An error occurred during the operation: {str(e)}")









EXTRACT_API = os.getenv("EXTRACT_API")
ALT_API = os.getenv("ALT_API")


def extract_video_info(video_url: str) -> str:
    api_urls = [f'{ALT_API}?url={video_url}', f'{EXTRACT_API}?url={video_url}']
    for api_url in api_urls:
        logger.info(api_url)
        session = cloudscraper.create_scraper()
        try:
            response = session.get(api_url, timeout=20)

            if response.status_code == 200:
                json_response = response.json()
                result = []
                if 'formats' in json_response:
                    for format_item in json_response['formats']:
                        format_url = format_item.get('url')
                        format_id = format_item.get('format_id')
                        p_cookies = format_item.get('cookies')
                        if format_id and format_url:
                            result.append({
                                "url": format_url,
                                "format_id": format_id,
                                "cookies": p_cookies
                            })
                    title = json_response.get('title')
                    logger.info(title)
                    if "pornhub.com" in video_url:
                        p_result = [item for item in result if 'hls' in item['format_id']]
                        return p_result
                    else:
                        if len(result) == 1:
                            new_item = {
                                "format_id": "This is Fake, Don't Choose This One",
                                "url": "none"
                            }
                            result.append(new_item)
                        return result
                else:
                    if 'url' in json_response:
                        d_url = json_response.get('url')
                        t_url = json_response.get('thumbnail')
                        result.append({
                            "url": d_url,
                            "format_id": "video"
                        })
                        result.append({
                            "url": t_url,
                            "format_id": "thumbnail"
                        })
                        return result
                    else:
                        return {"error": "No formats available"}
            else:
                logger.warning(f"Request failed with status code {response.status_code}, API: {api_url}")
        except Exception as e:
            logger.error(f"An error occurred: {e}")
    return {"error": "Both APIs failed to provide valid results."}


@app.post("/test")
async def test_download(request: Request):
    data = await request.json()
    video_url = data.get('url')
    response = extract_video_info(video_url)
    return response


@app.post("/hls")
async def download_hls_video(request: Request):
    data = await request.json()
    hls_url = data.get('url')

    timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
    output_template = str(Path(global_download_dir) / f'%(title)s_{timestamp}.%(ext)s')

    ydl_opts = {
        'format': 'best',
        'outtmpl': output_template,
        'quiet': True,
        'no_warnings': True,
        'noprogress': True,
        'merge_output_format': 'mp4'
    }
    try:
        await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([hls_url]))
    except Exception as e:
        return {"error": f"Download failed: {str(e)}"}

    downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
    if not downloaded_files:
        return {"error": "Download failed"}

    downloaded_file = downloaded_files[0]
    encoded_filename = urllib.parse.quote(downloaded_file.name)
    download_url = f"{BASE_URL}/file/{encoded_filename}"
    gc.collect()
    return {"url": download_url}




async def get_audio_download_url(track_id: str, quality: str) -> str:
    if quality == 'mp3':
        type = 'audio'
        quality = 128
    else:
        type = 'video'
    donwnload_url = f'https://chrunos-shadl.hf.space/yt/dl?url={track_id}&type={type}&quality={quality}'
    return donwnload_url
    

@app.post("/maxs")
async def download_high_quality_video(request: Request):
    user_ip = get_user_ip(request)
    
    if rate_limiter.is_rate_limited(user_ip):
        current_count = rate_limiter.get_current_count(user_ip)
        raise HTTPException(
            status_code=429,
            detail={
                "error": "You have exceeded the maximum number of requests per day. Please try again tomorrow.",
                "url": "https://t.me/chrunoss"
            }
        )

    
    data = await request.json()
    restricted_domain = "chrunos.com"
    video_url = data.get('url')
    quality = data.get('quality', '720')  # Default to 1080p if not specified
    logger.info(f'input url: {video_url}, {quality}')
    is_youtube_url = re.search(r'(youtube\.com|youtu\.be|instagram\.com)', video_url) is not None
    if video_url and restricted_domain in video_url:
        return {"error": "What is wrong with you?", "url": "https://t.me/chrunoss"}
     
    # Check if the requested quality is above 1080p
    if int(quality) > 720:
        error_message = "Quality above 720p is for Premium Members Only. Please check the URL for more information."
        help_url = "https://chrunos.com/premium-shortcuts/"  # Replace with your actual URL
        return {"error": error_message, "url": help_url}

    cookiefile = "firefox-cookies.txt"
    env_to_cookies_from_env("firefox-cookies.txt")

    timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
    output_template = str(Path(global_download_dir) / f'%(title).70s_{timestamp}.%(ext)s')

    # Convert quality string to height
    height_map = {
        '240': 240,
        '360': 360,
        '480': 480,
        '720': 720,
        '1080': 1080
    }
    max_height = height_map.get(quality, 720)  # Use the quality variable correctly

    # Determine format string based on quality
    format_str = f'bestvideo[height<={max_height}][vcodec^=avc]+bestaudio/best'

    ydl_opts = {
        'format': format_str,
        'outtmpl': output_template,
        'quiet': True,
        'no_warnings': True,
        'noprogress': True,
        'merge_output_format': 'mp4'
    }

    if is_youtube_url:
        ydl_opts["cookiefile"] = "firefox-cookies.txt"
        '''dl_url = await get_audio_download_url(video_url, quality)
        if dl_url and "http" in dl_url:
            return {"url": dl_url, "requests_remaining": rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)}
        else:
            return {
                "error": "Failed to Fetch the video."
                }
       '''
    # else:
    await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))

    downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp4"))
    if not downloaded_files:
        return {"error": "Download failed"}

    downloaded_file = downloaded_files[0]
    encoded_filename = urllib.parse.quote(downloaded_file.name)
    download_url = f"{BASE_URL}/file/{encoded_filename}"


    gc.collect()

    return {"url": download_url, "requests_remaining": rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)}








api_key_header = APIKeyHeader(name="X-API-Key")

# Store this securely in your environment variables
API_KEY = os.getenv("API_KEY")


async def verify_api_key(api_key: str = Security(api_key_header)):
    if api_key != API_KEY:
        raise HTTPException(
            status_code=403,
            detail="Invalid API key"
        )
    return api_key

@app.post("/audio")
async def download_audio(
    request: Request
    #api_key: str = Security(verify_api_key)
):
    user_ip = get_user_ip(request)
    
    if rate_limiter.is_rate_limited(user_ip):
        current_count = rate_limiter.get_current_count(user_ip)
        raise HTTPException(
            status_code=429,
            detail={
                "error": "You have exceeded the maximum number of requests per day. Please try again tomorrow.",
                "url": "https://t.me/chrunoss"
            }
        )
    data = await request.json()
    video_url = data.get('url')
    #cookiefile = "firefox-cookies.txt"
    #env_to_cookies_from_env("firefox-cookies.txt")

    timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
    output_template = str(Path(global_download_dir) / f'%(title).70s_{timestamp}.%(ext)s')

    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': output_template,
        'quiet': True,
        'no_warnings': True,
        'noprogress': True,
        #'cookiefile': cookiefile,
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192'
        }]
        
    }
    is_youtube_url = re.search(r'(youtube\.com|youtu\.be)', video_url) is not None
    if is_youtube_url:
        dl_url = await get_audio_download_url(video_url, 'mp3')
        if dl_url and "http" in dl_url:
            return {"url": dl_url, "requests_remaining": rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)}
        else:
            return {
                "error": "Failed to Fetch the video."
                }
    else:
        await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([video_url]))
    
        downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.*"))
        if not downloaded_files:
            return {"error": "Download failed"}
    
        downloaded_file = downloaded_files[0]
        encoded_filename = urllib.parse.quote(downloaded_file.name)
        download_url = f"{BASE_URL}/file/{encoded_filename}"
        gc.collect()
        return {"url": download_url, "requests_remaining": rate_limiter.max_requests - rate_limiter.get_current_count(user_ip)}

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

@app.post("/search")
async def search_and_download_song(request: Request,
    api_key: str = Security(verify_api_key)
):
    data = await request.json()
    song_name = data.get('songname')
    artist_name = data.get('artist')
    if artist_name:
        search_query = f"ytsearch:{song_name} {artist_name}"
    else:
        search_query = f"ytsearch:{song_name}"

    logging.info(f"Search query: {search_query}")
    cookiefile = "firefox-cookies.txt"
    env_to_cookies_from_env("firefox-cookies.txt")
    
    timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
    output_template = str(Path(global_download_dir) / f'%(title).70s_{timestamp}.%(ext)s')

    ydl_opts = {
        'format': 'bestaudio/best',
        'outtmpl': output_template,
        'quiet': True,
        'no_warnings': True,
        'noprogress': True,
        'postprocessors': [{
            'key': 'FFmpegExtractAudio',
            'preferredcodec': 'mp3',
            'preferredquality': '192'
        }],
        'cookiefile': cookiefile
    }

    try:
        logging.info("Starting yt-dlp search and download...")
        await run_in_threadpool(lambda: yt_dlp.YoutubeDL(ydl_opts).download([search_query]))
        logging.info("yt-dlp search and download completed")
    except yt_dlp.utils.DownloadError as e:
        error_message = str(e)
        logging.error(f"yt-dlp error: {error_message}")
        return JSONResponse(content={"error": error_message}, status_code=500)
    except Exception as e:
        error_message = str(e)
        logging.error(f"General error: {error_message}")
        return JSONResponse(content={"error": error_message}, status_code=500)
    
    downloaded_files = list(Path(global_download_dir).glob(f"*_{timestamp}.mp3"))
    if not downloaded_files:
        logging.error("Download failed: No MP3 files found")
        return JSONResponse(content={"error": "Download failed"}, status_code=500)

    downloaded_file = downloaded_files[0]
    encoded_filename = urllib.parse.quote(downloaded_file.name)
    download_url = f"{BASE_URL}/file/{encoded_filename}"
    logging.info(f"Download URL: {download_url}")

    # Log just before returning the response
    logging.info("Preparing to send response back to the client")
    gc.collect()
    return JSONResponse(content={"url": download_url}, status_code=200)

# Mount the static files directory
app.mount("/file", StaticFiles(directory=global_download_dir), name="downloads")







@app.middleware("http")
async def set_mime_type_middleware(request: Request, call_next):
    response = await call_next(request)
    if request.url.path.endswith(".mp4"):
        response.headers["Content-Type"] = "video/mp4"
    return response