From e8d5f07a27f3dc065ffd2f449e30e154deae0d9a Mon Sep 17 00:00:00 2001 From: Alex Stanev Date: Mon, 18 Jul 2011 02:26:08 +0300 Subject: [PATCH 1/5] Working plugion for chitanka.info integration into calibre --- src/calibre/customize/builtins.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 7b43f2844a..33eae37b58 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -1446,6 +1446,16 @@ class StoreZixoStore(StoreBase): headquarters = 'PL' formats = ['PDF, ZIXO'] +class StoreChitankaStore(StoreBase): + name = u'Моята библиотека' + author = 'Alex Stanev' + description = u'Независим сайт за DRM свободна литература на български език' + actual_plugin = 'calibre.gui2.store.stores.chitanka_plugin:ChitankaStore' + + drm_free_only = True + headquarters = 'BG' + formats = ['FB2', 'EPUB', 'TXT', 'SFB'] + plugins += [ StoreArchiveOrgStore, StoreAmazonKindleStore, @@ -1483,7 +1493,8 @@ plugins += [ StoreWHSmithUKStore, StoreWizardsTowerBooksStore, StoreWoblinkStore, - StoreZixoStore + StoreZixoStore, + StoreChitankaStore ] # }}} From f16f42490f4ecb508dd8934d64d6f36e8945f461 Mon Sep 17 00:00:00 2001 From: Alex Stanev Date: Mon, 18 Jul 2011 02:29:41 +0300 Subject: [PATCH 2/5] the plugin itself --- .../gui2/store/stores/chitanka_plugin.py | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/calibre/gui2/store/stores/chitanka_plugin.py diff --git a/src/calibre/gui2/store/stores/chitanka_plugin.py b/src/calibre/gui2/store/stores/chitanka_plugin.py new file mode 100644 index 0000000000..a384dbcfe4 --- /dev/null +++ b/src/calibre/gui2/store/stores/chitanka_plugin.py @@ -0,0 +1,87 @@ +# -*- coding: utf-8 -*- + +from __future__ import (unicode_literals, division, absolute_import, print_function) + +__license__ = 'GPL 3' +__copyright__ = '2011, Alex Stanev ' +__docformat__ = 'restructuredtext en' + +import re +import urllib +from contextlib import closing + +from lxml import html + +from PyQt4.Qt import QUrl + +from calibre import browser, url_slash_cleaner +from calibre.gui2 import open_url +from calibre.gui2.store import StorePlugin +from calibre.gui2.store.basic_config import BasicStoreConfig +from calibre.gui2.store.search_result import SearchResult +from calibre.gui2.store.web_store_dialog import WebStoreDialog + +class ChitankaStore(BasicStoreConfig, StorePlugin): + + def open(self, parent=None, detail_item=None, external=False): + url = 'http://chitanka.info' + + if external or self.config.get('open_external', False): + if detail_item: + url = url + detail_item + open_url(QUrl(url_slash_cleaner(url))) + else: + detail_url = None + if detail_item: + detail_url = url + detail_item + d = WebStoreDialog(self.gui, url, parent, detail_url) + d.setWindowTitle(self.name) + d.set_tags(self.config.get('tags', '')) + d.exec_() + + def search(self, query, max_results=10, timeout=60): + + url = 'http://chitanka.info/search?q=' + urllib.quote(query) #urllib.quote(query.encode('utf-8')) + + br = browser() + + counter = max_results + with closing(br.open(url, timeout=timeout)) as f: + ff = unicode(f.read(), 'utf-8') + doc = html.fromstring(ff) + + for data in doc.xpath('//ul[@class="superlist booklist"]/li'): + if counter <= 0: + break + + id = ''.join(data.xpath('.//a[@class="booklink"]/@href')) + if not id: + continue + + cover_url = ''.join(data.xpath('.//a[@class="booklink"]/img/@src')) + title = ''.join(data.xpath('.//a[@class="booklink"]/i/text()')) + author = ''.join(data.xpath('.//span[@class="bookauthor"]/a/text()')) + fb2 = ''.join(data.xpath('.//a[@class="dl dl-fb2"]/@href')) + epub = ''.join(data.xpath('.//a[@class="dl dl-epub"]/@href')) + txt = ''.join(data.xpath('.//a[@class="dl dl-txt"]/@href')) + #remove .zip extensions + if fb2.find('.zip') <> -1: + fb2 = fb2[:fb2.find('.zip')] + if epub.find('.zip') <> -1: + epub = epub[:epub.find('.zip')] + if txt.find('.zip') <> -1: + txt = txt[:txt.find('.zip')] + + counter -= 1 + + s = SearchResult() + s.cover_url = cover_url + s.title = title.strip() + s.author = author.strip() + s.detail_item = id.strip() + s.drm = SearchResult.DRM_UNLOCKED + s.downloads['FB2'] = 'http://chitanka.info' + fb2.strip() + s.downloads['EPUB'] = 'http://chitanka.info' + epub.strip() + s.downloads['TXT'] = 'http://chitanka.info' + txt.strip() + s.formats = 'FB2, EPUB, TXT, SFB' + yield s From ff6324ea526dd08f1f097dfa8aa63a72d0361e65 Mon Sep 17 00:00:00 2001 From: Alex Stanev Date: Mon, 18 Jul 2011 15:34:22 +0300 Subject: [PATCH 3/5] Added author name search --- .../gui2/store/stores/chitanka_plugin.py | 73 ++++++++++++++++--- 1 file changed, 63 insertions(+), 10 deletions(-) diff --git a/src/calibre/gui2/store/stores/chitanka_plugin.py b/src/calibre/gui2/store/stores/chitanka_plugin.py index a384dbcfe4..e714d426e8 100644 --- a/src/calibre/gui2/store/stores/chitanka_plugin.py +++ b/src/calibre/gui2/store/stores/chitanka_plugin.py @@ -41,14 +41,15 @@ class ChitankaStore(BasicStoreConfig, StorePlugin): def search(self, query, max_results=10, timeout=60): - url = 'http://chitanka.info/search?q=' + urllib.quote(query) #urllib.quote(query.encode('utf-8')) - - br = browser() - + base_url = 'http://chitanka.info' + url = base_url + '/search?q=' + urllib.quote(query) counter = max_results + + # search for book title + br = browser() with closing(br.open(url, timeout=timeout)) as f: - ff = unicode(f.read(), 'utf-8') - doc = html.fromstring(ff) + f = unicode(f.read(), 'utf-8') + doc = html.fromstring(f) for data in doc.xpath('//ul[@class="superlist booklist"]/li'): if counter <= 0: @@ -64,7 +65,8 @@ class ChitankaStore(BasicStoreConfig, StorePlugin): fb2 = ''.join(data.xpath('.//a[@class="dl dl-fb2"]/@href')) epub = ''.join(data.xpath('.//a[@class="dl dl-epub"]/@href')) txt = ''.join(data.xpath('.//a[@class="dl dl-txt"]/@href')) - #remove .zip extensions + + # remove .zip extensions if fb2.find('.zip') <> -1: fb2 = fb2[:fb2.find('.zip')] if epub.find('.zip') <> -1: @@ -80,8 +82,59 @@ class ChitankaStore(BasicStoreConfig, StorePlugin): s.author = author.strip() s.detail_item = id.strip() s.drm = SearchResult.DRM_UNLOCKED - s.downloads['FB2'] = 'http://chitanka.info' + fb2.strip() - s.downloads['EPUB'] = 'http://chitanka.info' + epub.strip() - s.downloads['TXT'] = 'http://chitanka.info' + txt.strip() + s.downloads['FB2'] = base_url + fb2.strip() + s.downloads['EPUB'] = base_url + epub.strip() + s.downloads['TXT'] = base_url + txt.strip() s.formats = 'FB2, EPUB, TXT, SFB' yield s + + # search for author names + for data in doc.xpath('//ul[@class="superlist"][1]/li'): + author_url = ''.join(data.xpath('.//a[contains(@href,"/person/")]/@href')) + if counter <= 0: + break + + br2 = browser() + with closing(br2.open(base_url + author_url, timeout=timeout)) as f: + if counter <= 0: + break + f = unicode(f.read(), 'utf-8') + doc2 = html.fromstring(f) + + # search for book title + for data in doc2.xpath('//ul[@class="superlist booklist"]/li'): + if counter <= 0: + break + + id = ''.join(data.xpath('.//a[@class="booklink"]/@href')) + if not id: + continue + + cover_url = ''.join(data.xpath('.//a[@class="booklink"]/img/@src')) + title = ''.join(data.xpath('.//a[@class="booklink"]/i/text()')) + author = ''.join(data.xpath('.//span[@class="bookauthor"]/a/text()')) + fb2 = ''.join(data.xpath('.//a[@class="dl dl-fb2"]/@href')) + epub = ''.join(data.xpath('.//a[@class="dl dl-epub"]/@href')) + txt = ''.join(data.xpath('.//a[@class="dl dl-txt"]/@href')) + + # remove .zip extensions + if fb2.find('.zip') <> -1: + fb2 = fb2[:fb2.find('.zip')] + if epub.find('.zip') <> -1: + epub = epub[:epub.find('.zip')] + if txt.find('.zip') <> -1: + txt = txt[:txt.find('.zip')] + + counter -= 1 + + s = SearchResult() + s.cover_url = cover_url + s.title = title.strip() + s.author = author.strip() + s.detail_item = id.strip() + s.drm = SearchResult.DRM_UNLOCKED + s.downloads['FB2'] = base_url + fb2.strip() + s.downloads['EPUB'] = base_url + epub.strip() + s.downloads['TXT'] = base_url + txt.strip() + s.formats = 'FB2, EPUB, TXT, SFB' + yield s From 17ab15dabe5a1c1f6ee1e20d0195aff7ed4f21dc Mon Sep 17 00:00:00 2001 From: Alex Stanev Date: Tue, 19 Jul 2011 09:11:08 +0300 Subject: [PATCH 4/5] Fixed compare and ordering --- src/calibre/customize/builtins.py | 24 +++++++++---------- .../gui2/store/stores/chitanka_plugin.py | 14 +++++------ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 33eae37b58..652b5cb335 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -1181,6 +1181,16 @@ class StoreBeWriteStore(StoreBase): headquarters = 'US' formats = ['EPUB', 'MOBI', 'PDF'] +class StoreChitankaStore(StoreBase): + name = u'Моята библиотека' + author = 'Alex Stanev' + description = u'Независим сайт за DRM свободна литература на български език' + actual_plugin = 'calibre.gui2.store.stores.chitanka_plugin:ChitankaStore' + + drm_free_only = True + headquarters = 'BG' + formats = ['FB2', 'EPUB', 'TXT', 'SFB'] + class StoreDieselEbooksStore(StoreBase): name = 'Diesel eBooks' description = u'Instant access to over 2.4 million titles from hundreds of publishers including Harlequin, HarperCollins, John Wiley & Sons, McGraw-Hill, Simon & Schuster and Random House.' @@ -1446,16 +1456,6 @@ class StoreZixoStore(StoreBase): headquarters = 'PL' formats = ['PDF, ZIXO'] -class StoreChitankaStore(StoreBase): - name = u'Моята библиотека' - author = 'Alex Stanev' - description = u'Независим сайт за DRM свободна литература на български език' - actual_plugin = 'calibre.gui2.store.stores.chitanka_plugin:ChitankaStore' - - drm_free_only = True - headquarters = 'BG' - formats = ['FB2', 'EPUB', 'TXT', 'SFB'] - plugins += [ StoreArchiveOrgStore, StoreAmazonKindleStore, @@ -1465,6 +1465,7 @@ plugins += [ StoreBNStore, StoreBeamEBooksDEStore, StoreBeWriteStore, + StoreChitankaStore, StoreDieselEbooksStore, StoreEbookNLStore, StoreEbookscomStore, @@ -1493,8 +1494,7 @@ plugins += [ StoreWHSmithUKStore, StoreWizardsTowerBooksStore, StoreWoblinkStore, - StoreZixoStore, - StoreChitankaStore + StoreZixoStore ] # }}} diff --git a/src/calibre/gui2/store/stores/chitanka_plugin.py b/src/calibre/gui2/store/stores/chitanka_plugin.py index e714d426e8..2ad236adec 100644 --- a/src/calibre/gui2/store/stores/chitanka_plugin.py +++ b/src/calibre/gui2/store/stores/chitanka_plugin.py @@ -67,11 +67,11 @@ class ChitankaStore(BasicStoreConfig, StorePlugin): txt = ''.join(data.xpath('.//a[@class="dl dl-txt"]/@href')) # remove .zip extensions - if fb2.find('.zip') <> -1: + if fb2.find('.zip') != -1: fb2 = fb2[:fb2.find('.zip')] - if epub.find('.zip') <> -1: + if epub.find('.zip') != -1: epub = epub[:epub.find('.zip')] - if txt.find('.zip') <> -1: + if txt.find('.zip') != -1: txt = txt[:txt.find('.zip')] counter -= 1 @@ -93,7 +93,7 @@ class ChitankaStore(BasicStoreConfig, StorePlugin): author_url = ''.join(data.xpath('.//a[contains(@href,"/person/")]/@href')) if counter <= 0: break - + print (author_url,) br2 = browser() with closing(br2.open(base_url + author_url, timeout=timeout)) as f: if counter <= 0: @@ -118,11 +118,11 @@ class ChitankaStore(BasicStoreConfig, StorePlugin): txt = ''.join(data.xpath('.//a[@class="dl dl-txt"]/@href')) # remove .zip extensions - if fb2.find('.zip') <> -1: + if fb2.find('.zip') != -1: fb2 = fb2[:fb2.find('.zip')] - if epub.find('.zip') <> -1: + if epub.find('.zip') != -1: epub = epub[:epub.find('.zip')] - if txt.find('.zip') <> -1: + if txt.find('.zip') != -1: txt = txt[:txt.find('.zip')] counter -= 1 From 4150cf5a511590cfa1439db721d0a9874b2c864a Mon Sep 17 00:00:00 2001 From: Alex Stanev Date: Tue, 19 Jul 2011 09:16:55 +0300 Subject: [PATCH 5/5] Fix whitespace --- src/calibre/gui2/store/stores/chitanka_plugin.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/calibre/gui2/store/stores/chitanka_plugin.py b/src/calibre/gui2/store/stores/chitanka_plugin.py index 2ad236adec..15b2151a4e 100644 --- a/src/calibre/gui2/store/stores/chitanka_plugin.py +++ b/src/calibre/gui2/store/stores/chitanka_plugin.py @@ -93,14 +93,14 @@ class ChitankaStore(BasicStoreConfig, StorePlugin): author_url = ''.join(data.xpath('.//a[contains(@href,"/person/")]/@href')) if counter <= 0: break - print (author_url,) + br2 = browser() with closing(br2.open(base_url + author_url, timeout=timeout)) as f: if counter <= 0: break f = unicode(f.read(), 'utf-8') doc2 = html.fromstring(f) - + # search for book title for data in doc2.xpath('//ul[@class="superlist booklist"]/li'): if counter <= 0: