forked from ottercorp/DalamudPlugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_pluginmaster.py
138 lines (114 loc) · 4.55 KB
/
generate_pluginmaster.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import json
import os
import codecs
from time import time
from sys import argv
from os.path import getmtime
from zipfile import ZipFile, ZIP_DEFLATED
# DOWNLOAD_URL = 'https://dalamudplugins-1253720819.cos.ap-nanjing.myqcloud.com/plugins/{plugin_name}/latest.zip'
DOWNLOAD_URL = 'https://service-knj2phup-1253720819.sh.apigw.tencentcs.com/release/dalamudcounter-1623520723?plugin={plugin_name}&isUpdate={is_update}&isTesting={is_testing}&branch=cn'
DEFAULTS = {
'IsHide': False,
'IsTestingExclusive': False,
'ApplicableVersion': 'any',
}
DUPLICATES = {
'DownloadLinkInstall': ['DownloadLinkTesting', 'DownloadLinkUpdate'],
}
TRIMMED_KEYS = [
'Author',
'Name',
'Description',
'InternalName',
'AssemblyVersion',
'RepoUrl',
'ApplicableVersion',
'Tags',
'DalamudApiLevel',
]
def main():
# extract the manifests from inside the zip files
master = extract_manifests()
# trim the manifests
master = [trim_manifest(manifest) for manifest in master]
# convert the list of manifests into a master list
add_extra_fields(master)
# write the master
write_master(master)
# update the LastUpdated field in master
last_updated()
# update the Markdown
update_md(master)
def extract_manifests():
manifests = []
for dirpath, dirnames, filenames in os.walk('./plugins'):
if len(filenames) == 0 or 'latest.zip' not in filenames:
continue
plugin_name = dirpath.split('/')[-1].split('\\')[-1]
latest_json = f'{dirpath}/{plugin_name}.json'
with codecs.open(latest_json, "r", "utf-8") as f:
content = f.read()
if content.startswith(u'\ufeff'):
content = content.encode('utf8')[3:].decode('utf8')
manifests.append(json.loads(content))
translations = {}
with codecs.open("translations.json", "r", "utf-8") as f:
translations = json.load(f)
for manifest in manifests:
desc = manifest.get('Description')
if desc and desc not in translations:
translations[desc] = ""
with codecs.open("translations.json", "w", "utf-8") as f:
json.dump(translations, f, indent=4)
for manifest in manifests:
desc = manifest.get('Description')
if desc in translations:
manifest['Description'] = translations[desc]
return manifests
def add_extra_fields(manifests):
downloadcounts = {}
if os.path.exists('downloadcounts.json'):
with open('downloadcounts.json', 'r') as f:
downloadcounts = json.load(f)
for manifest in manifests:
# generate the download link from the internal assembly name
manifest['DownloadLinkInstall'] = DOWNLOAD_URL.format(plugin_name=manifest["InternalName"], is_update=False, is_testing=False)
manifest['DownloadLinkUpdate'] = DOWNLOAD_URL.format(plugin_name=manifest["InternalName"], is_update=True, is_testing=False)
manifest['DownloadLinkTesting'] = DOWNLOAD_URL.format(plugin_name=manifest["InternalName"], is_update=False, is_testing=True)
# add default values if missing
for k, v in DEFAULTS.items():
if k not in manifest:
manifest[k] = v
# duplicate keys as specified in DUPLICATES
for source, keys in DUPLICATES.items():
for k in keys:
if k not in manifest:
manifest[k] = manifest[source]
manifest['DownloadCount'] = downloadcounts.get(manifest["InternalName"], 0)
def write_master(master):
# write as pretty json
with open('pluginmaster.json', 'w') as f:
json.dump(master, f, indent=4)
def trim_manifest(plugin):
return {k: plugin[k] for k in TRIMMED_KEYS if k in plugin}
def last_updated():
with open('pluginmaster.json') as f:
master = json.load(f)
for plugin in master:
latest = f'plugins/{plugin["InternalName"]}/latest.zip'
modified = int(getmtime(latest))
if 'LastUpdated' not in plugin or modified != int(plugin['LastUpdated']):
plugin['LastUpdated'] = str(modified)
with open('pluginmaster.json', 'w') as f:
json.dump(master, f, indent=4)
def update_md(master):
with codecs.open('mdtemplate.txt', 'r', 'utf8') as mdt:
md = mdt.read()
for plugin in master:
desc = plugin.get('Description', '')
desc = desc.replace('\n', '<br>')
md += f"\n| {plugin['Name']} | {plugin['Author']} | {desc} |"
with codecs.open('plugins.md', 'w', 'utf8') as f:
f.write(md)
if __name__ == '__main__':
main()