-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
207 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,77 @@ | ||
"""Utility module to check for new release of telegram-media-downloader""" | ||
import http.client | ||
import json | ||
|
||
import requests # type: ignore | ||
from loguru import logger | ||
from rich.console import Console | ||
from rich.markdown import Markdown | ||
|
||
from . import __version__ | ||
|
||
|
||
# pylint: disable = C0301 | ||
def check_for_updates() -> None: | ||
"""Checks for new releases. | ||
def get_latest_release(proxy_config: dict = None) -> dict: | ||
""" | ||
Get the latest release information. | ||
Using Github API checks for new release and prints information of new release if available. | ||
:param proxy_config: A dictionary containing proxy configuration settings (default: {}). | ||
:type proxy_config: dict | ||
:return: A dictionary containing the latest release information. | ||
:rtype: dict | ||
""" | ||
console = Console() | ||
try: | ||
headers: dict = { | ||
"Content-Type": "application/json", | ||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36", | ||
headers: dict = { | ||
"Content-Type": "application/json", | ||
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36", | ||
} | ||
|
||
if proxy_config: | ||
scheme = proxy_config.get("scheme", "") | ||
hostname = proxy_config.get("hostname", "") | ||
port = proxy_config.get("port", "") | ||
username = proxy_config.get("username") | ||
password = proxy_config.get("password") | ||
|
||
proxies = {} | ||
if proxy_config: | ||
proxies = { | ||
"http": f"{scheme}://{hostname}:{port}", | ||
"https": f"{scheme}://{hostname}:{port}", | ||
} | ||
conn = http.client.HTTPSConnection("api.github.com") | ||
conn.request( | ||
method="GET", | ||
url="/repos/tangyoha/telegram_media_downloader/releases/latest", | ||
|
||
if username and password: | ||
proxies["http"] = f"{scheme}://{username}:{password}@{hostname}:{port}" | ||
proxies["https"] = f"{scheme}://{username}:{password}@{hostname}:{port}" | ||
try: | ||
response = requests.get( | ||
url="https://api.github.com/repos/tangyoha/telegram_media_downloader/releases/latest", | ||
headers=headers, | ||
proxies=proxies, | ||
timeout=60, | ||
) | ||
res = conn.getresponse() | ||
latest_release: dict = json.loads(res.read().decode("utf-8")) | ||
if f"v{__version__}" != latest_release["tag_name"]: | ||
update_message: str = ( | ||
f"## New version of Telegram-Media-Downloader is available - {latest_release['name']}\n" | ||
f"You are using an outdated version v{__version__} please pull in the changes using `git pull` or download the latest release.\n\n" | ||
f"Find more details about the latest release here - {latest_release['html_url']}" | ||
) | ||
console.print(Markdown(update_message)) | ||
|
||
except Exception as e: | ||
console.log( | ||
f"Following error occurred when checking for updates\n{e.__class__}, {e}" | ||
logger.warning(f"{e}") | ||
return {} | ||
|
||
latest_release: dict = json.loads(response.text) | ||
|
||
if f"v{__version__}" != latest_release["tag_name"]: | ||
return latest_release | ||
|
||
return {} | ||
|
||
|
||
def check_for_updates(proxy_config: dict = None): | ||
"""Checks for new releases. | ||
Using Github API checks for new release and prints information of new release if available. | ||
""" | ||
console = Console() | ||
latest_release = get_latest_release(proxy_config) | ||
if latest_release: | ||
update_message: str = ( | ||
f"## New version of Telegram-Media-Downloader is available - {latest_release['name']}\n" | ||
f"You are using an outdated version v{__version__} please pull in the changes using `git pull` or download the latest release.\n\n" | ||
f"Find more details about the latest release here - {latest_release['html_url']}" | ||
) | ||
console.print(Markdown(update_message)) |