Spaces:
Paused
Paused
Create test.py
Browse files
test.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
import requests
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
m3u8_url = "https://vixcloud.co/playlist/247057?token=9b93a3831d50b0a6449a2b1b10e6a5bf&expires=1740138167&h=1"
|
| 8 |
+
m3u8_file_path = "video-path/myvideo.m3u8"
|
| 9 |
+
|
| 10 |
+
def fetch_and_save_m3u8(url, file_path):
|
| 11 |
+
"""Fetch the M3U8 file from the URL and save it to the local file system."""
|
| 12 |
+
response = requests.get(url)
|
| 13 |
+
response.raise_for_status() # Raise an error for bad responses
|
| 14 |
+
|
| 15 |
+
# Create directory if it doesn't exist
|
| 16 |
+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
| 17 |
+
|
| 18 |
+
with open(file_path, 'w') as f:
|
| 19 |
+
f.write(response.text)
|
| 20 |
+
|
| 21 |
+
@app.get("/")
|
| 22 |
+
async def startup_event():
|
| 23 |
+
"""Fetch and save the M3U8 file on application startup."""
|
| 24 |
+
try:
|
| 25 |
+
fetch_and_save_m3u8(m3u8_url, m3u8_file_path)
|
| 26 |
+
except requests.RequestException as e:
|
| 27 |
+
print(f"Failed to fetch M3U8 file: {e}")
|
| 28 |
+
|
| 29 |
+
@app.get("/video-path/myvideo.m3u8")
|
| 30 |
+
async def serve_m3u8():
|
| 31 |
+
"""Serve the saved M3U8 file."""
|
| 32 |
+
if not os.path.exists(m3u8_file_path):
|
| 33 |
+
# Return 404 if the file isn't found
|
| 34 |
+
raise HTTPException(status_code=404, detail="M3U8 file not found")
|
| 35 |
+
|
| 36 |
+
return FileResponse(m3u8_file_path, media_type='application/vnd.apple.mpegurl')
|
| 37 |
+
|
| 38 |
+
if __name__ == "__main__":
|
| 39 |
+
import uvicorn
|
| 40 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|