Enhance HLS proxy functionality and improve caching mechanism
- Updated AGENTS.md to clarify dlp.py module usage and segment handling. - Modified README.md to include ALLOW_LOCAL configuration for testing. - Refactored app.py to streamline HLS proxy logic and improve error handling. - Enhanced dlp.py to optimize caching and segment retrieval processes. - Updated player.html to ensure proper JSON formatting for proxy URLs. - Improved test_integration.py to validate HLS segment proxying and added test for Pornhub HLS extraction. - Adjusted test_proxy.py to reflect changes in caching functions and data structure.
This commit is contained in:
@@ -108,9 +108,25 @@ def test_hls_playlist_proxy(test_servers):
|
||||
def test_hls_segment_proxy(test_servers):
|
||||
"""Test proxying HLS segment"""
|
||||
video_url = f"http://127.0.0.1:{TEST_HTTP_PORT}/index.m3u8"
|
||||
proxy_url = f"http://127.0.0.1:{SERVER_PORT}/hls?url={urllib.parse.quote(video_url, safe='')}&path=segment000.ts"
|
||||
|
||||
response = requests.get(proxy_url, timeout=10)
|
||||
|
||||
# First get the rewritten playlist to extract the segment URL
|
||||
playlist_url = f"http://127.0.0.1:{SERVER_PORT}/hls?url={urllib.parse.quote(video_url, safe='')}"
|
||||
playlist_response = requests.get(playlist_url, timeout=10)
|
||||
assert playlist_response.status_code == 200
|
||||
|
||||
# Extract the segment path from the playlist (it's after the path= parameter)
|
||||
for line in playlist_response.text.split("\n"):
|
||||
if line.startswith("/hls?"):
|
||||
from urllib.parse import urlparse, parse_qs
|
||||
parsed = urlparse(line)
|
||||
params = parse_qs(parsed.query)
|
||||
if "path" in params:
|
||||
segment_path = params["path"][0]
|
||||
break
|
||||
|
||||
# Now request the segment using the path from the playlist
|
||||
segment_url = f"http://127.0.0.1:{SERVER_PORT}/hls?url={urllib.parse.quote(video_url, safe='')}&path={urllib.parse.quote(segment_path, safe='')}"
|
||||
response = requests.get(segment_url, timeout=10)
|
||||
assert response.status_code == 200
|
||||
assert len(response.content) > 0
|
||||
print("HLS segment proxy: OK")
|
||||
@@ -135,5 +151,19 @@ def test_index_page(test_servers):
|
||||
print("Index page: OK")
|
||||
|
||||
|
||||
@pytest.mark.skip(reason="External URL test - run manually to verify pornhub support")
|
||||
def test_pornhub_hls_extraction():
|
||||
"""Test that pornhub HLS URLs are extracted correctly"""
|
||||
import dlp
|
||||
dlp._session_cache.clear()
|
||||
dlp._cache_timestamps.clear()
|
||||
|
||||
# Test with actual pornhub URL
|
||||
url = "https://rt.pornhub.com/view_video.php?viewkey=69bc20ee15710"
|
||||
hls_url = dlp.get_stream_info(url)["hls_url"]
|
||||
assert hls_url and "m3u8" in hls_url
|
||||
print(f"PornHub HLS URL: {hls_url[:100]}...")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
pytest.main([__file__, "-v", "-s"])
|
||||
+8
-5
@@ -55,18 +55,21 @@ class TestCacheMechanics:
|
||||
dlp._session_cache.clear()
|
||||
dlp._cache_timestamps.clear()
|
||||
|
||||
test_data = {"test": "data"}
|
||||
dlp._set_cached_session("http://test.com/video", test_data)
|
||||
test_data = {"title": "Test Video", "thumbnail": "http://test.com/thumb.jpg", "hls_url": "http://test.com/stream.m3u8"}
|
||||
dlp._set_cached_info("http://test.com/video", test_data)
|
||||
|
||||
cached = dlp._get_cached_session("http://test.com/video")
|
||||
assert cached == test_data
|
||||
cached = dlp._get_cached_info("http://test.com/video")
|
||||
assert cached is not None
|
||||
assert cached["title"] == "Test Video"
|
||||
assert cached["thumbnail"] == "http://test.com/thumb.jpg"
|
||||
assert cached["hls_url"] == "http://test.com/stream.m3u8"
|
||||
|
||||
def test_cache_expiry(self):
|
||||
dlp.CACHE_TTL = 1
|
||||
dlp._session_cache.clear()
|
||||
dlp._cache_timestamps.clear()
|
||||
|
||||
dlp._set_cached_session("http://test.com/video", {"data": "test"})
|
||||
dlp._set_cached_info("http://test.com/video", {"data": "test"})
|
||||
import time
|
||||
time.sleep(1.1)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user