diff --git a/CheckViki.go b/CheckViki.go index 4c3e15e..13355a5 100644 --- a/CheckViki.go +++ b/CheckViki.go @@ -6,12 +6,20 @@ import ( "io/ioutil" "encoding/json" "log" + "flag" + "strings" ) // instance variables var vikiApi = "https://api.viki.io/v4/" var appId = "100444a" +func compStr(s1, s2 string) bool { + left := strings.ToLower(s1) + right := strings.ToLower(s2) + return strings.Contains(left, right) +} + func report(err error) { if (err != nil) { log.Fatal(err) @@ -32,7 +40,8 @@ func getJson(url string) []byte{ return body } -func getId(name string) string{ +func getId(rawName string) string{ + name := strings.Replace(rawName, " ", "+", -1) lookUp := "search.json?c=" + name + "&" url := vikiApi + lookUp + "app=" + appId search := getJson(url) @@ -40,14 +49,16 @@ func getId(name string) string{ // sets up struct type Result struct { Id string + Tt string } // parses result for series id var res []Result json.Unmarshal(search, &res) - if len(res) != 0 { + if len(res) != 0 && compStr(res[0].Tt, rawName) { return res[0].Id } else { + fmt.Println("series not found. blame viki api") return "" } } @@ -63,6 +74,7 @@ func getCent(id string, ep int) int{ Subtitle_completions struct { En int } + Number int } type Resp struct { @@ -75,8 +87,13 @@ func getCent(id string, ep int) int{ report(err) // reverse index - numEps := len(res.Response) + numEps := res.Response[0].Number i := numEps - ep + if (i > len(res.Response) - 1 || i < 0) { + fmt.Println("latest episode is:", numEps) + fmt.Println("queried episode too old or does not exist yet") + return -1 + } // retrieves percentage cent := res.Response[i].Subtitle_completions.En @@ -84,9 +101,17 @@ func getCent(id string, ep int) int{ } func main() { + // argument parser + var name = flag.String("series", "bong soon", "kdrama series to look up") + var ep = flag.Int("episode", 16, "episode number") + flag.Parse() + // sets up variables - name := "bong+soon" - id := getId(name) - cent := getCent(id, 5) - fmt.Println(name, "subbed at", cent) + id := getId(*name) + if (len(id) == 0) { + return + } + + cent := getCent(id, *ep) + fmt.Println(*name, "episode", *ep, "subbed at", cent) }