Spaces:
Paused
Paused
| from fastapi import FastAPI, HTTPException, Response | |
| import requests | |
| from bs4 import BeautifulSoup | |
| from urllib.parse import urlparse, parse_qs,unquote,quote | |
| import re | |
| app = FastAPI() | |
| User_Agent= "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 OPR/111.0.0.0" | |
| # Global variable to store M3U8 content | |
| m3u8_content = None | |
| def fetch_m3u8(url): | |
| """Fetch the M3U8 file from the URL and store it in memory.""" | |
| response = requests.get(url,headers = {"User-Agent": User_Agent}) | |
| response.raise_for_status() # Raise an error for bad responses | |
| return response.text | |
| async def clone_m3u8(d: str = None): | |
| if d: | |
| try: | |
| d = unquote(d) | |
| m3u8_content = fetch_m3u8(d) | |
| return Response(content=m3u8_content, media_type='application/vnd.apple.mpegurl') | |
| except requests.RequestException as e: | |
| print(f"Failed to fetch M3U8 file: {e}") | |
| raise HTTPException(status_code=404, detail="M3U8 content not found") | |
| async def get_version(): | |
| #Extract the version from the main page of the site | |
| try: | |
| import json | |
| proxies = {} | |
| SC_DOMAIN = "family" | |
| random_headers = {} | |
| random_headers['Referer'] = f"https://streamingcommunity.{SC_DOMAIN}/" | |
| random_headers['Origin'] = f"https://streamingcommunity.{SC_DOMAIN}" | |
| base_url = f'https://streamingcommunity.{SC_DOMAIN}/richiedi-un-titolo' | |
| response = requests.get(base_url, headers=random_headers, allow_redirects = True, proxies = proxies) | |
| #Soup the response | |
| soup = BeautifulSoup(response.text, "lxml") | |
| # Extract version | |
| version = json.loads(soup.find("div", {"id": "app"}).get("data-page"))['version'] | |
| return version | |
| except Exception as e: | |
| print("Couldn't find the version",e) | |
| version = "65e52dcf34d64173542cd2dc6b8bb75b" | |
| return version | |
| async def get_film(): | |
| tid ="9207" | |
| proxies = {} | |
| version = await get_version() | |
| SC_DOMAIN = "family" | |
| random_headers = {} | |
| random_headers['Referer'] = "https://streamingcommunity.buzz/" | |
| random_headers['Origin'] = "https://streamingcommunity.buzz" | |
| random_headers['x-inertia'] = "true" | |
| random_headers['x-inertia-version'] = version | |
| random_headers['User-Agent'] = User_Agent | |
| random_headers['user-agent'] = User_Agent | |
| #Access the iframe | |
| url = f'https://streamingcommunity.{SC_DOMAIN}/iframe/{tid}' | |
| response = requests.get(url, headers=random_headers, allow_redirects=True, proxies = proxies) | |
| iframe = BeautifulSoup(response.text, 'lxml') | |
| #Get the link of iframe | |
| iframe = iframe.find('iframe').get("src") | |
| #Get the ID containted in the src of iframe | |
| vixid = iframe.split("/embed/")[1].split("?")[0] | |
| parsed_url = urlparse(iframe) | |
| query_params = parse_qs(parsed_url.query) | |
| random_headers['Referer'] = f"https://streamingcommunity.{SC_DOMAIN}/" | |
| random_headers['Origin'] = f"https://streamingcommunity.{SC_DOMAIN}" | |
| random_headers['x-inertia'] = "true" | |
| random_headers['x-inertia-version'] = version | |
| random_headers['User-Agent'] = User_Agent | |
| random_headers['user-agent'] = User_Agent | |
| #Get real token and expires by looking at the page in the iframe, vixcloud/embed | |
| resp = requests.get(iframe, headers = random_headers, allow_redirects=True, proxies = proxies) | |
| soup= BeautifulSoup(resp.text, "lxml") | |
| script = soup.find("body").find("script").text | |
| token = re.search(r"'token':\s*'(\w+)'", script).group(1) | |
| expires = re.search(r"'expires':\s*'(\d+)'", script).group(1) | |
| quality = re.search(r'"quality":(\d+)', script).group(1) | |
| #Example url https://vixcloud.co/playlist/231315?b=1&token=bce060eec3dc9d1965a5d258dc78c964&expires=1728995040&rendition=1080p | |
| url = f'https://vixcloud.co/playlist/{vixid}.m3u8?token={token}&expires={expires}' | |
| if 'canPlayFHD' in query_params: | |
| canPlayFHD = 'h=1' | |
| url += "&h=1" | |
| if 'b' in query_params: | |
| b = 'b=1' | |
| url += "&b=1" | |
| ''' | |
| if quality == "1080": | |
| if "&h" in url: | |
| url = url | |
| elif "&b" in url and quality == "1080": | |
| url = url.replace("&b=1","&h=1") | |
| elif quality == "1080" and "&b" and "&h" not in url: | |
| url = url + "&h=1" | |
| else: | |
| url = url + f"&token={token}" | |
| ''' | |
| url720 = f'https://vixcloud.co/playlist/{vixid}.m3u8' | |
| url = quote(url) | |
| return url,url720,quality | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) |