forked from fallaciousreasoning/CalibreLibgenStore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibgen_client.py
More file actions
169 lines (128 loc) · 4.56 KB
/
libgen_client.py
File metadata and controls
169 lines (128 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
from lxml import etree
import random
from urllib.request import urlopen
from urllib.parse import urlencode
def xpath(node, path):
tree = node.getroottree()
base_xpath = tree.getpath(node)
return tree.xpath(base_xpath + path)
class LibgenMirror:
def __init__(self, url, format, size, unit):
self.url = url
self.format = format
self.size = size
self.unit = unit
@staticmethod
def parse(node, file_type, file_size, file_size_unit):
url = node.get('href')
return LibgenMirror(url, file_type, file_size, file_size_unit)
class LibgenBook:
def __init__(self, title, authors, series, md5, mirrors, language,
image_url):
self.title = title
self.authors = authors
self.series = series
self.md5 = md5
self.mirrors = mirrors
self.language = language
self.image_url = image_url
@staticmethod
def parse(node):
AUTHOR_XPATH = '/td[1]//a'
SERIES_XPATH = '/td[2]'
TITLE_XPATH = '/td[3]/a'
LANGUAGE_XPATH = '/td[4]'
FILE_XPATH = '/td[5]'
MIRRORS_XPATH = '/td[6]//a'
# Parse the Author(s) column into `authors`
authors = ' & '.join([
author.text for author in xpath(node, AUTHOR_XPATH)
])
if len(authors) == 0:
authors = 'Unknown'
# Parse File and Mirrors columns into a list of mirrors
file_info = xpath(node, FILE_XPATH)[0].text
file_type, file_size = file_info.split(' / ')
file_size, file_size_unit = file_size.split('\xa0')
mirrors = [
LibgenMirror.parse(n, file_type, file_size, file_size_unit)
for n in xpath(node, MIRRORS_XPATH)
]
# Parse other columns
series = xpath(node, SERIES_XPATH)[0].text
title = xpath(node, TITLE_XPATH)[0].text
md5 = xpath(node, TITLE_XPATH)[0].get('href').split('/')[-1]
language = xpath(node, LANGUAGE_XPATH)[0].text
if not authors or not title:
return None
return LibgenBook(title, authors, series, md5, mirrors, language, None)
class LibgenSearchResults:
def __init__(self, results, total):
self.results = results
self.total = total
@staticmethod
def parse(node):
SEARCH_ROW_SELECTOR = "/body/table/tbody/tr"
result_rows = xpath(node, SEARCH_ROW_SELECTOR)
results = []
for row in result_rows:
book = LibgenBook.parse(row)
if book is None:
continue
results.append(book)
total = len(results)
return LibgenSearchResults(results, total)
class LibgenFictionClient:
def __init__(self, mirror=None):
MIRRORS = [
"libgen.rs",
"libgen.is",
# "libgen.lc", # Still has the old-style search
"gen.lib.rus.ec",
"93.174.95.27",
]
if mirror is None:
self.base_url = "http://{}/fiction/".format(MIRRORS[0])
else:
self.base_url = "http://{}/fiction/".format(mirror)
def search(self, query):
url = self.base_url
query_params = {
'q': query,
'criteria': '',
'language': '',
'format': '',
}
query_string = urlencode(query_params)
request = urlopen(url + '?' + query_string)
html = request.read()
parser = etree.HTMLParser()
tree = etree.fromstring(html, parser)
return LibgenSearchResults.parse(tree)
def get_detail_url(self, md5):
detail_url = '{}{}'.format(self.base_url, md5)
return detail_url
def get_download_url(self, md5):
download_urls = [
'http://library.lol/fiction/{}'.format(md5),
]
for url in download_urls:
try:
request = urlopen(url)
html = request.read()
parser = etree.HTMLParser()
tree = etree.fromstring(html, parser)
SELECTOR = "//h2/a[contains(., 'GET')]"
SELECTOR = "//a[contains(., 'GET')]"
link = tree.xpath(SELECTOR)
return link[0].get('href')
except:
continue
if __name__ == "__main__":
client = LibgenFictionClient()
search_results = client.search("the count of monte cristo")
for result in search_results.results[:5]:
print(result.title)
print("Detail", client.get_detail_url(result.md5))
print("Download", client.get_download_url(result.md5))