LoremPizza commited on
Commit
331094b
·
verified ·
1 Parent(s): 38797d5

Update test.py

Browse files
Files changed (1) hide show
  1. test.py +16 -18
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
- 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
-
16
- with open(file_path, 'w') as f:
17
- f.write(response.text)
18
 
19
  @app.get("/")
20
  async def startup_event():
21
- """Fetch and save the M3U8 file on application startup."""
 
22
  try:
23
- fetch_and_save_m3u8(m3u8_url, m3u8_file_path)
 
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 saved M3U8 file."""
30
- if not os.path.exists(m3u8_file_path):
31
- # Return 404 if the file isn't found
32
- raise HTTPException(status_code=404, detail="M3U8 file not found")
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