No longer needs to download the checksum to get the file name. Does a
HEAD request on the installer and uses the file name on the url path after redirects to name it. Also adds downloading the xml checksum.
This commit is contained in:
parent
855e7ff186
commit
89fad8b62b
72
main.go
72
main.go
@ -35,7 +35,7 @@ func saveToken(tok *oauth2.Token, filename string) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to marshal token: %v", err)
|
log.Fatalf("Failed to marshal token: %v", err)
|
||||||
}
|
}
|
||||||
err = ioutil.WriteFile(filename, j, 0664)
|
err = ioutil.WriteFile(filename, j, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to save token: %v", err)
|
log.Fatalf("Failed to save token: %v", err)
|
||||||
}
|
}
|
||||||
@ -215,36 +215,39 @@ func getDownloadLinks(p *Product) []*DownloadLinks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func sanitizeName(name string) string {
|
func sanitizeName(name string) string {
|
||||||
s := strings.Replace(name, " ", "_", -1)
|
// s := strings.Replace(name, " ", "_", -1)
|
||||||
s = strings.Replace(s, "'", "", -1)
|
// s = strings.Replace(s, "'", "", -1)
|
||||||
s = strings.Replace(s, "™", "", -1)
|
// s = strings.Replace(s, "™", "", -1)
|
||||||
|
re := regexp.MustCompile("[^0-1a-zA-Z.-_]+")
|
||||||
|
s := name
|
||||||
|
s = re.ReplaceAllString(s, "_")
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeChecksumIfNeeded(dl *DownloadLinks, folder string) {
|
// func writeChecksumIfNeeded(dl *DownloadLinks, folder string) {
|
||||||
// TODO: reuse an existing checksum file somehow
|
// // TODO: reuse an existing checksum file somehow
|
||||||
res, err := client.Get(dl.Checksum)
|
// res, err := client.Get(dl.Checksum)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Fatalf("Failed to get file checksum info: %v", err)
|
// log.Fatalf("Failed to get file checksum info (%s): %v", dl.Checksum, err)
|
||||||
}
|
// }
|
||||||
defer res.Body.Close()
|
// defer res.Body.Close()
|
||||||
body, err := ioutil.ReadAll(res.Body)
|
// body, err := ioutil.ReadAll(res.Body)
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
log.Fatalf("Failed to parse file checksum body: %v", err)
|
// log.Fatalf("Failed to parse file checksum body (%s): %v", dl.Checksum, err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
re := regexp.MustCompile(`<file name="([^"]+)".*md5="([^"]+)`)
|
// re := regexp.MustCompile(`<file name="([^"]+)".*md5="([^"]+)`)
|
||||||
m := re.FindSubmatch(body)
|
// m := re.FindSubmatch(body)
|
||||||
if m != nil {
|
// if m != nil {
|
||||||
dl.Filename = sanitizeName(string(m[1]))
|
// dl.Filename = sanitizeName(string(m[1]))
|
||||||
dl.Md5 = string(m[2])
|
// dl.Md5 = string(m[2])
|
||||||
|
|
||||||
// err = ioutil.WriteFile(folder + "/" + sanitizeName(dl.InstallerName) + "/" + dl.Filename + ".md5", m[2], 0664)
|
// // err = ioutil.WriteFile(folder + "/" + sanitizeName(dl.InstallerName) + "/" + dl.Filename + ".md5", m[2], 0664)
|
||||||
// if err != nil {
|
// // if err != nil {
|
||||||
// log.Fatalf("Failed to write the checksum file: %v", err)
|
// // log.Fatalf("Failed to write the checksum file: %v", err)
|
||||||
// }
|
// // }
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
|
|
||||||
func updateBashScriptProductFolder(p *Product, folder string, scriptName string) {
|
func updateBashScriptProductFolder(p *Product, folder string, scriptName string) {
|
||||||
@ -263,7 +266,20 @@ func updateBashScriptProductFolder(p *Product, folder string, scriptName string)
|
|||||||
|
|
||||||
|
|
||||||
func writeBashScript(dl *DownloadLinks, folder string, scriptName string) {
|
func writeBashScript(dl *DownloadLinks, folder string, scriptName string) {
|
||||||
line := "wget --no-clobber --continue --quiet --show-progress -O \"" + folder + "/" + sanitizeName(dl.InstallerName) + "/" + dl.Filename + "\" \"" + dl.Downlink + "\"\n"
|
// Find out file name and size; have to follow redirects
|
||||||
|
res, err := client.Head(dl.Downlink)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to do a head request for the download file: %v", err)
|
||||||
|
}
|
||||||
|
defer res.Body.Close()
|
||||||
|
// log.Printf("head request url: %+v", res.Request.URL.Path)
|
||||||
|
|
||||||
|
pp := strings.Split(res.Request.URL.Path, "/")
|
||||||
|
dl.Filename = sanitizeName(pp[len(pp) - 1])
|
||||||
|
|
||||||
|
// Add the lines to the script
|
||||||
|
line := "wget --no-clobber --continue --quiet --show-progress -O \"" + folder + "/" + sanitizeName(dl.InstallerName) + "/" + dl.Filename + ".xml\" \"" + dl.Checksum + "\"\n"
|
||||||
|
line += "wget --no-clobber --continue --quiet --show-progress -O \"" + folder + "/" + sanitizeName(dl.InstallerName) + "/" + dl.Filename + "\" \"" + dl.Downlink + "\"\n"
|
||||||
|
|
||||||
f, err := os.OpenFile("./" + scriptName, os.O_APPEND|os.O_WRONLY, 0664)
|
f, err := os.OpenFile("./" + scriptName, os.O_APPEND|os.O_WRONLY, 0664)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -335,7 +351,7 @@ func main() {
|
|||||||
updateBashScriptProductFolder(p, productsFolder, scriptName)
|
updateBashScriptProductFolder(p, productsFolder, scriptName)
|
||||||
|
|
||||||
for _, d := range dl {
|
for _, d := range dl {
|
||||||
writeChecksumIfNeeded(d, productsFolder + "/" + sanitizeName(p.Title))
|
//writeChecksumIfNeeded(d, productsFolder + "/" + sanitizeName(p.Title))
|
||||||
writeBashScript(d, productsFolder, scriptName)
|
writeBashScript(d, productsFolder, scriptName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user