-
Notifications
You must be signed in to change notification settings - Fork 0
/
kazneb.py
executable file
·61 lines (45 loc) · 1.63 KB
/
kazneb.py
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
#!/usr/bin/env python3
import requests
import sys
import signal
import re
import argparse
class KanebLoader:
def __init__(self, url):
self.url = url
self.index = []
self.session = requests.Session()
self.session.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
}
def loadIndex(self):
'''
Загрузка списка страниц
'''
resp = self.session.get(self.url)
resp.raise_for_status()
html = resp.text
for m in re.finditer(r"/FileStore/dataFiles/\w+/\w+/\d+/content/[^\"]+", html):
self.index.append("https://kazneb.kz" + m.group(0).replace("amp;", ''))
print("Index of pages was loaded: %d" % len(self.index))
def loadBook(self):
for idx, pageUrl in enumerate(self.index):
print("Loading page: %d.png" % idx)
resp = self.session.get(pageUrl)
resp.raise_for_status()
with open("%d.png" % idx, 'wb') as f:
for chunk in resp.iter_content(1024):
f.write(chunk)
def signal_handler(sig, frame):
print('\nYou pressed Ctrl+C!')
sys.exit(0)
def main():
signal.signal(signal.SIGINT, signal_handler)
argparser = argparse.ArgumentParser(description='This program need to download books from elib.shpl.ru')
argparser.add_argument('BOOK_URL', type=str)
args = argparser.parse_args()
loader = KanebLoader(args.BOOK_URL)
loader.loadIndex()
loader.loadBook()
if __name__ == "__main__":
main()