Move video url extraction logic to crawler

This commit is contained in:
I-Al-Istannen
2020-05-30 00:14:08 +02:00
parent b969a1854a
commit 821c7ade26
2 changed files with 53 additions and 52 deletions

View File

@@ -3,6 +3,7 @@ Contains an ILIAS crawler alongside helper functions.
"""
import datetime
import json
import logging
import re
from enum import Enum
@@ -380,22 +381,44 @@ class IliasCrawler:
video_path: Path = Path(parent_path, title)
video_url = self._abs_url_from_link(link)
# The video had a direct download button we can use instead
if direct_download:
LOGGER.debug("Using direct download for video %r", str(video_path))
return [IliasDownloadInfo(
video_path,
self._abs_url_from_link(link),
modification_time
)]
return [IliasDownloadInfo(video_path, video_url, modification_time)]
return [IliasDownloadInfo(
video_path,
self._abs_url_from_link(link),
modification_time,
unpack_video=True
self._crawl_video_url_from_play_link(video_url),
modification_time
)]
def _crawl_video_url_from_play_link(self, play_url: str) -> Callable[[], Optional[str]]:
def inner() -> Optional[str]:
# Fetch the actual video page. This is a small wrapper page initializing a javscript
# player. Sadly we can not execute that JS. The actual video stream url is nowhere
# on the page, but defined in a JS object inside a script tag, passed to the player
# library.
# We do the impossible and RegEx the stream JSON object out of the page's HTML source
video_page_soup = soupify(self._session.get(play_url))
regex: re.Pattern = re.compile(
r"({\"streams\"[\s\S]+?),\s*{\"paella_config_file", re.IGNORECASE
)
json_match = regex.search(str(video_page_soup))
if json_match is None:
PRETTY.warning(f"Could not find json stream info for {play_url!r}")
return None
json_str = json_match.group(1)
# parse it
json_object = json.loads(json_str)
# and fetch the video url!
video_url = json_object["streams"][0]["sources"]["mp4"][0]["src"]
return video_url
return inner
def _crawl_exercises(self, element_path: Path, url: str) -> List[IliasDownloadInfo]:
"""
Crawl files offered for download in exercises.