CheckViki.go
2.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"fmt"
"net/http"
"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)
}
}
func getJson(url string) []byte{
// grabs url response
resp, err := http.Get(url)
report(err)
defer resp.Body.Close()
// retrieves body
body, err := ioutil.ReadAll(resp.Body)
report(err)
return body
}
func getId(rawName string) string{
name := strings.Replace(rawName, " ", "+", -1)
lookUp := "search.json?c=" + name + "&"
url := vikiApi + lookUp + "app=" + appId
search := getJson(url)
// 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 && compStr(res[0].Tt, rawName) {
return res[0].Id
} else {
fmt.Println("series not found. blame viki api")
return ""
}
}
func getCent(id string, ep int) int{
// prep and grab json data
lookup := "containers/" + id + "/episodes.json?"
url := vikiApi + lookup + "app=" + appId
eps := getJson(url)
// define the structure of the json
type Ep struct {
Subtitle_completions struct {
En int
}
Number int
}
type Resp struct {
Response []Ep
}
// parse subtitle percents for episodes
res := &Resp{}
err := json.Unmarshal(eps, &res)
report(err)
// reverse index
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
return cent
}
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
id := getId(*name)
if (len(id) == 0) {
return
}
cent := getCent(id, *ep)
fmt.Println(*name, "episode", *ep, "subbed at", cent)
}