mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Get books: Fix amazon.fr not working because of website changes. Fixes #1907067 [No books from Amazon.fr](https://bugs.launchpad.net/calibre/+bug/1907067)
This commit is contained in:
parent
9c6acb0550
commit
628faf593a
@ -3,7 +3,7 @@
|
|||||||
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
|
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
|
||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
store_version = 15 # Needed for dynamic plugin loading
|
store_version = 16 # Needed for dynamic plugin loading
|
||||||
|
|
||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
try:
|
try:
|
||||||
@ -11,7 +11,7 @@ try:
|
|||||||
except ImportError:
|
except ImportError:
|
||||||
from urllib import urlencode
|
from urllib import urlencode
|
||||||
|
|
||||||
from lxml import html
|
from lxml import html, etree
|
||||||
|
|
||||||
from PyQt5.Qt import QUrl
|
from PyQt5.Qt import QUrl
|
||||||
|
|
||||||
@ -21,7 +21,7 @@ from calibre.gui2.store import StorePlugin
|
|||||||
from calibre.gui2.store.search_result import SearchResult
|
from calibre.gui2.store.search_result import SearchResult
|
||||||
|
|
||||||
SEARCH_BASE_URL = 'https://www.amazon.fr/s/'
|
SEARCH_BASE_URL = 'https://www.amazon.fr/s/'
|
||||||
SEARCH_BASE_QUERY = {'url': 'search-alias=digital-text'}
|
SEARCH_BASE_QUERY = {'i': 'digital-text'}
|
||||||
BY = 'de'
|
BY = 'de'
|
||||||
KINDLE_EDITION = 'Format Kindle'
|
KINDLE_EDITION = 'Format Kindle'
|
||||||
DETAILS_URL = 'https://amazon.fr/dp/'
|
DETAILS_URL = 'https://amazon.fr/dp/'
|
||||||
@ -38,7 +38,7 @@ def search_amazon(query, max_results=10, timeout=60,
|
|||||||
write_html_to=None,
|
write_html_to=None,
|
||||||
base_url=SEARCH_BASE_URL,
|
base_url=SEARCH_BASE_URL,
|
||||||
base_query=SEARCH_BASE_QUERY,
|
base_query=SEARCH_BASE_QUERY,
|
||||||
field_keywords='field-keywords'
|
field_keywords='k'
|
||||||
):
|
):
|
||||||
uquery = base_query.copy()
|
uquery = base_query.copy()
|
||||||
uquery[field_keywords] = query
|
uquery[field_keywords] = query
|
||||||
@ -58,52 +58,30 @@ def search_amazon(query, max_results=10, timeout=60,
|
|||||||
with open(write_html_to, 'wb') as f:
|
with open(write_html_to, 'wb') as f:
|
||||||
f.write(raw)
|
f.write(raw)
|
||||||
doc = html.fromstring(raw)
|
doc = html.fromstring(raw)
|
||||||
try:
|
for result in doc.xpath('//div[contains(@class, "s-result-list")]//div[@data-index and @data-asin]'):
|
||||||
results = doc.xpath('//div[@id="atfResults" and @class]')[0]
|
kformat = ''.join(result.xpath('.//a[contains(text(), "{}")]//text()'.format(KINDLE_EDITION)))
|
||||||
except IndexError:
|
|
||||||
return
|
|
||||||
|
|
||||||
if 's-result-list-parent-container' in results.get('class', ''):
|
|
||||||
data_xpath = "descendant-or-self::li[@class and contains(concat(' ', normalize-space(@class), ' '), ' s-result-item ')]"
|
|
||||||
format_xpath = './/a[@title="%s"]/@title' % KINDLE_EDITION
|
|
||||||
asin_xpath = '@data-asin'
|
|
||||||
cover_xpath = "descendant-or-self::img[@class and contains(concat(' ', normalize-space(@class), ' '), ' s-access-image ')]/@src"
|
|
||||||
title_xpath = "descendant-or-self::h2[@class and contains(concat(' ', normalize-space(@class), ' '), ' s-access-title ')]//text()"
|
|
||||||
author_xpath = './/span[starts-with(text(), "%s ")]/following-sibling::span//text()' % BY
|
|
||||||
price_xpath = 'descendant::span[contains(@class, "s-price")]//text()'
|
|
||||||
else:
|
|
||||||
return
|
|
||||||
|
|
||||||
for data in doc.xpath(data_xpath):
|
|
||||||
if counter <= 0:
|
|
||||||
break
|
|
||||||
|
|
||||||
# Even though we are searching digital-text only Amazon will still
|
# Even though we are searching digital-text only Amazon will still
|
||||||
# put in results for non Kindle books (author pages). Se we need
|
# put in results for non Kindle books (author pages). Se we need
|
||||||
# to explicitly check if the item is a Kindle book and ignore it
|
# to explicitly check if the item is a Kindle book and ignore it
|
||||||
# if it isn't.
|
# if it isn't.
|
||||||
format = ''.join(data.xpath(format_xpath))
|
if 'kindle' not in kformat.lower():
|
||||||
if 'kindle' not in format.lower():
|
continue
|
||||||
|
asin = result.get('data-asin')
|
||||||
|
if not asin:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# We must have an asin otherwise we can't easily reference the
|
cover_url = ''.join(result.xpath('.//img/@src'))
|
||||||
# book later.
|
title = etree.tostring(result.xpath('.//h2')[0], method='text', encoding='unicode')
|
||||||
asin = data.xpath(asin_xpath)
|
adiv = result.xpath('.//div[contains(@class, "a-color-secondary")]')[0]
|
||||||
if asin:
|
aparts = etree.tostring(adiv, method='text', encoding='unicode').split()
|
||||||
asin = asin[0]
|
idx = aparts.index(BY)
|
||||||
else:
|
author = ' '.join(aparts[idx+1:]).split('|')[0].strip()
|
||||||
continue
|
price = ''
|
||||||
|
for span in result.xpath('.//span[contains(@class, "a-price")]/span[contains(@class, "a-offscreen")]'):
|
||||||
cover_url = ''.join(data.xpath(cover_xpath))
|
q = ''.join(span.xpath('./text()'))
|
||||||
|
if q:
|
||||||
title = ''.join(data.xpath(title_xpath))
|
price = q
|
||||||
author = ''.join(data.xpath(author_xpath))
|
break
|
||||||
try:
|
|
||||||
author = author.split('by ', 1)[1].split(" (")[0]
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
price = ''.join(data.xpath(price_xpath))
|
|
||||||
|
|
||||||
counter -= 1
|
counter -= 1
|
||||||
|
|
||||||
@ -111,8 +89,8 @@ def search_amazon(query, max_results=10, timeout=60,
|
|||||||
s.cover_url = cover_url.strip()
|
s.cover_url = cover_url.strip()
|
||||||
s.title = title.strip()
|
s.title = title.strip()
|
||||||
s.author = author.strip()
|
s.author = author.strip()
|
||||||
s.price = price.strip()
|
|
||||||
s.detail_item = asin.strip()
|
s.detail_item = asin.strip()
|
||||||
|
s.price = price.strip()
|
||||||
s.formats = 'Kindle'
|
s.formats = 'Kindle'
|
||||||
|
|
||||||
yield s
|
yield s
|
||||||
|
@ -22,6 +22,8 @@ from calibre.gui2.store.search_result import SearchResult
|
|||||||
|
|
||||||
SEARCH_BASE_URL = 'https://www.amazon.com/s/'
|
SEARCH_BASE_URL = 'https://www.amazon.com/s/'
|
||||||
SEARCH_BASE_QUERY = {'i': 'digital-text'}
|
SEARCH_BASE_QUERY = {'i': 'digital-text'}
|
||||||
|
BY = 'by'
|
||||||
|
KINDLE_EDITION = 'Kindle Edition'
|
||||||
DETAILS_URL = 'https://amazon.com/dp/'
|
DETAILS_URL = 'https://amazon.com/dp/'
|
||||||
STORE_LINK = 'https://www.amazon.com/Kindle-eBooks'
|
STORE_LINK = 'https://www.amazon.com/Kindle-eBooks'
|
||||||
DRM_SEARCH_TEXT = 'Simultaneous Device Usage'
|
DRM_SEARCH_TEXT = 'Simultaneous Device Usage'
|
||||||
@ -57,7 +59,7 @@ def search_amazon(query, max_results=10, timeout=60,
|
|||||||
f.write(raw)
|
f.write(raw)
|
||||||
doc = html.fromstring(raw)
|
doc = html.fromstring(raw)
|
||||||
for result in doc.xpath('//div[contains(@class, "s-result-list")]//div[@data-index and @data-asin]'):
|
for result in doc.xpath('//div[contains(@class, "s-result-list")]//div[@data-index and @data-asin]'):
|
||||||
kformat = ''.join(result.xpath('.//a[contains(text(), "Kindle Edition")]//text()'))
|
kformat = ''.join(result.xpath('.//a[contains(text(), "{}")]//text()'.format(KINDLE_EDITION)))
|
||||||
# Even though we are searching digital-text only Amazon will still
|
# Even though we are searching digital-text only Amazon will still
|
||||||
# put in results for non Kindle books (author pages). Se we need
|
# put in results for non Kindle books (author pages). Se we need
|
||||||
# to explicitly check if the item is a Kindle book and ignore it
|
# to explicitly check if the item is a Kindle book and ignore it
|
||||||
@ -72,7 +74,7 @@ def search_amazon(query, max_results=10, timeout=60,
|
|||||||
title = etree.tostring(result.xpath('.//h2')[0], method='text', encoding='unicode')
|
title = etree.tostring(result.xpath('.//h2')[0], method='text', encoding='unicode')
|
||||||
adiv = result.xpath('.//div[contains(@class, "a-color-secondary")]')[0]
|
adiv = result.xpath('.//div[contains(@class, "a-color-secondary")]')[0]
|
||||||
aparts = etree.tostring(adiv, method='text', encoding='unicode').split()
|
aparts = etree.tostring(adiv, method='text', encoding='unicode').split()
|
||||||
idx = aparts.index('by')
|
idx = aparts.index(BY)
|
||||||
author = ' '.join(aparts[idx+1:]).split('|')[0].strip()
|
author = ' '.join(aparts[idx+1:]).split('|')[0].strip()
|
||||||
price = ''
|
price = ''
|
||||||
for span in result.xpath('.//span[contains(@class, "a-price")]/span[contains(@class, "a-offscreen")]'):
|
for span in result.xpath('.//span[contains(@class, "a-price")]/span[contains(@class, "a-offscreen")]'):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user