-
Notifications
You must be signed in to change notification settings - Fork 1
/
utilities.py
50 lines (39 loc) · 1.33 KB
/
utilities.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
import os
import re
import logging
from constants import LOG_FILE_NAME
def create_logger():
logger = logging.getLogger('WebScrappy')
logger.setLevel(logging.DEBUG)
log_format = logging.Formatter('%(levelname)s: %(asctime)s - %(message)s')
handler = logging.FileHandler(filename=LOG_FILE_NAME, encoding='utf-8', mode='w')
handler.setFormatter(log_format)
logger.addHandler(handler)
return logger
def find_last_scraped_catalog_page():
"""
Parse log file to find line with information about last scraped catalog page.\n
If no file or no such line found - return 1
:return: last scraped catalog page number
:rtype: int
"""
if os.path.exists(LOG_FILE_NAME):
with open(LOG_FILE_NAME, 'r') as log_file:
lines = log_file.readlines()
for line in reversed(lines):
if line.rstrip('\n')[-3:].isdigit():
last_scraped_catalog_page = re.search(r'(\d+)/\d+$', line).group(1)
return int(last_scraped_catalog_page) + 1
# if line with info wasn't found
else:
return 1
else:
return 1
def save_html_to_file(html_content, save_path):
"""
:param html_content:
:param save_path:
:return:
"""
with open(save_path, 'wb') as f:
f.write(html_content)