Исправления косяков вывода текста в дискорд
This commit is contained in:
25
news-bot.py
25
news-bot.py
@@ -73,6 +73,16 @@ def script_content(script_ver, resp_changelog):
|
|||||||
soup = make_soup(resp_changelog)
|
soup = make_soup(resp_changelog)
|
||||||
page_text = str(soup)
|
page_text = str(soup)
|
||||||
page_text = page_text.replace("Вы можете помочь развитию проекта: https://linux-gaming.ru/donate/", '')
|
page_text = page_text.replace("Вы можете помочь развитию проекта: https://linux-gaming.ru/donate/", '')
|
||||||
|
|
||||||
|
# Удаляем несколько различных вхождений с помощью регулярных выражений
|
||||||
|
patterns_to_remove = [
|
||||||
|
r'###Scripts version \d+###',
|
||||||
|
r'\d{4}×\d{3} \d+ KB',
|
||||||
|
r'/tag/scripts'
|
||||||
|
]
|
||||||
|
for pattern in patterns_to_remove:
|
||||||
|
page_text = re.sub(pattern, '', page_text)
|
||||||
|
|
||||||
# Находим текст до определенного текста, тега или класса (например, до тега <hr>)
|
# Находим текст до определенного текста, тега или класса (например, до тега <hr>)
|
||||||
last_text = f"###Scripts version {script_ver - 1}"
|
last_text = f"###Scripts version {script_ver - 1}"
|
||||||
last_text = str(last_text)
|
last_text = str(last_text)
|
||||||
@@ -109,7 +119,7 @@ def script_content(script_ver, resp_changelog):
|
|||||||
|
|
||||||
|
|
||||||
def news_content(post_id):
|
def news_content(post_id):
|
||||||
logging.info(f"Запрос содержимого поста новости с ID: {post_id}")
|
logging.debug(f"Запрос содержимого поста новости с ID: {post_id}")
|
||||||
response = response_get(f"https://linux-gaming.ru/t/{post_id}.json")
|
response = response_get(f"https://linux-gaming.ru/t/{post_id}.json")
|
||||||
if response and response.status_code == 200:
|
if response and response.status_code == 200:
|
||||||
topic_data = response.json()
|
topic_data = response.json()
|
||||||
@@ -167,8 +177,7 @@ def news():
|
|||||||
'«Новости»')]
|
'«Новости»')]
|
||||||
return filtered_list_titles_and_ids
|
return filtered_list_titles_and_ids
|
||||||
else:
|
else:
|
||||||
logging.error(f"Ошибка при запросе тем с сайта: {resp_topics.status_code if resp_topics else
|
logging.error(f"Ошибка при запросе тем с сайта: {resp_topics.status_code if resp_topics else 'Нет доступа к сайту'}")
|
||||||
'Нет доступа к сайту'}")
|
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
|
||||||
@@ -281,10 +290,12 @@ def check_discord_public():
|
|||||||
for topic_id, topic_title in list_titles_and_ids:
|
for topic_id, topic_title in list_titles_and_ids:
|
||||||
if topic_title not in discord_messages:
|
if topic_title not in discord_messages:
|
||||||
list_for_public.append((topic_id, topic_title))
|
list_for_public.append((topic_id, topic_title))
|
||||||
if list_for_public != []:
|
if not list_for_public:
|
||||||
logging.info(f"Новости для публикации в дискорд: {list_for_public}")
|
|
||||||
else:
|
|
||||||
logging.info(f"Новостей для публикации в дискорд нет")
|
logging.info(f"Новостей для публикации в дискорд нет")
|
||||||
|
await client.close()
|
||||||
|
else:
|
||||||
|
logging.info(f"Новости для публикации в дискорд: {list_for_public}")
|
||||||
|
|
||||||
channel = client.get_channel(channel_id)
|
channel = client.get_channel(channel_id)
|
||||||
if not channel:
|
if not channel:
|
||||||
logging.error(f"ID канала Discord {channel_id} не существует")
|
logging.error(f"ID канала Discord {channel_id} не существует")
|
||||||
@@ -301,7 +312,7 @@ def check_discord_public():
|
|||||||
# Разбиваем содержимое на части по 4000 символов
|
# Разбиваем содержимое на части по 4000 символов
|
||||||
for i in range(0, len(content), 4000):
|
for i in range(0, len(content), 4000):
|
||||||
await channel.send(content[i:i+4000])
|
await channel.send(content[i:i+4000])
|
||||||
|
await client.close()
|
||||||
client.run(keys.discord_token)
|
client.run(keys.discord_token)
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user