-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelperDownloader.py
109 lines (104 loc) · 5.69 KB
/
HelperDownloader.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# coding=utf-8
import urllib.request
import sys, os, zipfile, webbrowser
localization = {
"notice":[
"\033[2J\033[0;0H\n--[Product of Apollo Zhu from WWITDC]--\n\n"
"Press `ENTER` to continue if you prefer using English\n"
"默认界面语言为英文。若需要更改为中文,请输入 zh \n" # Translation: Language is default to English. enter `zh` to use Chinese
],
"en":{
"networkIssue": "Network issue",
"sourceChanged": "The downlaod page has changed, developers are trying to solve this.\nPlease download the extension from {0}",
"subject": "Bug of BBHelper Donwloader",
"latestVersion": "Latest version: v",
"confirmDownload": "Start to download? (y/n, `ENTER` to start directly, or specify a version to download)",
"downloadingProgress": "\033[17DDownloading: {0:3}%",
"downloadError": "An error occured while downloading\nPlease check your network or specified version number",
"downloadCancel": "Cancelled downloading extension of versoin v{0}",
"downloadComplete": "Download completed!",
"unzip": "Start to unzip",
"unzipError": "An error occured while unzipping",
"unzipComplete": "Unzip completed",
"fileLocation": "Downloaded file is named `{1}` in {0}"
},
"zh":{
"networkIssue": "网络故障",
"sourceChanged": "网页内容发生改变,开发者正在处理中\n请直接从 {0} 下载",
"subject": "BiliBili助手下载器异常报告",
"latestVersion": "当前最新版本: v",
"confirmDownload": "是否需要下载? (y/n,直接回车开始,或是指定版本号):",
"downloadingProgress": "\033[14D下载进度:{0:3}%",
"downloadError": "下载过程出现问题\n请检查网络 或 输入版本号是否正确",
"downloadCancel": "已取消下载 v{0} 版本的 BiliBili助手",
"downloadComplete": "下载完成",
"unzip": "开始解压",
"unzipError": "解压过程出现问题",
"unzipComplete": "解压完成",
"fileLocation": "文件在 {0},名字为 {1}"
}
}
currentLanguage = "en"
def downloadProgressHandler(blockDownloadedCount, blockSize, totalSize):
percentage = 100.0 * blockDownloadedCount * blockSize / totalSize
if percentage >= 100:
percentage = 100
sys.stdout.write(localization[currentLanguage]["downloadingProgress"].format(int(percentage)))
sys.stdout.flush()
def downloadBiliBiliHelper():
sys.stdout.write("\033[2J\033[0;0H") # Clear screen
try:
link = "http://blackbili.nmzh.net/archives/bilibilihelper.html"
with urllib.request.urlopen(link) as download: # Update posting page
html = download.read().decode("utf-8")
try:
versionNumberLeft = html.index("更新 v") + 4
versionNumberRight = html.index("来啦!")
versionNumber = html[versionNumberLeft:versionNumberRight]
print(localization[currentLanguage]["latestVersion"]+ versionNumber)
response = input(localization[currentLanguage]["confirmDownload"])
if (response == "n" or response == "no"):
print(localization[currentLanguage]["downloadCancel"].format(versionNumber))
return
else:
try:
packageName = response
if (response == "y" or response == "yes" or response == ""):
packageName = versionNumber
packageName += ".zip"
source = "http://download.sueri.cn/software/Helper" + packageName
packagePath = os.getcwd() + "/" + packageName
urllib.request.urlretrieve(source, packagePath, downloadProgressHandler)
print("\n\033[;32m" + localization[currentLanguage]["downloadComplete"] + "\033[0m")
print(localization[currentLanguage]["unzip"])
try:
extensionName = ".crx"
with zipfile.ZipFile(packageName, 'r') as package:
for name in package.namelist():
if name.find(extensionName) > 0:
extensionName = name;
package.extract(extensionName)
break
if os.path.exists(extensionName):
os.remove(packagePath)
print("\033[;32m" + localization[currentLanguage]["unzipComplete"] + "\033[0m")
print(localization[currentLanguage]["fileLocation"].format(os.getcwd(),extensionName))
else:
raise IOError
except:
print("\033[;31m" + localization[currentLanguage]["unzipError"] + "\033[0m")
except:
print("\033[;31m" + localization[currentLanguage]["downloadError"] + "\033[0m")
except:
print("\033[;31m" + localization[currentLanguage]["sourceChanged"].format(link) + "\033[0m")
webbrowser.open("mailto:public-apollonian@outlook.com?subject=" + localization[currentLanguage]["subject"])
except:
print("\033[;31m" + localization[currentLanguage]["networkIssue"] + "\033[0m")
# Main entry
for notice in localization["notice"]:
print(notice)
print("=>", end=" ")
preferredLanguage = input()
if preferredLanguage in localization:
currentLanguage = preferredLanguage
downloadBiliBiliHelper()