Skip to content

Commit 571a271

Browse files
Gets search results displaying in Calibre
1 parent 8193fa4 commit 571a271

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

libgen_client.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from lxml import etree
1212
import re
1313
import urllib
14-
import urllib2
1514

1615
DEFAULT_FIELDS = "Title,Author,ID,MD5"
1716

@@ -82,6 +81,9 @@ def parse(node):
8281
downloads_nodes = xpath(node, DOWNLOADS_XPATH)
8382
downloads = [LibgenDownload.parse(n) for n in downloads_nodes]
8483

84+
if not author or not title:
85+
return None
86+
8587
return LibgenBook(title, author, series, downloads, language)
8688

8789
class LibgenSearchResults:
@@ -91,11 +93,17 @@ def __init__(self, results, total):
9193

9294
@staticmethod
9395
def parse(node):
94-
SEARCH_ROW_SELECTOR = "//table[2]//tr"
96+
SEARCH_ROW_SELECTOR = "//table[3]//tr"
9597

9698
result_rows = xpath(node, SEARCH_ROW_SELECTOR)
9799

98-
results = [LibgenBook.parse(row) for row in result_rows]
100+
results = []
101+
for row in result_rows:
102+
book = LibgenBook.parse(row)
103+
if book is None: continue
104+
105+
results.append(book)
106+
99107
total = 0
100108

101109
return LibgenSearchResults(results, total)
@@ -109,18 +117,21 @@ def search(self, query):
109117
query_params = {
110118
's': query,
111119
'f_group': 1
112-
}
120+
}
113121

114122
query_string = urllib.urlencode(query_params)
115-
request = urllib2.request(url + '?' + query_string)
123+
request = urllib.urlopen(url + '?' + query_string)
116124
html = request.read()
117125

118126
parser = etree.HTMLParser()
119-
tree = etree.fromstring(params, parser)
127+
tree = etree.fromstring(html, parser)
120128

121129
return LibgenSearchResults.parse(tree)
122130

123131

124132
if __name__ == "__main__":
125133
client = LibgenFictionClient()
126-
result = client.search("Stormlight Archive")
134+
result = client.search("way kings")
135+
136+
for result in result.results:
137+
print(result.title)

0 commit comments

Comments
 (0)