Working on package update notifier

This commit is contained in:
Davte
2020-04-26 23:14:10 +02:00
parent 5a70dcfeb2
commit f0be5b5ff2
3 changed files with 33 additions and 11 deletions

View File

@@ -14,15 +14,23 @@ import datetime
import json import json
# Third party modules # Third party modules
import davtelepot from sqlalchemy.exc import ResourceClosedError
from davtelepot import messages
from davtelepot.utilities import ( # Project modules
async_wrapper, Confirmator, extract, get_cleaned_text, get_user, from . import bot as davtelepot_bot, messages, __version__ as version
escape_html_chars, line_drawing_unordered_list, make_button, from .utilities import (
async_wrapper, CachedPage, Confirmator, extract, get_cleaned_text,
get_user, escape_html_chars, line_drawing_unordered_list, make_button,
make_inline_keyboard, remove_html_tags, send_part_of_text_file, make_inline_keyboard, remove_html_tags, send_part_of_text_file,
send_csv_file send_csv_file
) )
from sqlalchemy.exc import ResourceClosedError
# davtelepot_web_page = CachedPage(url="https://pypi.org/project/davtelepot/",
davtelepot_web_page = CachedPage(
url='https://pypi.python.org/pypi/davtelepot/json',
cache_time=2,
mode='json'
)
async def _forward_to(update, bot, sender, addressee, is_admin=False): async def _forward_to(update, bot, sender, addressee, is_admin=False):
@@ -793,7 +801,7 @@ async def get_version():
last_commit = f"{e}" last_commit = f"{e}"
if last_commit.startswith("fatal: not a git repository"): if last_commit.startswith("fatal: not a git repository"):
last_commit = "-" last_commit = "-"
davtelepot_version = davtelepot.__version__ davtelepot_version = version
return last_commit, davtelepot_version return last_commit, davtelepot_version
@@ -855,6 +863,17 @@ async def notify_new_version(bot):
return return
async def update_davtelepot(bot: davtelepot_bot,
monitoring_interval: int = 60 * 60):
while 1:
web_page = await davtelepot_web_page.get_page()
if web_page['info']['version'] != version:
bot.stop(message='New version of davtelepot detected.\n'
'=== RESTARTING ===',
final_state=65)
await asyncio.sleep(monitoring_interval)
def init(telegram_bot, talk_messages=None, admin_messages=None): def init(telegram_bot, talk_messages=None, admin_messages=None):
"""Assign parsers, commands, buttons and queries to given `bot`.""" """Assign parsers, commands, buttons and queries to given `bot`."""
if talk_messages is None: if talk_messages is None:
@@ -878,6 +897,8 @@ def init(telegram_bot, talk_messages=None, admin_messages=None):
for command in ['stop', 'restart', 'maintenance'] for command in ['stop', 'restart', 'maintenance']
] ]
asyncio.ensure_future(update_davtelepot(telegram_bot))
@telegram_bot.additional_task(when='BEFORE') @telegram_bot.additional_task(when='BEFORE')
async def load_talking_sessions(): async def load_talking_sessions():
sessions = [] sessions = []

View File

@@ -1,4 +1,4 @@
"""This module provides a glow-like middleware for Telegram bot API. """This module provides a python mirror for Telegram bot API.
All methods and parameters are the same as the original json API. All methods and parameters are the same as the original json API.
A simple aiohttp asynchronous web client is used to make requests. A simple aiohttp asynchronous web client is used to make requests.
@@ -10,11 +10,11 @@ import datetime
import json import json
import logging import logging
# Third party modules
from typing import Union, List from typing import Union, List
# Third party modules
import aiohttp import aiohttp
from aiohttp import web import aiohttp.web
class TelegramError(Exception): class TelegramError(Exception):
@@ -82,7 +82,7 @@ class TelegramBot:
""" """
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
app = web.Application() app = aiohttp.web.Application()
sessions_timeouts = { sessions_timeouts = {
'getUpdates': dict( 'getUpdates': dict(
timeout=35, timeout=35,

View File

@@ -59,6 +59,7 @@ setuptools.setup(
'bs4', 'bs4',
'dataset', 'dataset',
], ],
python_requires='>=3.5',
classifiers=[ classifiers=[
"Development Status :: 5 - Production/Stable", "Development Status :: 5 - Production/Stable",
"Environment :: Console", "Environment :: Console",