немного логов
This commit is contained in:
49
news-bot.py
49
news-bot.py
@@ -40,7 +40,7 @@ params_get = {
|
||||
}
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.setLevel(logging.INFO)
|
||||
|
||||
handler = colorlog.StreamHandler()
|
||||
handler.setFormatter(colorlog.ColoredFormatter(
|
||||
@@ -66,10 +66,13 @@ def main():
|
||||
|
||||
|
||||
def make_soup(resp_changelog):
|
||||
logging.debug(f"Вызываем make_soup")
|
||||
return BeautifulSoup(resp_changelog.text, 'html.parser')
|
||||
|
||||
|
||||
def html_to_text(html_content):
|
||||
logging.debug(f"Вызываем html_to_text")
|
||||
logging.debug(f"HTML на входе {html_content}")
|
||||
h = html2text.HTML2Text()
|
||||
h.ignore_links = False # Сохранение ссылок
|
||||
h.ignore_images = True # Игнорирование изображений
|
||||
@@ -77,7 +80,7 @@ def html_to_text(html_content):
|
||||
h.reference_links = True # Сохранение оригинальных ссылок
|
||||
markdown_text = h.handle(html_content)
|
||||
|
||||
logging.debug(f"Markdown text: {markdown_text}")
|
||||
logging.debug(f"Текст до обработки регулярками {markdown_text}")
|
||||
|
||||
# Удаление переносов строк из-за -
|
||||
markdown_text = re.sub(r'-\s*\n\s*', '-', markdown_text, flags=re.DOTALL)
|
||||
@@ -112,7 +115,6 @@ def html_to_text(html_content):
|
||||
]
|
||||
for pattern in patterns_to_remove:
|
||||
markdown_text = re.sub(pattern, '', markdown_text)
|
||||
logging.debug(f"Удаляем {pattern}")
|
||||
|
||||
# Удаление избыточных пустых строк после удаления строк
|
||||
markdown_text = re.sub(r'\n\s*\n', '\n', markdown_text)
|
||||
@@ -137,32 +139,41 @@ def html_to_text(html_content):
|
||||
|
||||
|
||||
def convert_links(text):
|
||||
logging.debug(f"Входим в convert_links")
|
||||
url_pattern = re.compile(r'https?://[^\s\)]+')
|
||||
return url_pattern.sub(lambda match: decode_url_params(match.group(0)), text)
|
||||
|
||||
url_pattern = url_pattern.sub(lambda match: decode_url_params(match.group(0)), text)
|
||||
logging.debug(f"Возврат url_pattern {url_pattern}")
|
||||
return url_pattern
|
||||
|
||||
def decode_url_params(url):
|
||||
logging.debug(f"Входим в decode_url_params")
|
||||
parsed_url = urllib.parse.urlparse(url)
|
||||
query_params = urllib.parse.parse_qs(parsed_url.query)
|
||||
for key, values in query_params.items():
|
||||
if key.lower() == 'to' and values:
|
||||
return urllib.parse.unquote(values[0])
|
||||
logging.debug(f"Возврат url {url}")
|
||||
return url
|
||||
|
||||
|
||||
def remove_empty_lines(text_data):
|
||||
lines = text_data.splitlines()
|
||||
non_empty_lines = [line for line in lines if line.strip()]
|
||||
return '\n'.join(non_empty_lines)
|
||||
|
||||
logging.debug(f"Входим в remove_empty_lines")
|
||||
lines = text_data.splitlines()
|
||||
non_empty_lines = [line for line in lines if line.strip()]
|
||||
non_empty_lines = '\n'.join(non_empty_lines)
|
||||
logging.debug(f"Возврат non_empty_lines {non_empty_lines}")
|
||||
return non_empty_lines
|
||||
|
||||
def remove_markdown_links(markdown_text):
|
||||
logging.debug(f"Входим в remove_markdown_links")
|
||||
# Регулярное выражение для поиска Markdown-ссылок и замена их на только URL
|
||||
markdown_text = re.sub(r'\[.*?\]\((https?://.*?)\)', r'\1' or r'(`https?://.*?)`\)', markdown_text)
|
||||
logging.debug(f"Возврат markdown_text {markdown_text}")
|
||||
return markdown_text
|
||||
|
||||
|
||||
def remove_duplicate_links(text):
|
||||
logging.debug(f"Входим в remove_duplicate_links")
|
||||
seen_links = set()
|
||||
|
||||
def replace_link(match):
|
||||
@@ -175,17 +186,21 @@ def remove_duplicate_links(text):
|
||||
# Регулярное выражение для поиска Markdown-ссылок
|
||||
link_pattern = re.compile(r'(\[.*?\]\((https:\/\/.*?)\))')
|
||||
text = re.sub(link_pattern, replace_link, text)
|
||||
|
||||
logging.debug(f"Возвращаем text {text}")
|
||||
return text
|
||||
|
||||
|
||||
def extract_links(text):
|
||||
logging.debug(f"Входим в extract_links")
|
||||
# Регулярное выражение для поиска ссылок
|
||||
url_pattern = re.compile(r'https?://\S+')
|
||||
return url_pattern.findall(text)
|
||||
url_pattern = url_pattern.findall(text)
|
||||
logging.debug(f"Возвращаем url_pattern {url_pattern}")
|
||||
return url_pattern
|
||||
|
||||
|
||||
def script_content(script_ver, resp_changelog):
|
||||
logging.debug(f"Вход в script_content")
|
||||
soup = make_soup(resp_changelog)
|
||||
page_text = str(soup)
|
||||
page_text = page_text.replace("Вы можете помочь развитию проекта: https://linux-gaming.ru/donate/", '')
|
||||
@@ -213,7 +228,7 @@ def script_content(script_ver, resp_changelog):
|
||||
"raw": site_text,
|
||||
"category": keys.cat_num
|
||||
}
|
||||
|
||||
logging.debug(f"Возвращаем post_text - {post_text}\n post_data - {post_data}")
|
||||
return post_text, post_data, post_text
|
||||
|
||||
|
||||
@@ -251,7 +266,7 @@ def resp_change():
|
||||
|
||||
if resp_changelog and resp_changelog.status_code == 200:
|
||||
matches_changelog = re.findall(r'###Scripts version (\d+)###', resp_changelog.text)
|
||||
logging.info(f"Найдены версии в истории изменений: {matches_changelog}")
|
||||
logging.debug(f"Найдены версии в истории изменений: {matches_changelog}")
|
||||
last_changelog = int(max(matches_changelog))
|
||||
logging.info(f"Последняя версия в истории изменений: {last_changelog}")
|
||||
return last_changelog, resp_changelog
|
||||
@@ -371,7 +386,7 @@ def check_discord_public():
|
||||
list_for_public.append((topic_id, topic_title))
|
||||
|
||||
if not list_for_public:
|
||||
logging.info(f"Новостей для публикации в дискорд нет")
|
||||
logging.warning(f"Новостей для публикации в дискорд нет")
|
||||
await client_discord.close()
|
||||
else:
|
||||
logging.info(f"Новости для публикации в дискорд: {list_for_public}")
|
||||
@@ -467,7 +482,7 @@ def check_vk_posts():
|
||||
list_for_public.append((topic_id, topic_title))
|
||||
|
||||
if not list_for_public:
|
||||
logging.info(f"Новостей для публикации в ВК нет")
|
||||
logging.warning(f"Новостей для публикации в ВК нет")
|
||||
else:
|
||||
for topic_id, topic_title in reversed(list_for_public):
|
||||
if topic_id > keys.start_topic_id:
|
||||
@@ -487,7 +502,7 @@ def check_vk_posts():
|
||||
else:
|
||||
vk_post(url_vk_post, content)
|
||||
else:
|
||||
logging.info(f"Новостей для публикации в ВК нет")
|
||||
logging.warning(f"Новостей для публикации в ВК нет")
|
||||
|
||||
|
||||
def tg_post(post_text, client_tg):
|
||||
@@ -529,7 +544,7 @@ def check_tg_news():
|
||||
list_for_public.append((topic_id, topic_title))
|
||||
|
||||
if not list_for_public:
|
||||
logging.info(f"Новостей для публикации в Telegram нет")
|
||||
logging.warning(f"Новостей для публикации в Telegram нет")
|
||||
await client_tg.disconnect()
|
||||
else:
|
||||
logging.info(f"Новости для публикации в Telegram: {list_for_public}")
|
||||
|
Reference in New Issue
Block a user