Skip to content

Commit

Permalink
fix: video m3u8 URL retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
ivansaul committed Nov 23, 2024
1 parent 91d8d21 commit af9470f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
23 changes: 17 additions & 6 deletions src/facilito/collectors/video.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import re

from playwright.async_api import BrowserContext

from ..constants import VIDEO_M3U8_URL
from ..constants import VIDEO_BASE_URL, VIDEO_M3U8_URL
from ..errors import VideoError
from ..models import Video
from ..utils import is_video
Expand All @@ -9,6 +11,7 @@
async def fetch_video(context: BrowserContext, url: str) -> Video:
VIDEO_ID_SELECTOR = "input[name='video_id']"
COURSE_ID_SELECTOR = "input[name='course_id']"
M3U8_PATTERN = r"\/hls\/.*?\.m3u8"

if not is_video(url):
raise VideoError()
Expand All @@ -17,13 +20,21 @@ async def fetch_video(context: BrowserContext, url: str) -> Video:
page = await context.new_page()
await page.goto(url)

course_id = await page.locator(COURSE_ID_SELECTOR).first.get_attribute("value")
video_id = await page.locator(VIDEO_ID_SELECTOR).first.get_attribute("value")
if m3u8_urls := re.findall(M3U8_PATTERN, await page.content()):
url = VIDEO_BASE_URL + m3u8_urls[0]

else:
course_id = await page.locator(COURSE_ID_SELECTOR).first.get_attribute(
"value"
)
video_id = await page.locator(VIDEO_ID_SELECTOR).first.get_attribute(
"value"
)

if not video_id or not course_id:
raise VideoError()
if not video_id or not course_id:
raise VideoError()

url = VIDEO_M3U8_URL.format(course_id=course_id, video_id=video_id)
url = VIDEO_M3U8_URL.format(course_id=course_id, video_id=video_id)

except Exception:
raise VideoError()
Expand Down
7 changes: 2 additions & 5 deletions src/facilito/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
LOGIN_URL = BASE_URL + "/users/sign_in"
HOME_URL = BASE_URL + "/home"

VIDEO_BASE_URL = "https://video-storage.codigofacilito.com"
VIDEO_M3U8_URL = (
"https://video-storage.codigofacilito.com"
+ "/hls"
+ "/{course_id}"
+ "/{video_id}"
+ "/playlist.m3u8"
VIDEO_BASE_URL + "/hls" + "/{course_id}" + "/{video_id}" + "/playlist.m3u8"
)

# --- Session directory ---
Expand Down

0 comments on commit af9470f

Please # to comment.