Commit 0627529bbb9bde7365be707aeab24042cdbc1422
1 parent
6322efcaca
Exists in
master
added command line arguments, fixed bugs
Showing 1 changed file with 32 additions and 7 deletions Side-by-side Diff
CheckViki.go
View file @
0627529
... | ... | @@ -6,12 +6,20 @@ |
6 | 6 | "io/ioutil" |
7 | 7 | "encoding/json" |
8 | 8 | "log" |
9 | + "flag" | |
10 | + "strings" | |
9 | 11 | ) |
10 | 12 | |
11 | 13 | // instance variables |
12 | 14 | var vikiApi = "https://api.viki.io/v4/" |
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 | + | |
15 | 23 | func report(err error) { |
16 | 24 | if (err != nil) { |
17 | 25 | log.Fatal(err) |
... | ... | @@ -32,7 +40,8 @@ |
32 | 40 | return body |
33 | 41 | } |
34 | 42 | |
35 | -func getId(name string) string{ | |
43 | +func getId(rawName string) string{ | |
44 | + name := strings.Replace(rawName, " ", "+", -1) | |
36 | 45 | lookUp := "search.json?c=" + name + "&" |
37 | 46 | url := vikiApi + lookUp + "app=" + appId |
38 | 47 | search := getJson(url) |
39 | 48 | |
40 | 49 | |
... | ... | @@ -40,14 +49,16 @@ |
40 | 49 | // sets up struct |
41 | 50 | type Result struct { |
42 | 51 | Id string |
52 | + Tt string | |
43 | 53 | } |
44 | 54 | |
45 | 55 | // parses result for series id |
46 | 56 | var res []Result |
47 | 57 | json.Unmarshal(search, &res) |
48 | - if len(res) != 0 { | |
58 | + if len(res) != 0 && compStr(res[0].Tt, rawName) { | |
49 | 59 | return res[0].Id |
50 | 60 | } else { |
61 | + fmt.Println("series not found. blame viki api") | |
51 | 62 | return "" |
52 | 63 | } |
53 | 64 | } |
... | ... | @@ -63,6 +74,7 @@ |
63 | 74 | Subtitle_completions struct { |
64 | 75 | En int |
65 | 76 | } |
77 | + Number int | |
66 | 78 | } |
67 | 79 | |
68 | 80 | type Resp struct { |
69 | 81 | |
... | ... | @@ -75,8 +87,13 @@ |
75 | 87 | report(err) |
76 | 88 | |
77 | 89 | // reverse index |
78 | - numEps := len(res.Response) | |
90 | + numEps := res.Response[0].Number | |
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 | |
81 | 98 | // retrieves percentage |
82 | 99 | cent := res.Response[i].Subtitle_completions.En |
83 | 100 | |
... | ... | @@ -84,10 +101,18 @@ |
84 | 101 | } |
85 | 102 | |
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() | |
108 | + | |
87 | 109 | // sets up variables |
88 | - name := "bong+soon" | |
89 | - id := getId(name) | |
90 | - cent := getCent(id, 5) | |
91 | - fmt.Println(name, "subbed at", cent) | |
110 | + id := getId(*name) | |
111 | + if (len(id) == 0) { | |
112 | + return | |
113 | + } | |
114 | + | |
115 | + cent := getCent(id, *ep) | |
116 | + fmt.Println(*name, "episode", *ep, "subbed at", cent) | |
92 | 117 | } |