Skip to content

Commit

Permalink
Add cookies always to yt-dlp not only if they are needed for moodle.
Browse files Browse the repository at this point in the history
  • Loading branch information
C0D3D3V committed Mar 14, 2023
1 parent fa82df3 commit ef9560c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 13 additions & 11 deletions moodle_dl/downloader/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,9 @@ async def get_head_infos(self, dl_url: str) -> HeadInfo:
logging.warning('[%d] Head request for external file failed with unexpected error', self.task_id)
raise head_err from None

async def download_using_yt_dlp(self, dl_url: str, infos: HeadInfo, delete_if_successful: bool, use_cookies: bool):
async def download_using_yt_dlp(self, dl_url: str, infos: HeadInfo, delete_if_successful: bool):
"""
@param delete_if_successful: Deletes the tmp file if download was successful
@param use_cookies: Adds the cookies to the requests
@return: False if the page should be downloaded anyway; True if yt-dlp has processed the URL and we are done
"""
# We try to limit the filename to < 250 chars
Expand All @@ -391,7 +390,7 @@ async def download_using_yt_dlp(self, dl_url: str, infos: HeadInfo, delete_if_su

ydl_opts.update(self.opts.yt_dlp_options)

if use_cookies and self.opts.cookies_text is not None:
if self.opts.cookies_text is not None:
ydl_opts.update({'cookiefile': StringIO(self.opts.cookies_text)})

ydl = yt_dlp.YoutubeDL(ydl_opts)
Expand Down Expand Up @@ -478,15 +477,15 @@ async def download_using_external_downloader(self, dl_url: str, external_dl_cmd:

self.file.saved_to = str(Path(self.destination) / self.filename)

async def external_download_url(self, add_token: bool, delete_if_successful: bool, use_cookies: bool):
async def external_download_url(self, add_token: bool, delete_if_successful: bool, needs_moodle_cookies: bool):
"""
Use only for "external" shortcut/URL files.
It tests whether a URL refers to a file, that is not an HTML web page then downloads it.
Otherwise an attempt will be made to download it using yt-dlp.
@param add_token: Adds the ws-token to the url
@param delete_if_successful: Deletes the tmp file if download was successful
@param use_cookies: Adds the cookies to the requests
@param needs_moodle_cookies: For this URL moodle cookies are required
In case of an failure an exception will be raised
"""
url_to_download = self.file.content_fileurl
Expand All @@ -499,8 +498,10 @@ async def external_download_url(self, add_token: bool, delete_if_successful: boo
# If temporary file is not needed delete it as soon as possible
PT.remove_file(self.file.saved_to)

if use_cookies and self.opts.cookies_text is None:
# Without cookies we can not proceed
if needs_moodle_cookies and self.opts.cookies_text is None:
# Without Moodle cookies we should not continue
# We assume that there are Moodle cookies in the cookie file, if one exists.
# TODO: Perhaps explicitly check if Moodle cookies are set
raise ValueError(
'Moodle cookies are missing. Set a private token so that moodle-dl can obtain moodle cookies'
)
Expand All @@ -523,7 +524,6 @@ async def external_download_url(self, add_token: bool, delete_if_successful: boo
dl_url=url_to_download,
infos=infos,
delete_if_successful=delete_if_successful,
use_cookies=use_cookies,
)
if yt_dlp_processed:
return
Expand Down Expand Up @@ -716,16 +716,18 @@ async def real_run(self) -> bool:
await self.create_html_file()

elif self.file.module_modname.startswith('index_mod'):
await self.external_download_url(add_token=True, delete_if_successful=True, use_cookies=False)
await self.external_download_url(add_token=True, delete_if_successful=True, needs_moodle_cookies=False)

elif self.file.module_modname.startswith('cookie_mod'):
await self.external_download_url(add_token=False, delete_if_successful=True, use_cookies=True)
await self.external_download_url(add_token=False, delete_if_successful=True, needs_moodle_cookies=True)

elif self.file.module_modname.startswith('url') and not self.file.content_fileurl.startswith('data:'):
# Create a shortcut and maybe downloading it
await self.create_shortcut()
if self.opts.download_linked_files and not self.is_filtered_external_domain():
await self.external_download_url(add_token=False, delete_if_successful=False, use_cookies=False)
await self.external_download_url(
add_token=False, delete_if_successful=False, needs_moodle_cookies=False
)

elif self.file.content_fileurl.startswith('data:'):
await self.create_data_url_file()
Expand Down
2 changes: 1 addition & 1 deletion moodle_dl/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.3.1.2'
__version__ = '2.3.1.3'

0 comments on commit ef9560c

Please sign in to comment.