New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[extractor/iwara] Fix and improve authentication #7137
[extractor/iwara] Fix and improve authentication #7137
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do not subclass IwaraUserIE
and IwaraPlaylistIE
from the concrete IwaraIE
class. Revert to subclassing from IwaraBaseIE
, and instead move the _perform_login
and _prepare_auth_header
methods into the base class.
After moving the methods into the base class, I think _perform_login
should be changed like so:
def _perform_login(self, username, password):
+ if self._USERTOKEN and self._MEDIATOKEN:
+ return
- if self.cache.load(self._NETRC_MACHINE, username) and self._get_media_token():
+ elif self.cache.load(self._NETRC_MACHINE, username) and self._get_media_token():
self.write_debug('Skipping logging in')
return
This will prevent a duplicate login/media token request when going from a playlist extractor to the video extractor
You could also check after loading the user token from cache if it is expired: diff --git a/yt_dlp/extractor/iwara.py b/yt_dlp/extractor/iwara.py
index 1749dcd1a..64105c39b 100644
--- a/yt_dlp/extractor/iwara.py
+++ b/yt_dlp/extractor/iwara.py
@@ -2,15 +2,18 @@
import urllib.parse
import hashlib
import json
+import time
from .common import InfoExtractor
from ..utils import (
ExtractorError,
OnDemandPagedList,
int_or_none,
+ jwt_decode_hs256,
mimetype2ext,
qualities,
traverse_obj,
+ try_call,
unified_timestamp,
)
@@ -27,7 +30,8 @@ def _get_user_token(self, invalidate=False):
username, password = self._get_login_info()
IwaraBaseIE._USERTOKEN = username and self.cache.load(self._NETRC_MACHINE, username)
- if not IwaraBaseIE._USERTOKEN or invalidate:
+ expiry = try_call(lambda: jwt_decode_hs256(IwaraBaseIE._USERTOKEN)['exp']) or 0
+ if not IwaraBaseIE._USERTOKEN or invalidate or expiry <= time.time():
IwaraBaseIE._USERTOKEN = self._download_json(
'https://api.iwara.tv/user/login', None, note='Logging in',
data=json.dumps({ |
This should be applied in the last commit. |
@toomyzoom I pushed some changes to improve and simplify the authentication code. Now tokens are always invalidated based on expiration time, and the extractor will refresh both tokens as needed. I've tested the changes with and without an account and haven't had any issue. Let me know if you have any problems with it |
Closes yt-dlp#7035, Closes yt-dlp#7207 Authored by: toomyzoom
IMPORTANT: PRs without the template will be CLOSED
Description of your pull request and other information
This is the same issue in #6671 where access token is needed for some NSFW video. Update to allow user profile and playlist extractors to use the same access token.
Also has fix to renew expired user token in the cache (#7207)
Fixes #7035, Fixes #7207
Template
Before submitting a pull request make sure you have:
In order to be accepted and merged into yt-dlp each piece of code must be in public domain or released under Unlicense. Check all of the following options that apply:
What is the purpose of your pull request?
Copilot Summary
🤖 Generated by Copilot at 64c1ecb
Summary
🔒📦✅
Improve
IwaraIE
and related extractors. This pull request enhances the authentication and extraction logic for theIwaraIE
,IwaraUserIE
, andIwaraPlaylistIE
extractors, which handle videos from the Iwara website. It also adds a new test case and a helper method for the API headers.Walkthrough
_prepare_auth_header
inIwaraIE
class (link, link)IwaraIE
class as the parent class forIwaraUserIE
andIwaraPlaylistIE
classes to inherit the new helper method (link, link)_prepare_auth_header
method to API requests for user, playlist, and playlist information extraction inyt_dlp/extractor/iwara.py
to support private and restricted videos (link, link, link)IwaraUserIE
class to extract videos from a user profile page (link)