Commit 6322efcacad82744587a7c1df4b85b296b726c99

Authored by Benjamin Tea
0 parents
Exists in master

first commit

Showing 1 changed file with 92 additions and 0 deletions Side-by-side Diff

CheckViki.go View file @ 6322efc
... ... @@ -0,0 +1,92 @@
  1 +package main
  2 +
  3 +import (
  4 + "fmt"
  5 + "net/http"
  6 + "io/ioutil"
  7 + "encoding/json"
  8 + "log"
  9 +)
  10 +
  11 +// instance variables
  12 +var vikiApi = "https://api.viki.io/v4/"
  13 +var appId = "100444a"
  14 +
  15 +func report(err error) {
  16 + if (err != nil) {
  17 + log.Fatal(err)
  18 + }
  19 +}
  20 +
  21 +func getJson(url string) []byte{
  22 + // grabs url response
  23 + resp, err := http.Get(url)
  24 + report(err)
  25 +
  26 + defer resp.Body.Close()
  27 +
  28 + // retrieves body
  29 + body, err := ioutil.ReadAll(resp.Body)
  30 + report(err)
  31 +
  32 + return body
  33 +}
  34 +
  35 +func getId(name string) string{
  36 + lookUp := "search.json?c=" + name + "&"
  37 + url := vikiApi + lookUp + "app=" + appId
  38 + search := getJson(url)
  39 +
  40 + // sets up struct
  41 + type Result struct {
  42 + Id string
  43 + }
  44 +
  45 + // parses result for series id
  46 + var res []Result
  47 + json.Unmarshal(search, &res)
  48 + if len(res) != 0 {
  49 + return res[0].Id
  50 + } else {
  51 + return ""
  52 + }
  53 +}
  54 +
  55 +func getCent(id string, ep int) int{
  56 + // prep and grab json data
  57 + lookup := "containers/" + id + "/episodes.json?"
  58 + url := vikiApi + lookup + "app=" + appId
  59 + eps := getJson(url)
  60 +
  61 + // define the structure of the json
  62 + type Ep struct {
  63 + Subtitle_completions struct {
  64 + En int
  65 + }
  66 + }
  67 +
  68 + type Resp struct {
  69 + Response []Ep
  70 + }
  71 +
  72 + // parse subtitle percents for episodes
  73 + res := &Resp{}
  74 + err := json.Unmarshal(eps, &res)
  75 + report(err)
  76 +
  77 + // reverse index
  78 + numEps := len(res.Response)
  79 + i := numEps - ep
  80 +
  81 + // retrieves percentage
  82 + cent := res.Response[i].Subtitle_completions.En
  83 + return cent
  84 +}
  85 +
  86 +func main() {
  87 + // sets up variables
  88 + name := "bong+soon"
  89 + id := getId(name)
  90 + cent := getCent(id, 5)
  91 + fmt.Println(name, "subbed at", cent)
  92 +}