Skip to content
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

Bandcamp fix #730

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions catalog/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"use_local_response",
"RetryDownloader",
"BasicDownloader",
"BasicDownloader2",
"CachedDownloader",
"ProxiedDownloader",
"BasicImageDownloader",
Expand Down
61 changes: 60 additions & 1 deletion catalog/common/downloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from urllib.parse import quote

import filetype
import httpx
import requests
from django.conf import settings
from django.core.cache import cache
Expand Down Expand Up @@ -103,6 +104,14 @@ def xml(self):
return etree.fromstring(self.content, base_url=self.url)


class DownloaderResponse2(httpx.Response):
def html(self):
return html.fromstring(self.content.decode("utf-8"))

def xml(self):
return etree.fromstring(self.content, base_url=str(self.url))


class DownloadError(Exception):
def __init__(self, downloader, msg=None):
self.url = downloader.url
Expand Down Expand Up @@ -163,7 +172,9 @@ def validate_response(self, response) -> int:
else:
return RESPONSE_INVALID_CONTENT

def _download(self, url) -> Tuple[DownloaderResponse | MockResponse | None, int]:
def _download(
self, url
) -> Tuple[DownloaderResponse | DownloaderResponse2 | MockResponse | None, int]:
try:
if not _mock_mode:
resp = cast(
Expand All @@ -187,6 +198,54 @@ def _download(self, url) -> Tuple[DownloaderResponse | MockResponse | None, int]
self.logs.append(
{"response_type": response_type, "url": url, "exception": None}
)
return resp, response_type
except RequestException as e:
self.logs.append(
{"response_type": RESPONSE_NETWORK_ERROR, "url": url, "exception": e}
)
return None, RESPONSE_NETWORK_ERROR

def download(self):
resp, self.response_type = self._download(self.url)
if self.response_type == RESPONSE_OK and resp:
return resp
else:
raise DownloadError(self)


class BasicDownloader2(BasicDownloader):
def validate_response(self, response) -> int:
if response is None:
return RESPONSE_NETWORK_ERROR
elif response.status_code == 200:
return RESPONSE_OK
else:
return RESPONSE_INVALID_CONTENT

def _download(self, url):
try:
if not _mock_mode:
resp = cast(
DownloaderResponse2,
httpx.get(url, headers=self.headers, timeout=self.get_timeout()),
)
resp.__class__ = DownloaderResponse2
if settings.DOWNLOADER_SAVEDIR:
try:
with open(
settings.DOWNLOADER_SAVEDIR + "/" + get_mock_file(url),
"w",
encoding="utf-8",
) as fp:
fp.write(resp.text)
except Exception:
logger.warning("Save downloaded data failed.")
else:
resp = MockResponse(self.url)
response_type = self.validate_response(resp)
self.logs.append(
{"response_type": response_type, "url": url, "exception": None}
)

return resp, response_type
except RequestException as e:
Expand Down
2 changes: 1 addition & 1 deletion catalog/sites/bandcamp.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def validate_url_fallback(cls, url):
return False

def scrape(self):
content = BasicDownloader(self.url).download().html()
content = BasicDownloader2(self.url).download().html()
try:
title = self.query_str(content, "//h2[@class='trackTitle']/text()")
artist = [
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ dev-dependencies = [
"djlint~=1.34.1",
"isort~=5.13.2",
"lxml-stubs>=0.5.1",
"pyright>=1.1.371",
"pyright>=1.1.373",
"ruff",
"mkdocs-material>=9.5.25",
]
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.lock
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ pygments==2.18.0
# via mkdocs-material
pymdown-extensions==10.8.1
# via mkdocs-material
pyright==1.1.371
pyright==1.1.373
python-dateutil==2.9.0.post0
# via dateparser
# via django-auditlog
Expand Down