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

apply contenttyping learnings from doab-check #1034

Merged
merged 3 commits into from
Mar 18, 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
6 changes: 5 additions & 1 deletion core/loaders/harvest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import re
import time
from urllib.parse import urljoin, quote
from urllib.parse import quote, unquote, urljoin, urlparse, urlsplit, urlunsplit

import requests

Expand Down Expand Up @@ -405,6 +405,10 @@ def harvest_oapen(ebook):
if is_bookshop_url(ebook.url):
return set_bookshop(ebook)
if '/bitstream/' in ebook.url:
if "%" in ebook.url:
(scheme, netloc, path, query, fragment) = urlsplit(ebook.url)
newpath = quote(unquote(path), encoding='latin1')
ebook.url = urlunsplit((scheme, netloc, newpath, query, fragment))
return make_dl_ebook(ebook.url, ebook, user_agent=settings.USER_AGENT)
return None, 0

Expand Down
41 changes: 33 additions & 8 deletions core/models/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import re
import requests
import time
from urllib.parse import urlparse
from urllib.parse import quote, unquote, urlparse, urlsplit, urlunsplit

from django.apps import apps
from django.conf import settings
Expand Down Expand Up @@ -73,16 +73,41 @@ def __init__(self):
self.last_call = dict()

def content_type(self, url):
def handle_ude(url, ude):
# fallback for non-ascii, non-utf8 bytes in redirect location
(scheme, netloc, path, query, fragment) = urlsplit(url)
newpath = quote(unquote(path), encoding='latin1')
url = urlunsplit((scheme, netloc, newpath, query, fragment))
try:
r = requests.get(url, allow_redirects=True)
except:
logger.error('Error processing %s after unicode error', url)
return '', ''
try:
r = requests.head(url, allow_redirects=True)
if r.status_code == 405:
r = requests.get(url)
elif r.status_code == 404:
logger.error('File not found (404) for %s', url)
return '404', ''
return r.headers.get('content-type', ''), r.headers.get('content-disposition', '')
try:
r = requests.head(url, allow_redirects=True)
if r.status_code == 405:
try:
r = requests.get(url)
except UnicodeDecodeError as ude:
if 'utf-8' in str(ude):
return handle_ude(url, ude)
except UnicodeDecodeError as ude:
if 'utf-8' in str(ude):
return handle_ude(url, ude)
except requests.exceptions.SSLError:
try:
r = requests.get(url, verify=False)
except:
logger.error('Error processing %s verification off', url)
return '', ''
except:
logger.error('Error processing %s', url)
return '', ''
if r.status_code == 404:
logger.error('File not found (404) for %s', url)
return '404', ''
return r.headers.get('content-type', ''), r.headers.get('content-disposition', '')

def calc_type(self, url):
logger.info(url)
Expand Down
2 changes: 1 addition & 1 deletion settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@
LOGOUT_URL = "/accounts/logout/"
LOGIN_ERROR_URL = '/accounts/login-error/'

USER_AGENT = "unglue.it.bot v0.0.1 <https://unglue.it>"
USER_AGENT = "unglue.it.bot v0.0.1 (https://unglue.it)"

# The amount of the transaction that Gluejar takes
GLUEJAR_COMMISSION = 0.06
Expand Down