From 3bfca2f2fbabb72ffbda01e016fd53852eb3998e Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 7 May 2022 16:53:03 +0300 Subject: refactor a bit --- mgs.py | 92 ------------------------------------------------ mosgorsud/__init__.py | 5 +++ mosgorsud/parser.py | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++ telegram_notify.py | 43 ++++++++++++++--------- to_csv.py | 20 ++++++----- 5 files changed, 141 insertions(+), 116 deletions(-) delete mode 100644 mgs.py create mode 100644 mosgorsud/__init__.py create mode 100644 mosgorsud/parser.py diff --git a/mgs.py b/mgs.py deleted file mode 100644 index 96e22a9..0000000 --- a/mgs.py +++ /dev/null @@ -1,92 +0,0 @@ -import requests -import textract -import re -import os -import tempfile -import random -import string -from bs4 import BeautifulSoup -from typing import List, Dict - -headers = { - 'Referer': 'https://mos-gorsud.ru/', - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0' -} - -regex = r"""(?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)/)(?:[^\s()<>{}\[\]]+|\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\))+(?:\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\)|[^\s`!()\[\]{};:\'\".,<>?«»“”‘’])|(?:(? str: - return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(n)).lower() - - -def get_links(s: str) -> List[str]: - return list(set(re.findall(regex, s))) - - -class MGSPiracy: - BASE_URL = "https://mos-gorsud.ru/mgs/defend" - - def __init__(self, from_page: int, to_page: int): - self.from_page = from_page - self.to_page = to_page - - def get_cases(self) -> List[Dict]: - cases = [] - - for page in range(self.from_page, self.to_page+1): - print(f'page {page}') - - url = self.BASE_URL + '?page=' + str(page) - r = requests.get(url, headers=headers) - - soup = BeautifulSoup(r.text, "html.parser") - rows = soup.select('.searchResultContainer table.custom_table tbody tr') - - for row in rows: - cols = row.find_all('td') - - date = cols[0].get_text().strip() - statement_number = cols[1].get_text().strip() - applicant = cols[3].get_text().strip() - object = cols[4].get_text().strip() - link = self.mgs_url(cols[5].find('a')['href']) - - decision_text = self.get_document_text(link) - violation_links = '\n'.join(get_links(decision_text)) - - cases.append(dict( - date=date, - statement_number=statement_number, - applicant=applicant, - object=object, - doc_link=link, - violation_links=violation_links, - decision_text=decision_text - )) - - return cases - - def mgs_url(self, url: str) -> str: - if not url.startswith('http:') and not url.startswith('https:'): - if not url.startswith('/'): - url = '/' + url - url = 'https://mos-gorsud.ru' + url - return url - - def get_document_text(self, url: str) -> str: - print(f'downloading {url}') - - r = requests.get(url, allow_redirects=True, headers=headers) - content_disposition = r.headers['Content-Disposition'] - filename, file_extension = os.path.splitext(re.search('attachment; filename="(.*?)"', content_disposition).group(1)) - - tempname = '%s/%s%s' % (tempfile.gettempdir(), strgen(10), file_extension) - - with open(tempname, 'wb') as f: - f.write(r.content) - - text = textract.process(tempname).decode('utf-8') - os.unlink(tempname) - - return text diff --git a/mosgorsud/__init__.py b/mosgorsud/__init__.py new file mode 100644 index 0000000..3d9be46 --- /dev/null +++ b/mosgorsud/__init__.py @@ -0,0 +1,5 @@ +from .parser import get_cases + +__all__ = [ + 'get_cases' +] \ No newline at end of file diff --git a/mosgorsud/parser.py b/mosgorsud/parser.py new file mode 100644 index 0000000..0130354 --- /dev/null +++ b/mosgorsud/parser.py @@ -0,0 +1,97 @@ +import requests +import textract +import re +import os +import tempfile +import random +import string +import logging + +from bs4 import BeautifulSoup +from typing import List, Dict + +logger = logging.getLogger(__name__) + +BASE_URL = "https://mos-gorsud.ru/mgs/defend" + +headers = { + 'Referer': 'https://mos-gorsud.ru/', + 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0' +} + +regex = r"""(?i)\b((?:https?:(?:/{1,3}|[a-z0-9%])|[a-z0-9.\-]+[.](?:com|net|org|edu|gov|mil|aero|asia|biz|cat|coop|info|int|jobs|mobi|museum|name|post|pro|tel|travel|xxx|ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cs|cu|cv|cx|cy|cz|dd|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|Ja|sk|sl|sm|sn|so|sr|ss|st|su|sv|sx|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)/)(?:[^\s()<>{}\[\]]+|\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\))+(?:\([^\s()]*?\([^\s()]+\)[^\s()]*?\)|\([^\s]+?\)|[^\s`!()\[\]{};:\'\".,<>?«»“”‘’])|(?:(? str: + return ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(n)).lower() + + +def get_links(s: str) -> List[str]: + return list(set(re.findall(regex, s))) + + +def get_full_url(url: str) -> str: + if not url.startswith('http:') and not url.startswith('https:'): + if not url.startswith('/'): + url = '/' + url + url = 'https://mos-gorsud.ru' + url + return url + + +def get_document_text(url: str) -> str: + print(f'downloading {url}') + + r = requests.get(url, allow_redirects=True, headers=headers) + content_disposition = r.headers['Content-Disposition'] + filename, file_extension = os.path.splitext(re.search('attachment; filename="(.*?)"', content_disposition).group(1)) + + tempname = '%s/%s%s' % (tempfile.gettempdir(), strgen(10), file_extension) + + with open(tempname, 'wb') as f: + f.write(r.content) + + text = textract.process(tempname).decode('utf-8') + os.unlink(tempname) + + return text + + +def get_cases(from_page: int, to_page: int) -> List[Dict]: + cases = [] + + for page in range(from_page, to_page+1): + url = f'{BASE_URL}?page={page}' + print(f'page {page} ({url})') + + r = requests.get(url, headers=headers) + + soup = BeautifulSoup(r.text, "html.parser") + rows = soup.select('.searchResultContainer table.custom_table tbody tr') + + for row in rows: + cols = row.find_all('td') + + try: + date = cols[0].get_text().strip() + statement_number = cols[1].get_text().strip() + applicant = cols[3].get_text().strip() + object = cols[4].get_text().strip() + link = get_full_url(cols[5].find('a')['href']) + + decision_text = get_document_text(link) + violation_links = '\n'.join(get_links(decision_text)) + + cases.append(dict( + date=date, + statement_number=statement_number, + applicant=applicant, + object=object, + doc_link=link, + violation_links=violation_links, + decision_text=decision_text + )) + + except (TypeError, KeyError) as e: + logger.exception(e) + + return cases diff --git a/telegram_notify.py b/telegram_notify.py index a834b34..0ccb2b9 100755 --- a/telegram_notify.py +++ b/telegram_notify.py @@ -1,6 +1,9 @@ #!/usr/bin/env python3 import traceback -from mgs import MGSPiracy +import mosgorsud +import requests +import urllib3.exceptions + from argparse import ArgumentParser from ch1p import State, telegram_notify from html import escape @@ -10,21 +13,24 @@ if __name__ == '__main__': # parse arguments parser = ArgumentParser() parser.add_argument('--state-file', required=True) - parser.add_argument('--token', help='Telegram bot token', required=True) - parser.add_argument('--chat-id', type=int, help='Telegram chat id (with bot)', required=True) - parser.add_argument('--from', type=int, default=1, help='First page', dest='_from') - parser.add_argument('--to', type=int, default=5, help='Last page') + parser.add_argument('--token', required=True, + help='Telegram bot token',) + parser.add_argument('--chat-id', type=int, required=True, + help='Telegram chat id (with bot)') + parser.add_argument('--from', type=int, default=1, dest='_from', + help='First page') + parser.add_argument('--to', type=int, default=5, + help='Last page') parser.add_argument('--domains', nargs='+', required=True) - args = parser.parse_args() + arg = parser.parse_args() try: # get recent cases - mgs = MGSPiracy(from_page=args._from, to_page=args.to) - cases = mgs.get_cases() + cases = mosgorsud.get_cases(from_page=arg._from, to_page=arg.to) # read state - state = State(file=args.state_file, + state = State(file=arg.state_file, default=dict(cases=[])) # loop through cases @@ -34,10 +40,10 @@ if __name__ == '__main__': continue matched = False - for mydomain in args.domains: + for mydomain in arg.domains: if mydomain in case['decision_text']: matched = True - results.append('%s found in %s' % (mydomain, case['statement_number'])) + results.append('%s found in %s (%s)' % (mydomain, case['statement_number'], case['doc_link'])) state['cases'].append(case['statement_number']) if matched: @@ -50,14 +56,19 @@ if __name__ == '__main__': telegram_notify(text=escape(text), parse_mode='HTML', - token=args.token, - chat_id=args.chat_id) + token=arg.token, + chat_id=arg.chat_id) except KeyboardInterrupt: pass + except (TimeoutError, requests.exceptions.ConnectionError, urllib3.exceptions.MaxRetryError): + telegram_notify(text='mosgorsud error: network timeout', + token=arg.token, + chat_id=arg.chat_id) + except: - telegram_notify(text='error: '+escape(traceback.format_exc()), + telegram_notify(text='mosgorsud error: '+escape(traceback.format_exc()), parse_mode='HTML', - token=args.token, - chat_id=args.chat_id) + token=arg.token, + chat_id=arg.chat_id) diff --git a/to_csv.py b/to_csv.py index e08bc24..85674fd 100755 --- a/to_csv.py +++ b/to_csv.py @@ -1,22 +1,26 @@ #!/usr/bin/env python3 import csv -from mgs import MGSPiracy +import mosgorsud + from argparse import ArgumentParser + if __name__ == '__main__': # parse arguments argp = ArgumentParser() - argp.add_argument('--output', type=str, default='output.csv', help='CSV output file name') - argp.add_argument('--from', type=int, default=0, help='First page', dest='_from') - argp.add_argument('--to', type=int, default=10, help='Last page') - args = argp.parse_args() + argp.add_argument('--output', type=str, default='output.csv', + help='CSV output file name') + argp.add_argument('--from', type=int, default=1, dest='_from', + help='First page') + argp.add_argument('--to', type=int, default=10, + help='Last page') + arg = argp.parse_args() # get cases - mgs = MGSPiracy(from_page=args._from, to_page=args.to) - cases = mgs.get_cases() + cases = mosgorsud.get_cases(from_page=arg._from, to_page=arg.to) # write to csv - f = open(args.output, 'w', newline='') + f = open(arg.output, 'w', newline='') csv_writer = csv.writer(f) for case in cases: -- cgit v1.2.3