-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
180 lines (136 loc) · 4.66 KB
/
main.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
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
170
171
172
173
174
175
176
177
178
179
180
"""
Copyright (c) 2022 Plugin Andrey ([email protected])
Licensed under the MIT License
"""
import configparser
import argparse
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
description='Running the application.'
)
parser.add_argument('-l', '--log', dest='log', type=str, default="",
help='The log write to file\npython main.py --log out.log')
parser.add_argument('-d', '--debug', dest='debug', action="store_true", default=False,
help='Debug mode\n-d or --debug')
parser.add_argument('-cc', '--cache', dest='cache', action="store_true", default=False,
help='Use requests cache\n-cc or --cache')
args = parser.parse_args()
# config = configparser.ConfigParser()
# include config.file
# config.read("config.ini")
import requests as _requests
# Кешируем запросы
CACHE = args.cache
if CACHE:
import requests_cache
requests_cache.install_cache('requests_cache')
from tool import log
from bs4 import BeautifulSoup
from dataclasses import dataclass
from typing import Any, List, Union
import os
from datetime import datetime
if args.log:
logger = log(__name__, args.log)
else:
logger = log(__name__)
BASE_URL = ""
DEBUG = args.debug
REPORT_PATH = os.path.join(os.getcwd(), datetime.now().strftime("%Y_%m_%d-%I_%M_%S_%p") + ".csv")
HEADERS = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:86.0) Gecko/20100101 Firefox/86.0"
}
class BaseParserException(Exception):
"""Базовое исключение"""
pass
class NotUrlsException(BaseParserException):
"""Если нет каких то ссылок"""
pass
class BadStatusCode(BaseParserException):
"""Сервер вернул плохой ответ"""
pass
class ConnectionError(BaseParserException):
"""Проблемы с соединением"""
pass
class Requests:
"""Класс обертка над библиотекой requests"""
class Response:
"""Регистрация """
def __init__(self, url, status_code):
self.url = url
self.status_code = status_code
self.type_url = None
_response_list: List[Response] = []
COUNT = 0
def get(self, *args, **kwargs):
"""get запрос"""
Requests.COUNT += 1
url = args[0]
try:
res = _requests.get(*args, **kwargs)
Requests._response_list.append(self.Response(url, res.status_code))
if res.status_code == 200:
logger.info(f"Response [{res.status_code}] {url}")
else:
logger.warning(f"Status code: {res.status_code} {url}")
raise BadStatusCode(f"Bad status code: {res.status_code}")
except _requests.exceptions.ConnectionError:
Requests._response_list.append(self.Response(url, 900))
logger.warning(f"Connection Error {url}")
raise ConnectionError(f"Problem with {url} ")
return res
def post(self, *args, **kwargs):
"""post запрос"""
pass
def __del__(self):
logger.info(self.requests_report())
def requests_report(self):
"""Вывод статистики по парсингу"""
status200 = list(filter(lambda x: x.status_code == 200, self._response_list))
statusBad = list(filter(lambda x: x.status_code != 200, self._response_list))
string = f"\n{'-'*14} requests result {'-'*14}\n" \
f"Urls {Requests.COUNT}\n" \
f"Good {len(status200)}\n" \
f"Bad {len(statusBad)}\n" \
f"{'-'*45}"
return string
requests = Requests()
class Product:
"""Объект парсинга"""
products = []
COLUMN_NAME = []
def __init__(self, url):
pass
def dump_csv(self):
pass
def dump_json(self):
pass
@staticmethod
def get_soup(html: str) -> BeautifulSoup:
soup = BeautifulSoup(html, "html.parser")
return soup
class FunctionUnit:
"""Функциональный блок"""
SHARE_DATA:Union[Any, None] = None
def __init__(self, func):
self.func = func
def run(self):
self.func(FunctionUnit)
def __call__(self, *args, **kwargs):
self.func(FunctionUnit)
def search_content(cls: FunctionUnit):
pass
def end_parsing(cls: FunctionUnit):
pass
def main():
unit01 = FunctionUnit(search_content)
unit02 = FunctionUnit(end_parsing)
unit01.run()
unit02.run()
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
logger.debug("\n >>> Stop. CTRL+C")
finally:
del requests