mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Get books: Fix smashwords plugin not working because of website changes
Fixes #1918737 [smashwords search doesn't work](https://bugs.launchpad.net/calibre/+bug/1918737)
This commit is contained in:
parent
4cd2abd3c1
commit
33eac400fd
@ -1,7 +1,7 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from __future__ import absolute_import, division, print_function, unicode_literals
|
from __future__ import absolute_import, division, print_function, unicode_literals
|
||||||
|
|
||||||
store_version = 5 # Needed for dynamic plugin loading
|
store_version = 6 # Needed for dynamic plugin loading
|
||||||
|
|
||||||
__license__ = 'GPL 3'
|
__license__ = 'GPL 3'
|
||||||
__copyright__ = '2011, John Schember <john@nachtimwald.com>'
|
__copyright__ = '2011, John Schember <john@nachtimwald.com>'
|
||||||
@ -27,7 +27,7 @@ from calibre.gui2.store.search_result import SearchResult
|
|||||||
from calibre.gui2.store.web_store_dialog import WebStoreDialog
|
from calibre.gui2.store.web_store_dialog import WebStoreDialog
|
||||||
|
|
||||||
|
|
||||||
def search(query, max_results=10, timeout=60):
|
def search(query, max_results=10, timeout=60, save_raw=None):
|
||||||
url = 'https://www.smashwords.com/books/search?query=' + quote(query)
|
url = 'https://www.smashwords.com/books/search?query=' + quote(query)
|
||||||
|
|
||||||
br = browser()
|
br = browser()
|
||||||
@ -38,23 +38,24 @@ def search(query, max_results=10, timeout=60):
|
|||||||
|
|
||||||
counter = max_results
|
counter = max_results
|
||||||
with closing(br.open(url, timeout=timeout)) as f:
|
with closing(br.open(url, timeout=timeout)) as f:
|
||||||
doc = html.fromstring(f.read())
|
raw = f.read()
|
||||||
for data in doc.xpath('//div[@id="pageContent"]//div[@class="library-book"]'):
|
if save_raw:
|
||||||
|
with open(save_raw, 'wb') as r:
|
||||||
|
r.write(raw)
|
||||||
|
doc = html.fromstring(raw)
|
||||||
|
for data in doc.xpath('//div[@id="pageContent"]//div[contains(@class, "library-book")]'):
|
||||||
if counter <= 0:
|
if counter <= 0:
|
||||||
break
|
break
|
||||||
data = html.fromstring(html.tostring(data))
|
data = html.fromstring(html.tostring(data))
|
||||||
|
|
||||||
id = None
|
id_a = ''.join(data.xpath('//span[contains(@class, "library-title")]/a/@href'))
|
||||||
id_a = ''.join(data.xpath('//a[contains(@class, "library-title")]/@href'))
|
if not id_a:
|
||||||
if id_a:
|
|
||||||
id = id_a.split('/')[-1]
|
|
||||||
if not id:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
cover_url = ''.join(data.xpath('//img[contains(@class, "book-list-image")]/@src'))
|
cover_url = ''.join(data.xpath('//img[contains(@class, "book-list-image")]/@src'))
|
||||||
|
|
||||||
title = ''.join(data.xpath('.//a[contains(@class, "library-title")]/text()'))
|
title = ''.join(data.xpath('.//span[contains(@class, "library-title")]//text()'))
|
||||||
author = ''.join(data.xpath('.//a[@itemprop="author"]//text()'))
|
author = ''.join(data.xpath('.//span[contains(@class, "library-by-line")]/a//text()'))
|
||||||
|
|
||||||
price = ''.join(data.xpath('.//div[@class="subnote"]//text()'))
|
price = ''.join(data.xpath('.//div[@class="subnote"]//text()'))
|
||||||
if 'Price:' in price:
|
if 'Price:' in price:
|
||||||
@ -74,7 +75,7 @@ def search(query, max_results=10, timeout=60):
|
|||||||
s.title = title.strip()
|
s.title = title.strip()
|
||||||
s.author = author.strip()
|
s.author = author.strip()
|
||||||
s.price = price
|
s.price = price
|
||||||
s.detail_item = '/books/view/' + id.strip()
|
s.detail_item = id_a
|
||||||
s.drm = SearchResult.DRM_UNLOCKED
|
s.drm = SearchResult.DRM_UNLOCKED
|
||||||
|
|
||||||
yield s
|
yield s
|
||||||
@ -119,5 +120,5 @@ class SmashwordsStore(BasicStoreConfig, StorePlugin):
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import sys
|
import sys
|
||||||
for r in search(' '.join(sys.argv[1:])):
|
for r in search(' '.join(sys.argv[1:]), save_raw='/t/raw.html'):
|
||||||
print(r)
|
print(r)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user