use custom HTTPBasicAuth class

to support LazyPrompt as password
and to generate the Authorization header only once
instead of for every request
pull/4906/head
Mike Fährmann 10 months ago
parent bdebe4597a
commit e256434c9e
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -36,7 +36,7 @@ class DanbooruExtractor(BaseExtractor):
username, api_key = self._get_auth_info()
if username:
self.log.debug("Using HTTP Basic Auth for user '%s'", username)
self.session.auth = (username, api_key)
self.session.auth = util.HTTPBasicAuth(username, api_key)
def skip(self, num):
pages = num // self.per_page

@ -1239,7 +1239,7 @@ class DeviantartOAuthAPI():
self.log.info("Requesting public access token")
data = {"grant_type": "client_credentials"}
auth = (self.client_id, self.client_secret)
auth = util.HTTPBasicAuth(self.client_id, self.client_secret)
response = self.extractor.request(
url, method="POST", data=data, auth=auth, fatal=False)
data = response.json()

@ -183,7 +183,7 @@ class OAuthBase(Extractor):
}
if auth:
auth = (client_id, client_secret)
auth = util.HTTPBasicAuth(client_id, client_secret)
else:
auth = None
data["client_id"] = client_id

@ -9,7 +9,7 @@
"""Extractors for https://pixeldrain.com/"""
from .common import Extractor, Message
from .. import text
from .. import text, util
BASE_PATTERN = r"(?:https?://)?pixeldrain\.com"
@ -23,7 +23,7 @@ class PixeldrainExtractor(Extractor):
def _init(self):
api_key = self.config("api-key")
if api_key:
self.session.auth = ("", api_key)
self.session.auth = util.HTTPBasicAuth("", api_key)
def parse_datetime(self, date_string):
return text.parse_datetime(

@ -423,9 +423,10 @@ class RedditAPI():
"grants/installed_client"),
"device_id": "DO_NOT_TRACK_THIS_DEVICE"}
auth = util.HTTPBasicAuth(self.client_id, "")
response = self.extractor.request(
url, method="POST", headers=self.headers,
data=data, auth=(self.client_id, ""), fatal=False)
data=data, auth=auth, fatal=False)
data = response.json()
if response.status_code != 200:

@ -490,6 +490,19 @@ CODES = {
}
class HTTPBasicAuth():
__slots__ = ("authorization",)
def __init__(self, username, password):
self.authorization = b"Basic " + binascii.b2a_base64(
username.encode("latin1") + b":" + str(password).encode("latin1")
)[:-1]
def __call__(self, request):
request.headers["Authorization"] = self.authorization
return request
class LazyPrompt():
__slots__ = ()

Loading…
Cancel
Save