mirror of
https://github.com/Tilo-K/ddragon-cdn.git
synced 2026-01-09 00:21:01 +00:00
First commit
This commit is contained in:
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
*.exe
|
||||
current.txt
|
||||
error.log
|
||||
ddragon-*.tgz
|
||||
data/
|
||||
9
go.mod
Normal file
9
go.mod
Normal file
@@ -0,0 +1,9 @@
|
||||
module tilo-k/ddragon-cdn
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 // indirect
|
||||
github.com/otiai10/copy v1.9.0 // indirect
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
|
||||
)
|
||||
12
go.sum
Normal file
12
go.sum
Normal file
@@ -0,0 +1,12 @@
|
||||
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs=
|
||||
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
|
||||
github.com/otiai10/copy v1.9.0 h1:7KFNiCgZ91Ru4qW4CWPf/7jqtxLagGRmIxWldPP9VY4=
|
||||
github.com/otiai10/copy v1.9.0/go.mod h1:hsfX19wcn0UWIHUQ3/4fHuehhk2UyArQ9dVFAn3FczI=
|
||||
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
|
||||
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
|
||||
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
|
||||
github.com/otiai10/mint v1.4.0/go.mod h1:gifjb2MYOoULtKLqUAEILUG/9KONW6f7YsJ6vQLTlFI=
|
||||
github.com/walle/targz v0.0.0-20140417120357-57fe4206da5a h1:6cKSHLRphD9Fo1LJlISiulvgYCIafJ3QfKLimPYcAGc=
|
||||
github.com/walle/targz v0.0.0-20140417120357-57fe4206da5a/go.mod h1:nccQrXCnc5SjsThFLmL7hYbtT/mHJcuolPifzY5vJqE=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
|
||||
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
122
main.go
Normal file
122
main.go
Normal file
@@ -0,0 +1,122 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"github.com/alcortesm/tgz"
|
||||
"github.com/otiai10/copy"
|
||||
)
|
||||
|
||||
func getVersions() []string {
|
||||
url := "https://ddragon.leagueoflegends.com/api/versions.json"
|
||||
|
||||
res, err := http.Get(url)
|
||||
checkError(err)
|
||||
defer res.Body.Close()
|
||||
|
||||
body, err := ioutil.ReadAll(res.Body)
|
||||
checkError(err)
|
||||
|
||||
versions := make([]string, 0)
|
||||
json.Unmarshal(body, &versions)
|
||||
|
||||
return versions
|
||||
}
|
||||
|
||||
func loadDdragon(version string) string {
|
||||
url := fmt.Sprintf("https://ddragon.leagueoflegends.com/cdn/dragontail-%s.tgz", version)
|
||||
filename := "ddragon-" + version + ".tgz"
|
||||
|
||||
if _, err := os.Stat(filename); os.IsNotExist(err) {
|
||||
res, err := http.Get(url)
|
||||
checkError(err)
|
||||
defer res.Body.Close()
|
||||
|
||||
out, err := os.Create(filename)
|
||||
checkError(err)
|
||||
defer out.Close()
|
||||
|
||||
io.Copy(out, res.Body)
|
||||
}
|
||||
|
||||
return filename
|
||||
}
|
||||
|
||||
func getCurrentVersion() string {
|
||||
_, err := os.Stat("current.txt")
|
||||
if os.IsNotExist(err) {
|
||||
return ""
|
||||
}
|
||||
|
||||
_, err = os.Stat("data")
|
||||
if os.IsNotExist(err) {
|
||||
return ""
|
||||
}
|
||||
|
||||
data, err := ioutil.ReadFile("current.txt")
|
||||
checkError(err)
|
||||
|
||||
return string(data)
|
||||
}
|
||||
|
||||
func loadCurrent() {
|
||||
versions := getVersions()
|
||||
if getCurrentVersion() != versions[0] {
|
||||
file := loadDdragon(versions[0])
|
||||
ioutil.WriteFile("current.txt", []byte(versions[0]), 0777)
|
||||
|
||||
path, err := tgz.Extract(file)
|
||||
checkError(err)
|
||||
|
||||
_, err = os.Stat("data")
|
||||
if !os.IsNotExist(err) {
|
||||
err = os.Remove("data")
|
||||
}
|
||||
checkError(err)
|
||||
|
||||
dest, err := filepath.Abs("./data")
|
||||
checkError(err)
|
||||
err = os.Rename(path, dest)
|
||||
if err != nil {
|
||||
copy.Copy(path, dest)
|
||||
}
|
||||
os.RemoveAll(path)
|
||||
|
||||
src, _ := filepath.Abs(filepath.Join("data", versions[0]))
|
||||
dst, _ := filepath.Abs(filepath.Join("data", "latest"))
|
||||
|
||||
err = os.Rename(src, dst)
|
||||
checkError(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
loadCurrent()
|
||||
|
||||
go func() {
|
||||
dur, err := time.ParseDuration("30m")
|
||||
checkError(err)
|
||||
|
||||
for true {
|
||||
time.Sleep(dur)
|
||||
loadCurrent()
|
||||
}
|
||||
}()
|
||||
|
||||
fs := http.FileServer(http.Dir("./data"))
|
||||
http.Handle("/", fs)
|
||||
|
||||
log.Print("Listening on :60002...")
|
||||
err := http.ListenAndServe(":60002", nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
18
util.go
Normal file
18
util.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
func checkError(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println("[!] ", err)
|
||||
f, _ := os.OpenFile("error.log", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
||||
v, _ := time.Now().UTC().MarshalText()
|
||||
f.WriteString(fmt.Sprintf("[%s] %s\n", string(v), err))
|
||||
}
|
||||
Reference in New Issue
Block a user