Skip to content

Commit 4eecf64

Browse files
Adds image cover support
1 parent 4d48e81 commit 4eecf64

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

libgen_client.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
DEFAULT_FIELDS = "Title,Author,ID,MD5"
22

3+
BASE_URL = "http://libgen.io/"
34
LIBGEN_URL = "http://libgen.io/foreignfiction/"
45

56
BOOK_ENDPOINT = "json.php?ids={0}&fields={1}"
@@ -58,12 +59,13 @@ def parse(node):
5859
return LibgenDownload(url, format, size, unit)
5960

6061
class LibgenBook:
61-
def __init__(self, title, author, series, downloads, language):
62+
def __init__(self, title, author, series, downloads, language, image_url):
6263
self.title = title
6364
self.author = author
6465
self.series = series
6566
self.downloads = downloads
6667
self.language = language
68+
self.image_url = image_url
6769

6870
@staticmethod
6971
def parse(node):
@@ -72,6 +74,7 @@ def parse(node):
7274
TITLE_XPATH = '/td[3]'
7375
LANGUAGE_XPATH = '/td[4]'
7476
DOWNLOADS_XPATH = '/td[5]/div/a[1]'
77+
IMAGE_REGEX = re.compile("\&lt\;img src=\"?/(fictioncovers/.*?)\"? .*?\&gt\;")
7578

7679
author_result = xpath(node, AUTHOR_XPATH)
7780
author = author_result[0].text if len(author_result) > 0 else 'Unknown'
@@ -85,7 +88,11 @@ def parse(node):
8588
if not author or not title:
8689
return None
8790

88-
return LibgenBook(title, author, series, downloads, language)
91+
raw_html = etree.tostring(node)
92+
image_match = IMAGE_REGEX.search(raw_html)
93+
image_url = BASE_URL + image_match.groups(1)[0] if image_match is not None else None
94+
95+
return LibgenBook(title, author, series, downloads, language, image_url)
8996

9097
class LibgenSearchResults:
9198
def __init__(self, results, total):

0 commit comments

Comments
 (0)