1
+ import requests
2
+ import random
3
+ import base64
4
+ from termcolor import colored
5
+ import inquirer
6
+ import webbrowser
7
+ import os
8
+ from time import sleep
9
+ from alive_progress import alive_bar
10
+
11
+ from main import version
12
+ from .logger import Logger
13
+ from .pretty_print import error , ok
14
+
15
+ class AutoUpdate :
16
+
17
+ def Checker ():
18
+ try :
19
+ url = "https://api.github.com/repos/Official-Husko/NN-Downloader/releases/latest"
20
+
21
+ headers = {
22
+ "User-Agent" :f"nn-downloader/{ version } (by Official Husko on GitHub)" ,
23
+ "Accept" : "application/vnd.github+json" ,
24
+ "X-GitHub-Api-Version" : "2022-11-28"
25
+ }
26
+
27
+ req = requests .get (url , headers = headers ).json ()
28
+ repo_version = req .get ("tag_name" )
29
+ download_link = req ["assets" ][0 ]["browser_download_url" ]
30
+
31
+ if str (version ) < repo_version :
32
+ print (colored ("UPDATE AVAILABLE! " , "red" , attrs = ["blink" ]))
33
+
34
+ body = req .get ("body" )
35
+ name = req .get ("name" )
36
+ date = req .get ("published_at" ).replace ("T" , " " ).replace ("Z" , "" )
37
+
38
+ print ("" )
39
+ print (f"Latest release is { colored (name , 'light_blue' )} released on { colored (date , 'yellow' )} " )
40
+ print ("" )
41
+ print (body )
42
+ print ("" )
43
+ amount_question = [
44
+ inquirer .List ('selection' ,
45
+ message = colored ("Do you want to download the update?" , "green" ),
46
+ choices = ["Yes" , "No" ],
47
+ ),
48
+ ]
49
+ amount_answers = inquirer .prompt (amount_question )
50
+ print ("" )
51
+ decision = amount_answers .get ("selection" )
52
+ if decision == "Yes" :
53
+ r = requests .get (download_link , headers = {"User-Agent" :f"nn-downloader/{ version } (by Official Husko on GitHub)" }, timeout = 5 , stream = True )
54
+ with alive_bar (int (int (r .headers .get ('content-length' )) / 1024 + 1 )) as bar :
55
+ bar .text = f'-> Downloading Update { repo_version } , please wait...'
56
+ file = open (f"nn-downloader-{ repo_version } .exe" , 'wb' )
57
+ for chunk in r .iter_content (chunk_size = 1024 ):
58
+ if chunk :
59
+ file .write (chunk )
60
+ file .flush ()
61
+ bar ()
62
+ print (f"{ ok } Update successfully downloaded! The program will now close and delete the old exe." )
63
+ if os .path .exists ("delete-exe.bat" ):
64
+ os .remove ("delete-exe.bat" )
65
+ with open ("delete-exe.bat" , "a" ) as bat_creator :
66
+ bat_content = f'TASKKILL -F /IM NN-Downloader.exe\n timeout 3\n DEL .\\ NN-Downloader.exe\n ren .\\ nn-downloader-{ repo_version } .exe NN-Downloader.exe\n DEL .\\ delete-exe.bat'
67
+ bat_creator .write (bat_content )
68
+ bat_creator .close ()
69
+ os .startfile (r".\\delete-exe.bat" )
70
+ sleep (5 )
71
+ exit (0 )
72
+ elif decision == "No" :
73
+ if not os .path .exists ("outdated" ):
74
+ with open ("outdated" , "a" ) as mark_outdated :
75
+ mark_outdated .close ()
76
+ elif str (version ) >= repo_version :
77
+ try :
78
+ os .remove ("outdated" )
79
+ except Exception :
80
+ pass
81
+
82
+ except Exception as e :
83
+ # Construct and print the error
84
+ error_str = f"An error occured while checking for updates! Please report this. Exception: { e } "
85
+ print (f"{ error } { error_str } " )
86
+ Logger .log_event (error_str , req )
87
+ sleep (7 )
0 commit comments