File size: 2,580 Bytes
8fc0b3a
 
 
 
 
 
 
 
 
 
 
0881ff6
 
8fc0b3a
 
23544b0
0881ff6
 
 
 
8fc0b3a
 
 
 
 
 
 
 
dee6194
 
 
8fc0b3a
2d707b4
8fc0b3a
 
 
 
 
 
2d707b4
8fc0b3a
 
 
 
 
0881ff6
 
8fc0b3a
 
0881ff6
8fc0b3a
 
 
 
 
 
 
 
 
0881ff6
8fc0b3a
 
 
23544b0
8fc0b3a
0881ff6
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
firebase_config = {
    "apiKey": "AIzaSyBy6tm3udmXYPZ216ggqs6P2Up2owC6U4Q",
    "authDomain": "chalo-5e71c.firebaseapp.com",
    "databaseURL": "https://chalo-5e71c-default-rtdb.asia-southeast1.firebasedatabase.app",
    "projectId": "chalo-5e71c",
    "storageBucket": "chalo-5e71c.appspot.com",
    "messagingSenderId": "485358834771",
    "appId": "1:485358834771:web:d4aa0d636433916a87a2a6"
}
from fastapi import FastAPI,BackgroundTasks , Request
import requests
import random
import string
from pyrebase import initialize_app
import httpx
app = FastAPI()
def gen_ran():
    length_of_string = 6
    random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=length_of_string))  
    return random_string     
# Firebase configuration
async def keep_alive_request(receiver_url: str):
    async with httpx.AsyncClient() as client:
        response = await client.get(receiver_url)
        print(f"Keep-alive request sent to {receiver_url}, Status Code: {response.status_code}")


firebase = initialize_app(firebase_config)
@app.get("/")
async def root():
    return {"message": "Hello World"}
@app.get("/fetch_and_store_data")
def fetch_and_store_data(request: Request,url: str = "https://dailymo-api.onrender.com/dailymo/?reqUrl=https://dai.ly/k4Q1uMXJNjgzkHzEO2p"):
    try:
        # Fetch data from the provided URL
        receiver_url = str(request.base_url)
        print(receiver_url)
        res=0
        while res!=200:
            response = requests.get(url+'&vidFormat=http-380-0')
            res=response.status_code
            background_tasks = BackgroundTasks()
            background_tasks.add_task(keep_alive_request, receiver_url)
            print(response.status_code)
        print(response.json())
        # data_to_store = {"a":"a"}  # Assuming the response is in JSON format, adjust accordingly
        add=gen_ran()
        # Write data to Firebase Realtime Database
        db = firebase.database()
        db.child('finally'+add).set(response.json())

        return {"status": "success", "message": "Data fetched and stored successfully." , "data_stored":response.json()}

    except Exception as e:
        return {"status": "error", "message": f"An error occurred: {str(e)}"}
@app.get("/fetch")
def fetch_and_store_data():
    try:
        # Fetch data from the provided URL
        
        db = firebase.database()
        response=db.child("finally").child("url").get()
        return {"status": response.val()}

    except Exception as e:
        return {"status": "error", "message": f"An error occurred: {str(e)}"}