11# -*-coding: utf-8 -*-
22import asyncio
33import aiohttp
4+ import re
45from typing import List
5- from unicodedata import name
66from os import getenv
77from sys import argv
88import traceback
1212from _utils import *
1313from discord import update_hook
1414
15+ github_download_url_regex = re .compile (
16+ r"https://github\.com/(?P<author>[a-zA-Z0-9-]+)/(?P<repo>[a-zA-Z0-9\.\-\_]*)/releases/download/(?P<version>[a-zA-Z\.0-9]+)/(?P<filename>.*)\.zip"
17+ )
18+
19+
1520async def batch_github_plugin_info (
1621 info : P , tags : ETagsType , github_token = None , webhook_url : str | None = None
1722) -> P :
@@ -44,11 +49,31 @@ async def batch_github_plugin_info(
4449
4550 if info .get (release_date , "" ) != latest_rel .get ("published_at" ):
4651 info [release_date ] = latest_rel .get ("published_at" )
52+
4753 if assets :
48- info [url_download ] = assets [0 ]["browser_download_url" ]
54+ browser_download_url = None
55+
56+ if len (assets ) > 1 :
57+ match = github_download_url_regex .match (info [url_download ])
58+ if not match :
59+ raise ValueError (
60+ f"Download URL did not mach regex: { info [url_download ]!r} "
61+ )
62+ filename = f"{ match ['filename' ]} .zip"
63+ for asset in assets :
64+ if asset ["browser_download_url" ].endswith (filename ):
65+ browser_download_url = asset ["browser_download_url" ]
66+
67+ info [url_download ] = (
68+ browser_download_url or assets [0 ]["browser_download_url" ]
69+ )
70+
4971 if webhook_url :
5072 await send_notification (
51- info , clean (latest_rel ["tag_name" ], "v" ), latest_rel , webhook_url
73+ info ,
74+ clean (latest_rel ["tag_name" ], "v" ),
75+ latest_rel ,
76+ webhook_url ,
5277 )
5378 info [version ] = clean (latest_rel ["tag_name" ], "v" )
5479
@@ -77,7 +102,6 @@ def remove_unused_etags(plugin_infos: PluginsType, etags: ETagsType) -> ETagsTyp
77102 plugin_ids = [info .get ("ID" ) for info in plugin_infos ]
78103
79104 for id , tag in etags .items ():
80-
81105 if id not in plugin_ids :
82106 print (
83107 f"Plugin with ID { id } has been removed. The associated ETag will be also removed now."
@@ -94,14 +118,15 @@ async def send_notification(
94118) -> None :
95119 if not webhook_url :
96120 return
97-
121+
98122 if version_tuple (info [version ]) != version_tuple (latest_ver ):
99123 tqdm .write (f"Update detected: { info [plugin_name ]} { latest_ver } " )
100124 try :
101125 await update_hook (webhook_url , info , latest_ver , release )
102126 except Exception as e :
103127 tqdm .write (str (e ))
104128
129+
105130async def main ():
106131 webhook_url = None
107132 if len (argv ) > 1 :
0 commit comments