Queer European MD passionate about IT
Pārlūkot izejas kodu

/version command looks for updates

Davte 4 gadi atpakaļ
vecāks
revīzija
f840f9fe13
2 mainītis faili ar 66 papildinājumiem un 31 dzēšanām
  1. 1 1
      davtelepot/__init__.py
  2. 65 30
      davtelepot/administration_tools.py

+ 1 - 1
davtelepot/__init__.py

@@ -11,7 +11,7 @@ __author__ = "Davide Testa"
 __email__ = "davide@davte.it"
 __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
 __license__ = "GNU General Public License v3.0"
-__version__ = "2.5.10"
+__version__ = "2.5.11"
 __maintainer__ = "Davide Testa"
 __contact__ = "t.me/davte"
 

+ 65 - 30
davtelepot/administration_tools.py

@@ -830,6 +830,46 @@ async def get_last_commit():
     return last_commit
 
 
+async def get_new_versions(bot: Bot,
+                           notification_interval: datetime.timedelta = None) -> dict:
+    """Get new versions of packages in bot.packages.
+
+    Result: {"name": {"current": "0.1", "new": "0.2"}}
+    """
+    if notification_interval is None:
+        notification_interval = datetime.timedelta(seconds=0)
+    news = dict()
+    for package in bot.packages:
+        package_web_page = CachedPage.get(
+            f'https://pypi.python.org/pypi/{package.__name__}/json',
+            cache_time=2,
+            mode='json'
+        )
+        web_page = await package_web_page.get_page()
+        if web_page is None or isinstance(web_page, Exception):
+            logging.error(f"Cannot get updates for {package.__name__}, "
+                          "skipping...")
+            continue
+        new_version = web_page['info']['version']
+        current_version = package.__version__
+        notification_record = bot.db['updates_notifications'].find_one(
+            package=package.__name__,
+            order_by=['-id'],
+            _limit=1
+        )
+        if (
+                new_version != current_version
+                and (notification_record is None
+                     or notification_record['notified_at']
+                     < datetime.datetime.now() - notification_interval)
+        ):
+            news[package.__name__] = {
+                'current': current_version,
+                'new': new_version
+            }
+    return news
+
+
 async def _version_command(bot: Bot, update, user_record):
     last_commit = await get_last_commit()
     text = bot.get_message(
@@ -842,7 +882,29 @@ async def _version_command(bot: Bot, update, user_record):
         f"<code>{package.__version__}</code>"
         for package in bot.packages
     )
-    return text
+    temporary_message = await bot.send_message(
+        text=text + '\n\n⏳ Checking for updates... ☑️',
+        update=update,
+        send_default_keyboard=False
+    )
+    news = await get_new_versions(bot=bot)
+    if not news:
+        text += '\n\n⌛️ All packages are updated! ✅'
+    else:
+        text += '\n\n' + bot.get_message(
+            'admin', 'updates_available', 'header',
+            user_record=user_record
+        ) + '\n\n'
+        text += '\n'.join(
+            f"<b>{package}</b>: "
+            f"<code>{versions['current']}</code> —> "
+            f"<code>{versions['new']}</code>"
+            for package, versions in news.items()
+        )
+    await bot.edit_message_text(
+        text=text,
+        update=temporary_message
+    )
 
 
 async def notify_new_version(bot: Bot):
@@ -918,35 +980,8 @@ async def get_package_updates(bot: Bot,
             seconds=notification_interval
         )
     while 1:
-        news = dict()
-        for package in bot.packages:
-            package_web_page = CachedPage.get(
-                f'https://pypi.python.org/pypi/{package.__name__}/json',
-                cache_time=2,
-                mode='json'
-            )
-            web_page = await package_web_page.get_page()
-            if web_page is None or isinstance(web_page, Exception):
-                logging.error(f"Cannot get updates for {package.__name__}, "
-                              "skipping...")
-                continue
-            new_version = web_page['info']['version']
-            current_version = package.__version__
-            notification_record = bot.db['updates_notifications'].find_one(
-                package=package.__name__,
-                order_by=['-id'],
-                _limit=1
-            )
-            if (
-                    new_version != current_version
-                    and (notification_record is None
-                         or notification_record['notified_at']
-                         < datetime.datetime.now() - notification_interval)
-            ):
-                news[package.__name__] = {
-                    'current': current_version,
-                    'new': new_version
-                }
+        news = await get_new_versions(bot=bot,
+                                      notification_interval=notification_interval)
         if news:
             for admin in bot.administrators:
                 text = bot.get_message(