-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
autoupdate_main.py
75 lines (63 loc) · 2.17 KB
/
autoupdate_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import os
import sys
from io import BytesIO
from zipfile import ZipFile
import requests
def update(version: str):
url = "https://github.com/charlesbel/Microsoft-Rewards-Farmer/archive/refs/heads/master.zip"
folderName = "Microsoft-Rewards-Farmer-master"
with open(".gitignore", "r") as f:
exclusions = f.read().splitlines()
exclusions = [e for e in exclusions if e != "" and not e.startswith("#")] + [
".gitignore",
".git",
]
print("Removing old files...")
for root, dirs, files in os.walk(".", topdown=False):
for name in files:
path = os.path.join(root, name)
relativePath = path[2:]
if not relativePath.startswith(tuple(exclusions)):
os.remove(path)
print("Downloading...")
r = requests.get(url)
data = BytesIO(r.content)
print("Extracting...")
with ZipFile(data, "r") as zipObj:
files = [
f
for f in zipObj.namelist()
if f.startswith(folderName) and not f.endswith("/")
]
for file in files:
newName = file.replace(f"{folderName}/", "")
dirName = os.path.dirname(newName)
if "/" in newName and not os.path.exists(dirName):
print(f"Creating folder {dirName}...")
os.makedirs(dirName)
with zipObj.open(file) as src, open(newName, "wb") as dst:
dst.write(src.read())
with open("version.txt", "w") as f:
f.write(version)
print("Done !")
def getCurrentVersion():
if os.path.exists("version.txt"):
with open("version.txt", "r") as f:
version = f.read()
return version
return None
def getLatestVersion():
r = requests.get(
"https://api.github.com/repos/charlesbel/Microsoft-Rewards-Farmer/commits/master"
)
return r.json()["sha"]
if __name__ == "__main__":
currentVersion = getCurrentVersion()
latestVersion = getLatestVersion()
if currentVersion != latestVersion:
print("New version available !")
update(latestVersion)
print("Starting...")
import main
main.sys.argv[1:] = sys.argv[1:]
main.main()