File size: 2,896 Bytes
897ca92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from characterai import PyCAI as AnotherAPI
import requests
import re 
import json
import logging

app = FastAPI()

class VideoLinkExtractor:
    """Class for extracting video links from HTML content."""
    
    def __init__(self, html_content):
        self.html_content = html_content

    def clean_str(self, s):
        """Cleans and returns a JSON-encoded string."""
        try:
            return json.loads(f'{{"text": "{s}"}}')['text']
        except json.JSONDecodeError as e:
            logging.error(f"Error decoding JSON: {e}")
            return None

    def get_link(self, regex):
        """Extracts and returns link based on the provided regex."""
        match = re.search(regex, self.html_content)
        return self.clean_str(match.group(1)) if match else None

    def get_title(self):
        """Extracts and returns the title from HTML content."""
        return self.get_link(r'<title>(.*?)<\/title>')

@app.get("/AnotherAPI/{api_key}/FB_Downloader/")
async def get_video_links(url: str, api_key: str,):
    API = AnotherAPI(api_key)    
    API.chat.new_chat('csTC3hw0Fnj1Whnl0uV1Nb3_oYIillMQtdBH5NEl0Gs')
    if not url:
        return {"success": False, "message": "Please provide the URL"}
    
    headers = {
        'sec-fetch-user': '?1',
        'sec-ch-ua-mobile': '?0',
        'sec-fetch-site': 'none',
        'sec-fetch-dest': 'document',
        'sec-fetch-mode': 'navigate',
        'cache-control': 'max-age=0',
        'authority': 'www.facebook.com',
        'upgrade-insecure-requests': '1',
        'accept-language': 'en-GB,en;q=0.9,tr-TR;q=0.8,tr;q=0.7,en-US;q=0.6',
        'sec-ch-ua': '"Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36',
        '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'
    }

    try:
        r = requests.get(url, headers=headers)
        r.raise_for_status()
        extractor = VideoLinkExtractor(r.text)

        data = {
            "success": True,
            "title": extractor.get_title(),
            "links": {} 
        }

        sd_link = extractor.get_link(r'browser_native_sd_url":"([^"]+)"')
        if sd_link:
            data["links"]["Download Low Quality"] = sd_link + "&dl=1"

        hd_link = extractor.get_link(r'browser_native_hd_url":"([^"]+)"')
        if hd_link:
            data["links"]["Download High Quality"] = hd_link + "&dl=1"

        return data
    
    except requests.RequestException as e:
        logging.error(f"Request failed: {e}")
        return {"success": False, "message": str(e)}
    
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="127.0.0.1", port=8100)