-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.go
146 lines (121 loc) · 3.8 KB
/
generate.go
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
139
140
141
142
143
144
145
146
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
type Plugin struct {
Author string `json:"Author"`
Name string `json:"Name"`
Description string `json:"Description"`
Punchline string `json:"Punchline"`
Changelog string `json:"Changelog"`
Tags []string `json:"Tags"`
InternalName string `json:"InternalName"`
AssemblyVersion string `json:"AssemblyVersion"`
RepoUrl string `json:"RepoUrl"`
ApplicableVersion string `json:"ApplicableVersion"`
DalamudApiLevel int `json:"DalamudApiLevel"`
IconUrl string `json:"IconUrl"`
ImageUrls []string `json:"ImageUrls"`
DownloadLinkInstall string `json:"DownloadLinkInstall"`
IsHide bool `json:"IsHide"`
IsTestingExclusive bool `json:"IsTestingExclusive"`
DownloadLinkTesting string `json:"DownloadLinkTesting"`
DownloadLinkUpdate string `json:"DownloadLinkUpdate"`
DownloadCount int `json:"DownloadCount"`
LastUpdated int64 `json:"LastUpdated"`
}
type Release struct {
Assets []struct {
DownloadCount int `json:"download_count"`
UpdatedAt string `json:"updated_at"`
} `json:"assets"`
}
func main() {
var plugins []Plugin
folder, err := os.Open("Plugins")
if err != nil {
fmt.Println("error opening folder:", err)
return
}
defer folder.Close()
contents, err := folder.Readdir(-1)
if err != nil {
fmt.Println("error reading contents of folder:", err)
return
}
for _, info := range contents {
if info.IsDir() {
file, err := os.Open(filepath.Join("Plugins", info.Name(), info.Name()+".json"))
if err != nil {
fmt.Printf("error opening .json in %s: %v\n", info.Name(), err)
continue
}
defer file.Close()
bytes, err := io.ReadAll(file)
if err != nil {
fmt.Printf("error reading .json in %s: %v\n", info.Name(), err)
continue
}
var plugin Plugin
if err := json.Unmarshal(bytes, &plugin); err != nil {
fmt.Printf("error unmarshaling .json in %s: %v\n", info.Name(), err)
continue
}
downloadLink := plugin.RepoUrl + "/releases/latest/download/latest.zip"
plugin.DownloadLinkInstall = downloadLink
plugin.DownloadLinkTesting = downloadLink
plugin.DownloadLinkUpdate = downloadLink
//region get downloads from GitHub api
api := strings.Replace(plugin.RepoUrl, "github.com", "api.github.com/repos", -1) + "/releases"
getApi, _ := http.Get(api)
body, _ := io.ReadAll(getApi.Body)
var releases []Release
err = json.Unmarshal(body, &releases)
if err != nil {
fmt.Println(err)
return
}
var totalDownloadCount int
for _, release := range releases {
for _, asset := range release.Assets {
totalDownloadCount += asset.DownloadCount
}
}
//endregion
//region get latest update time from GitHub api
latestApi := api + "/latest"
getLatestApi, _ := http.Get(latestApi)
body, _ = io.ReadAll(getLatestApi.Body)
var latestRelease Release
err = json.Unmarshal(body, &latestRelease)
if err != nil {
return
}
var updatedString = latestRelease.Assets[0].UpdatedAt
var lastUpdated, _ = time.Parse(time.RFC3339, updatedString)
//endregion
plugin.DownloadCount = totalDownloadCount
plugin.LastUpdated = lastUpdated.Unix()
plugins = append(plugins, plugin)
fmt.Println(fmt.Sprintf("added %s to plugin manifest", plugin.Name))
}
}
bytes, err := json.MarshalIndent(plugins, "", " ")
if err != nil {
fmt.Println("error marshaling plugins:", err)
return
}
err = os.WriteFile("pluginmaster.json", bytes, 0644)
if err != nil {
fmt.Println("error writing to file:", err)
return
}
fmt.Println(fmt.Sprintf("successfully generated pluginmaster with %d plugins", len(contents)))
}