Commit 0627529bbb9bde7365be707aeab24042cdbc1422
1 parent
6322efcaca
Exists in
master
added command line arguments, fixed bugs
Showing 1 changed file with 32 additions and 7 deletions Inline Diff
CheckViki.go
View file @
0627529
package main | 1 | 1 | package main | |
2 | 2 | |||
import ( | 3 | 3 | import ( | |
"fmt" | 4 | 4 | "fmt" | |
"net/http" | 5 | 5 | "net/http" | |
"io/ioutil" | 6 | 6 | "io/ioutil" | |
"encoding/json" | 7 | 7 | "encoding/json" | |
"log" | 8 | 8 | "log" | |
9 | "flag" | |||
10 | "strings" | |||
) | 9 | 11 | ) | |
10 | 12 | |||
// instance variables | 11 | 13 | // instance variables | |
var vikiApi = "https://api.viki.io/v4/" | 12 | 14 | var vikiApi = "https://api.viki.io/v4/" | |
var appId = "100444a" | 13 | 15 | var appId = "100444a" | |
14 | 16 | |||
17 | func compStr(s1, s2 string) bool { | |||
18 | left := strings.ToLower(s1) | |||
19 | right := strings.ToLower(s2) | |||
20 | return strings.Contains(left, right) | |||
21 | } | |||
22 | ||||
func report(err error) { | 15 | 23 | func report(err error) { | |
if (err != nil) { | 16 | 24 | if (err != nil) { | |
log.Fatal(err) | 17 | 25 | log.Fatal(err) | |
} | 18 | 26 | } | |
} | 19 | 27 | } | |
20 | 28 | |||
func getJson(url string) []byte{ | 21 | 29 | func getJson(url string) []byte{ | |
// grabs url response | 22 | 30 | // grabs url response | |
resp, err := http.Get(url) | 23 | 31 | resp, err := http.Get(url) | |
report(err) | 24 | 32 | report(err) | |
25 | 33 | |||
defer resp.Body.Close() | 26 | 34 | defer resp.Body.Close() | |
27 | 35 | |||
// retrieves body | 28 | 36 | // retrieves body | |
body, err := ioutil.ReadAll(resp.Body) | 29 | 37 | body, err := ioutil.ReadAll(resp.Body) | |
report(err) | 30 | 38 | report(err) | |
31 | 39 | |||
return body | 32 | 40 | return body | |
} | 33 | 41 | } | |
34 | 42 | |||
func getId(name string) string{ | 35 | 43 | func getId(rawName string) string{ | |
44 | name := strings.Replace(rawName, " ", "+", -1) | |||
lookUp := "search.json?c=" + name + "&" | 36 | 45 | lookUp := "search.json?c=" + name + "&" | |
url := vikiApi + lookUp + "app=" + appId | 37 | 46 | url := vikiApi + lookUp + "app=" + appId | |
search := getJson(url) | 38 | 47 | search := getJson(url) | |
39 | 48 | |||
// sets up struct | 40 | 49 | // sets up struct | |
type Result struct { | 41 | 50 | type Result struct { | |
Id string | 42 | 51 | Id string | |
52 | Tt string | |||
} | 43 | 53 | } | |
44 | 54 | |||
// parses result for series id | 45 | 55 | // parses result for series id | |
var res []Result | 46 | 56 | var res []Result | |
json.Unmarshal(search, &res) | 47 | 57 | json.Unmarshal(search, &res) | |
if len(res) != 0 { | 48 | 58 | if len(res) != 0 && compStr(res[0].Tt, rawName) { | |
return res[0].Id | 49 | 59 | return res[0].Id | |
} else { | 50 | 60 | } else { | |
61 | fmt.Println("series not found. blame viki api") | |||
return "" | 51 | 62 | return "" | |
} | 52 | 63 | } | |
} | 53 | 64 | } | |
54 | 65 | |||
func getCent(id string, ep int) int{ | 55 | 66 | func getCent(id string, ep int) int{ | |
// prep and grab json data | 56 | 67 | // prep and grab json data | |
lookup := "containers/" + id + "/episodes.json?" | 57 | 68 | lookup := "containers/" + id + "/episodes.json?" | |
url := vikiApi + lookup + "app=" + appId | 58 | 69 | url := vikiApi + lookup + "app=" + appId | |
eps := getJson(url) | 59 | 70 | eps := getJson(url) | |
60 | 71 | |||
// define the structure of the json | 61 | 72 | // define the structure of the json | |
type Ep struct { | 62 | 73 | type Ep struct { | |
Subtitle_completions struct { | 63 | 74 | Subtitle_completions struct { | |
En int | 64 | 75 | En int | |
} | 65 | 76 | } | |
77 | Number int | |||
} | 66 | 78 | } | |
67 | 79 | |||
type Resp struct { | 68 | 80 | type Resp struct { | |
Response []Ep | 69 | 81 | Response []Ep | |
} | 70 | 82 | } | |
71 | 83 | |||
// parse subtitle percents for episodes | 72 | 84 | // parse subtitle percents for episodes | |
res := &Resp{} | 73 | 85 | res := &Resp{} | |
err := json.Unmarshal(eps, &res) | 74 | 86 | err := json.Unmarshal(eps, &res) | |
report(err) | 75 | 87 | report(err) | |
76 | 88 | |||
// reverse index | 77 | 89 | // reverse index | |
numEps := len(res.Response) | 78 | 90 | numEps := res.Response[0].Number | |
i := numEps - ep | 79 | 91 | i := numEps - ep | |
92 | if (i > len(res.Response) - 1 || i < 0) { | |||
93 | fmt.Println("latest episode is:", numEps) | |||
94 | fmt.Println("queried episode too old or does not exist yet") | |||
95 | return -1 | |||
96 | } | |||
80 | 97 | |||
// retrieves percentage | 81 | 98 | // retrieves percentage | |
cent := res.Response[i].Subtitle_completions.En | 82 | 99 | cent := res.Response[i].Subtitle_completions.En | |
return cent | 83 | 100 | return cent | |
} | 84 | 101 | } | |
85 | 102 | |||
func main() { | 86 | 103 | func main() { | |
104 | // argument parser | |||
105 | var name = flag.String("series", "bong soon", "kdrama series to look up") | |||
106 | var ep = flag.Int("episode", 16, "episode number") | |||
107 | flag.Parse() |