Spaces:
Paused
Paused
Update test.py
Browse files
test.py
CHANGED
|
@@ -1,37 +1,35 @@
|
|
| 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://hfs302.serversicuro.cc/hls/,dnzpfec63dg4a3gyvapx72rcyqjwtaix6g6gsfexbdzt3uusvijhskjgwl5a,.urlset/master.m3u8"
|
| 8 |
-
m3u8_file_path = "myvideo.m3u8"
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
response = requests.get(url)
|
| 13 |
response.raise_for_status() # Raise an error for bad responses
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
with open(file_path, 'w') as f:
|
| 17 |
-
f.write(response.text)
|
| 18 |
|
| 19 |
@app.get("/")
|
| 20 |
async def startup_event():
|
| 21 |
-
"""Fetch
|
|
|
|
| 22 |
try:
|
| 23 |
-
|
|
|
|
| 24 |
except requests.RequestException as e:
|
| 25 |
print(f"Failed to fetch M3U8 file: {e}")
|
| 26 |
|
| 27 |
@app.get("/video-path/myvideo.m3u8")
|
| 28 |
async def serve_m3u8():
|
| 29 |
-
"""Serve the
|
| 30 |
-
if
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
return FileResponse(m3u8_file_path, media_type='application/vnd.apple.mpegurl')
|
| 35 |
|
| 36 |
if __name__ == "__main__":
|
| 37 |
import uvicorn
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException, Response
|
|
|
|
| 2 |
import requests
|
|
|
|
| 3 |
|
| 4 |
app = FastAPI()
|
| 5 |
m3u8_url = "https://hfs302.serversicuro.cc/hls/,dnzpfec63dg4a3gyvapx72rcyqjwtaix6g6gsfexbdzt3uusvijhskjgwl5a,.urlset/master.m3u8"
|
|
|
|
| 6 |
|
| 7 |
+
# Global variable to store M3U8 content
|
| 8 |
+
m3u8_content = None
|
| 9 |
+
|
| 10 |
+
def fetch_m3u8(url):
|
| 11 |
+
"""Fetch the M3U8 file from the URL and store it in memory."""
|
| 12 |
response = requests.get(url)
|
| 13 |
response.raise_for_status() # Raise an error for bad responses
|
| 14 |
+
return response.text
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
@app.get("/")
|
| 17 |
async def startup_event():
|
| 18 |
+
"""Fetch the M3U8 file on application startup."""
|
| 19 |
+
global m3u8_content
|
| 20 |
try:
|
| 21 |
+
global m3u8_content
|
| 22 |
+
m3u8_content = fetch_m3u8(m3u8_url)
|
| 23 |
except requests.RequestException as e:
|
| 24 |
print(f"Failed to fetch M3U8 file: {e}")
|
| 25 |
|
| 26 |
@app.get("/video-path/myvideo.m3u8")
|
| 27 |
async def serve_m3u8():
|
| 28 |
+
"""Serve the in-memory M3U8 content."""
|
| 29 |
+
if m3u8_content is None:
|
| 30 |
+
raise HTTPException(status_code=404, detail="M3U8 content not found")
|
| 31 |
+
|
| 32 |
+
return Response(content=m3u8_content, media_type='application/vnd.apple.mpegurl')
|
|
|
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|
| 35 |
import uvicorn
|