Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,25 +1,58 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
app = FastAPI()
|
5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
return {"message": "Hello World"}
|
10 |
-
@app.get("/dailymo/")
|
11 |
-
async def read_item(reqUrl: str = 'https://www.dailymotion.com/video/x8pdo3p', vidFormat: str='http-720-0'):
|
12 |
-
|
13 |
-
video_url = reqUrl
|
14 |
-
desired_format = vidFormat
|
15 |
-
ydl_opts = {
|
16 |
-
'quiet': True,
|
17 |
-
'format': desired_format,
|
18 |
-
}
|
19 |
-
ydl = yt_dlp.YoutubeDL(ydl_opts)
|
20 |
-
info_dict = ydl.extract_info(video_url, download=False)
|
21 |
-
format_url = info_dict.get('url')
|
22 |
-
if format_url:
|
23 |
-
return {"url":format_url,"reqUrl":reqUrl,"vidFormat":vidFormat}
|
24 |
-
else:
|
25 |
-
return {"url":"Not found","reqUrl":reqUrl,"vidFormat":vidFormat}
|
|
|
1 |
+
firebase_config = {
|
2 |
+
"apiKey": "AIzaSyBy6tm3udmXYPZ216ggqs6P2Up2owC6U4Q",
|
3 |
+
"authDomain": "chalo-5e71c.firebaseapp.com",
|
4 |
+
"databaseURL": "https://chalo-5e71c-default-rtdb.asia-southeast1.firebasedatabase.app",
|
5 |
+
"projectId": "chalo-5e71c",
|
6 |
+
"storageBucket": "chalo-5e71c.appspot.com",
|
7 |
+
"messagingSenderId": "485358834771",
|
8 |
+
"appId": "1:485358834771:web:d4aa0d636433916a87a2a6"
|
9 |
+
}
|
10 |
+
from fastapi import FastAPI,BackgroundTasks , Request
|
11 |
+
import requests
|
12 |
+
from pyrebase import initialize_app
|
13 |
+
import httpx
|
14 |
app = FastAPI()
|
15 |
|
16 |
+
# Firebase configuration
|
17 |
+
async def keep_alive_request(receiver_url: str):
|
18 |
+
async with httpx.AsyncClient() as client:
|
19 |
+
response = await client.get(receiver_url)
|
20 |
+
print(f"Keep-alive request sent to {receiver_url}, Status Code: {response.status_code}")
|
21 |
+
|
22 |
+
|
23 |
+
firebase = initialize_app(firebase_config)
|
24 |
+
|
25 |
+
@app.get("/fetch_and_store_data")
|
26 |
+
def fetch_and_store_data(request: Request,url: str = "https://dailymo-api.onrender.com/dailymo/?reqUrl=https://dai.ly/k4Q1uMXJNjgzkHzEO2p&vidFormat=http-1080@60-0"):
|
27 |
+
try:
|
28 |
+
# Fetch data from the provided URL
|
29 |
+
receiver_url = str(request.base_url)
|
30 |
+
print(receiver_url)
|
31 |
+
res=0
|
32 |
+
while res!=200:
|
33 |
+
response = requests.get(url)
|
34 |
+
res=response.status_code
|
35 |
+
background_tasks = BackgroundTasks()
|
36 |
+
background_tasks.add_task(keep_alive_request, receiver_url)
|
37 |
+
print(response.status_code)
|
38 |
+
print(response.json())
|
39 |
+
data_to_store = {"a":"a"} # Assuming the response is in JSON format, adjust accordingly
|
40 |
+
|
41 |
+
# Write data to Firebase Realtime Database
|
42 |
+
db = firebase.database()
|
43 |
+
db.child('finally').set(response.json())
|
44 |
+
|
45 |
+
return {"status": "success", "message": "Data fetched and stored successfully." , "data_stored":response.json()}
|
46 |
+
|
47 |
+
except Exception as e:
|
48 |
+
return {"status": "error", "message": f"An error occurred: {str(e)}"}
|
49 |
+
@app.get("/fetch")
|
50 |
+
def fetch_and_store_data():
|
51 |
+
try:
|
52 |
+
# Fetch data from the provided URL
|
53 |
+
db = firebase.database()
|
54 |
+
response=db.child("finally").child("url").get()
|
55 |
+
return {"status": response.val()}
|
56 |
|
57 |
+
except Exception as e:
|
58 |
+
return {"status": "error", "message": f"An error occurred: {str(e)}"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|