Skip to content

Instantly share code, notes, and snippets.

@Vic020
Created October 10, 2020 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vic020/bfef14ea2938adf45f6d98a79a4825ca to your computer and use it in GitHub Desktop.
Save Vic020/bfef14ea2938adf45f6d98a79a4825ca to your computer and use it in GitHub Desktop.
mac版本大脚更新脚本
package main
import (
"archive/zip"
"bytes"
"fmt"
"gopkg.in/cheggaaa/pb.v1"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"sync"
"time"
)
var (
UpdateUrl = "http://bigfoot.178.com/wow/update.html"
InterfacePackageUrl = "http://wow.bfupdate.178.com/BigFoot/Interface/3.1/Interface.%s.zip"
)
var (
WorkDir = "/tmp/wow-bigfoot-for-mac"
GameDir = `/Applications/World of Warcraft/`
InterfaceDir = "_retail_"
)
var (
goRoutineGroup sync.WaitGroup
logger *log.Logger
)
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func httpGet(url string) []byte {
resp, err := http.Get(url)
checkError(err)
defer resp.Body.Close()
bytes, err := ioutil.ReadAll(resp.Body)
checkError(err)
return bytes
}
func getLatestPackageVersion() string {
html := httpGet(UpdateUrl)
r := regexp.MustCompile(`>V([\d.]+)版本说明`)
version := string(r.FindSubmatch(html)[1])
log.Println("Latest Version is: ", version)
return version
}
func downloadLatestPackage(version string) string {
// TODO: check package downloaded
log.Printf("Downloading %s version package", version)
downloadUrl := fmt.Sprintf(InterfacePackageUrl, version)
packName := fmt.Sprintf("update.%s.zip", version)
filePath := filepath.Join(WorkDir, packName)
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
log.Println("Package existed Path:", filePath)
return filePath
}
if _, err := os.Stat(WorkDir); os.IsNotExist(err) {
e := os.Mkdir(WorkDir, 0755)
checkError(e)
}
// Get package size
response, e := http.Head(downloadUrl)
defer response.Body.Close()
checkError(e)
length, e := strconv.Atoi(response.Header.Get("Content-Length"))
checkError(e)
file, err := os.Create(filePath)
defer file.Close()
checkError(err)
start := time.Now()
resp, err := http.Get(downloadUrl)
defer resp.Body.Close()
checkError(err)
bar := pb.New(length).SetUnits(pb.U_BYTES)
bar.ShowBar = true
bar.Start()
proxyReader := bar.NewProxyReader(resp.Body)
_, err = io.Copy(file, proxyReader)
checkError(err)
log.Printf("Download %s version package cost %s", version, time.Now().Sub(start))
return filePath
}
func unZipPackage(filePath string) string {
file, e := ioutil.ReadFile(filePath)
checkError(e)
reader, e := zip.NewReader(bytes.NewReader(file), int64(len(file)))
checkError(e)
start := time.Now()
for _, zipFile := range reader.File {
if zipFile.FileInfo().IsDir() {
createFolder(zipFile)
} else {
goRoutineGroup.Add(1)
go createFile(zipFile)
}
}
goRoutineGroup.Wait()
log.Printf("Unzip Cost: %s", time.Now().Sub(start))
return WorkDir
}
func createFile(file *zip.File) {
f, e := file.Open()
defer f.Close()
checkError(e)
ctx, e := ioutil.ReadAll(f)
checkError(e)
fileFullPath := filepath.Join(GameDir, InterfaceDir, file.Name)
e = ioutil.WriteFile(fileFullPath, ctx, file.Mode())
checkError(e)
log.Println("Written file to:", fileFullPath)
goRoutineGroup.Done()
}
func createFolder(file *zip.File) {
fileFullPath := filepath.Join(GameDir, InterfaceDir, file.Name)
e := os.MkdirAll(fileFullPath, file.Mode())
checkError(e)
log.Println("Written folder to:", fileFullPath)
}
func main() {
// single file to download interface
// 1. check weather updated
// 2. download the latest version interface zip package
// 3. unzip and move to game folder
version := getLatestPackageVersion()
filePath := downloadLatestPackage(version)
unZipPackage(filePath)
log.Println("Done.")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment