1+ # -*- coding: utf-8 -*-
2+
3+ from __future__ import (unicode_literals , division , absolute_import , print_function )
4+ store_version = 5 # Needed for dynamic plugin loading
5+
6+ __license__ = 'MIT'
7+ __copyright__ = 'Jay Harris <jay.harris@outlook.co.nz>'
8+ __docformat__ = 'restructuredtext en'
9+
10+ #####################################################################
11+ # Plug-in base class
12+ #####################################################################
13+
14+ from calibre .customize import InterfaceActionBase
15+
16+ PLUGIN_NAME = 'Libgen Fiction'
17+ PLUGIN_DESCRIPTION = 'Adds a Libfen Fiction search provider to Calibre'
18+ PLUGIN_VERSION_TUPLE = (0 , 0 , 0 )
19+ PLUGIN_VERSION = '.' .join ([str (x ) for x in PLUGIN_VERSION_TUPLE ])
20+ PLUGIN_AUTHORS = "Jay Harris <jay.harris@outlook.co.nz"
21+
22+ #####################################################################
23+
24+ import base64
25+ import mimetypes
26+ import re
27+ import urllib
28+ import urllib2
29+ from contextlib import closing
30+
31+ from lxml import etree
32+
33+ from .libgen_client import LibgenFictionClient
34+
35+ from calibre import browser , url_slash_cleaner
36+ from calibre .constants import __appname__ , __version__
37+ from calibre .gui2 .store .basic_config import BasicStoreConfig
38+ from calibre .gui2 .store .search_result import SearchResult
39+ from calibre .gui2 .store import StorePlugin
40+
41+ from calibre .customize import StoreBase
42+
43+
44+ web_url = 'http://libgen.io/'
45+ libgen = LibgenFictionClient ()
46+
47+ def search (query , max_results = 10 , timeout = 60 ):
48+ libgen_results = libgen .search (query )
49+ for result in libgen_results .results [:min (max_results , len (libgen_results .results ))]:
50+ s = SearchResult ()
51+
52+ s .title = result .title
53+ s .author = result .author
54+ s .series = result .series
55+ s .language = result .language
56+
57+ for download in result .downloads :
58+ s .downloads [download .format ] = download .url
59+
60+ s .formats = ', ' .join (s .downloads .keys ())
61+
62+ # don't show results with no downloads
63+ if not s .formats :
64+ continue
65+
66+ yield s
67+
68+
69+ class LibgenStore (StorePlugin ):
70+ def search (self , query , max_results = 10 , timeout = 60 ):
71+ '''
72+ Searches LibGen for Books
73+ '''
74+ for result in search (query , max_results , timeout ):
75+ yield result
76+
77+ if __name__ == '__main__' :
78+ import sys
79+
80+ query = ' ' .join (sys .argv [1 :]) if len (sys .argv ) > 1 else "Stormlight Archive"
81+ for result in search (' ' .join (sys .argv [1 :])):
82+ print ('=========================================================================================================\n Title: {0}\n Author: {1}\n Series: {2}\n Language: {3}\n Downloads: {4}' .format (result .title , result .author , result .series , result .language , len (result .downloads )))
83+
84+ class LibgenStoreWrapper (StoreBase ):
85+ name = PLUGIN_NAME
86+ description = PLUGIN_DESCRIPTION
87+ supported_platforms = ['windows' , 'osx' , 'linux' ]
88+ author = PLUGIN_AUTHORS
89+ version = PLUGIN_VERSION_TUPLE
90+ minimum_calibre_version = (1 , 0 , 0 )
91+ #actual_plugin = "calibre_plugins.liben_fiction.LibgenStore"
92+ affiliate = False
93+
94+ def load_actual_plugin (self , gui ):
95+ '''
96+ This method must return the actual interface action plugin object.
97+ '''
98+ #mod, cls = self.actual_plugin.split(':')
99+ store = LibgenStore (gui , self .name )
100+ self .actual_plugin_object = store #getattr(importlib.import_module(mod), cls)(gui, self.name)
101+ return self .actual_plugin_object
0 commit comments