Skip to content

Commit

Permalink
Barnes & Noble - changes for user-agent and request headers
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwidude68 committed May 11, 2024
1 parent 9830f19 commit 9ecbbbb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 4 additions & 0 deletions barnes_noble/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Barnes & Noble Change Log

## [1.5.5] - 2024-05-11
### Fixed
- Changes to the B&N website causing timeouts requiring Chrome user-agent > 80 and some specific accept headers.

## [1.5.4] - 2024-04-02
### Added
- Add support for pasting web urls as identifiers from the B&N website.
Expand Down
18 changes: 14 additions & 4 deletions barnes_noble/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
__license__ = 'GPL v3'
__copyright__ = '2011, Grant Drake'

import time, re
import time, re, random
from six import text_type as unicode
from six.moves.urllib.parse import quote
try:
Expand All @@ -15,6 +15,7 @@
from lxml.html import fromstring

from calibre import as_unicode
from calibre.constants import numeric_version as calibre_version
from calibre.ebooks.metadata import check_isbn
from calibre.ebooks.metadata.sources.base import Source
from calibre.utils.icu import lower
Expand All @@ -26,7 +27,7 @@ class BarnesNoble(Source):
name = 'Barnes & Noble'
description = 'Downloads metadata and covers from Barnes & Noble'
author = 'Grant Drake'
version = (1, 5, 4)
version = (1, 5, 5)
minimum_calibre_version = (2, 0, 0)

ID_NAME = 'barnesnoble'
Expand All @@ -39,7 +40,7 @@ class BarnesNoble(Source):

BASE_URL = 'https://search.barnesandnoble.com'
BROWSE_URL = 'https://www.barnesandnoble.com'
SEARCH_URL = 'https://www.barnesandnoble.com/s?'
SEARCH_URL = 'https://www.barnesandnoble.com/s'

def config_widget(self):
'''
Expand All @@ -48,6 +49,11 @@ def config_widget(self):
from calibre_plugins.barnes_noble.config import ConfigWidget
return ConfigWidget(self)

@property
def user_agent(self):
# May 2024 - B&N started getting picky about the user agent, rejecting Chrome version 80 which was the calibre default.
return 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'

def get_book_url(self, identifiers):
barnes_noble_id = identifiers.get(self.ID_NAME, None)
if barnes_noble_id:
Expand Down Expand Up @@ -89,7 +95,7 @@ def create_query(self, log, title=None, authors=None, identifiers={}):
tokens += [quote(t.encode('utf-8') if isinstance(t, unicode) else t) for t in author_tokens]
if len(tokens) == 0:
return None
return BarnesNoble.SEARCH_URL + 'store=book&keyword=' + '+'.join(tokens)
return BarnesNoble.SEARCH_URL + '/' + '%20'.join(tokens).lower()

def get_cached_cover_url(self, identifiers):
url = None
Expand Down Expand Up @@ -153,6 +159,8 @@ def identify(self, log, result_queue, abort, title=None, authors=None,
multiple_results_found = False
try:
log.info('Querying: %s' % query)
br.set_current_header('Accept','*/*')
br.set_current_header('Accept-Encoding','gzip, deflate, br')
response = br.open_novisit(query, timeout=timeout)
# Check whether we got redirected to a book page.
# If we did, will use the url.
Expand Down Expand Up @@ -361,6 +369,8 @@ def download_cover(self, log, result_queue, abort,
if abort.is_set():
return
br = self.browser
br.set_current_header('Accept','*/*')
br.set_current_header('Accept-Encoding','gzip, deflate, br')
log('Downloading cover from:', cached_url)
try:
cdata = br.open_novisit(cached_url, timeout=timeout).read()
Expand Down
2 changes: 2 additions & 0 deletions barnes_noble/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def __init__(self, url, result_queue, browser, log, relevance, plugin, timeout=2
self.log, self.timeout = log, timeout
self.relevance, self.plugin = relevance, plugin
self.browser = browser.clone_browser()
self.browser.set_current_header('Accept','*/*')
self.browser.set_current_header('Accept-Encoding','gzip, deflate, br')
self.cover_url = self.barnes_noble_id = self.isbn = None

def run(self):
Expand Down

0 comments on commit 9ecbbbb

Please sign in to comment.