twitch.go - twitch-go - twitch.tv web application in Go
 (HTM) git clone git://git.codemadness.org/twitch-go
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       twitch.go (5744B)
       ---
            1 package twitch
            2 
            3 import (
            4         "encoding/json"
            5         "errors"
            6         "fmt"
            7         "io/ioutil"
            8         "net/http"
            9         "net/url"
           10         "time"
           11 )
           12 
           13 // set your Twitch Client-ID.
           14 var Clientid string
           15 
           16 type Token struct {
           17         Mobile_restricted bool
           18         Sig               string
           19         Token             string
           20 }
           21 
           22 type Featured struct {
           23         Featured []struct {
           24                 Image     string
           25                 Priority  int
           26                 Scheduled bool
           27                 Sponsored bool
           28                 Stream    struct {
           29                         Id          int
           30                         Average_fps float64
           31                         Created_at  string
           32                         Channel     struct {
           33                                 Broadcaster_language string
           34                                 Delay                int
           35                                 Followers            int
           36                                 Display_name         string
           37                                 Language             string
           38                                 Name                 string
           39                                 Logo                 string
           40                                 Mature               bool
           41                                 Partner              bool
           42                                 Status               string
           43                                 Updated_at           string
           44                                 Url                  string
           45                                 Views                int
           46                         }
           47                         Game         string
           48                         Video_height int
           49                         Viewers      int
           50                 }
           51                 Text  string
           52                 Title string
           53         }
           54 }
           55 
           56 type Game struct {
           57         Streams []struct {
           58                 Id          int
           59                 Average_fps float64
           60                 Channel     struct {
           61                         Broadcaster_language string
           62                         Delay                int
           63                         Followers            int
           64                         Display_name         string
           65                         Language             string
           66                         Name                 string
           67                         Logo                 string
           68                         Mature               bool
           69                         Partner              bool
           70                         Status               string
           71                         Updated_at           string
           72                         Url                  string
           73                         Views                int
           74                 }
           75                 Created_at   string
           76                 Game         string
           77                 Video_height int
           78                 Viewers      int
           79         }
           80 }
           81 
           82 type Games struct {
           83         Top []struct {
           84                 Channels int
           85                 Game     struct {
           86                         Box struct {
           87                                 Large    string
           88                                 Medium   string
           89                                 Small    string
           90                                 Template string
           91                         }
           92                         Giantbomb_id int
           93                         Logo         struct {
           94                                 Large    string
           95                                 Medium   string
           96                                 Small    string
           97                                 Template string
           98                         }
           99                         Name string
          100                 }
          101                 Viewers int
          102         }
          103 }
          104 
          105 type User struct {
          106         Broadcaster_type  string `json:"broadcaster_type"`
          107         Description       string `json:"description"`
          108         Display_name      string `json:"display_name"`
          109         Email             string `json:"email"`
          110         Id                string `json:"id"`
          111         Login             string `json:"login"`
          112         Offline_image_url string `json:"offline_image_url"`
          113         Profile_image_url string `json:"profile_image_url"`
          114         Type              string `json:"type"`
          115         View_count        int64  `json:"view_count"`
          116 }
          117 
          118 type Users struct {
          119         Items []User `json:"data"`
          120 }
          121 
          122 type Video struct {
          123         Created_at    string `json:"created_at"`
          124         Description   string `json:"description"`
          125         Duration      string `json:"duration"`
          126         Id            string `json:"id"`
          127         Language      string `json:"language"`
          128         Published_at  string `json:"published_at"`
          129         Thumbnail_url string `json:"thumbnail_url"`
          130         Title         string `json:"title"`
          131         Type          string `json:"type"`
          132         Url           string `json:"url"`
          133         User_id       string `json:"user_id"`
          134         User_name     string `json:"user_name"`
          135         View_count    int64  `json:"view_count"`
          136         Viewable      string `json:"viewable"`
          137 }
          138 
          139 type Videos struct {
          140         Items []Video `json:"data"`
          141 }
          142 
          143 func ReadAllUrl(url string) ([]byte, error) {
          144         client := http.Client{
          145                 Timeout: time.Duration(30) * time.Second,
          146         }
          147         req, err := http.NewRequest("GET", url, nil)
          148         if err != nil {
          149                 return nil, err
          150         }
          151 
          152         req.Header.Set("Client-ID", Clientid)
          153         req.Header.Set("User-Agent", "")
          154 
          155         resp, err := client.Do(req)
          156         if resp != nil {
          157                 defer resp.Body.Close()
          158         }
          159         if err != nil {
          160                 return nil, err
          161         }
          162         body, err := ioutil.ReadAll(resp.Body)
          163         if err != nil {
          164                 return nil, err
          165         }
          166         if resp.StatusCode != 200 {
          167                 return nil, errors.New(resp.Status)
          168         }
          169         return body, nil
          170 }
          171 
          172 func GetToken(channel string) (*Token, error) {
          173         url := fmt.Sprintf("https://api.twitch.tv/api/channels/%s/access_token", channel)
          174         body, err := ReadAllUrl(url)
          175         if err != nil {
          176                 return nil, err
          177         }
          178         var v Token
          179         err = json.Unmarshal(body, &v)
          180         if err != nil {
          181                 return nil, err
          182         }
          183         return &v, nil
          184 }
          185 
          186 func GetFeatured() (*Featured, error) {
          187         url := "https://api.twitch.tv/kraken/streams/featured?limit=100"
          188         body, err := ReadAllUrl(url)
          189         if err != nil {
          190                 return nil, err
          191         }
          192         var v Featured
          193         err = json.Unmarshal(body, &v)
          194         if err != nil {
          195                 return nil, err
          196         }
          197         return &v, nil
          198 }
          199 
          200 func GetGames() (*Games, error) {
          201         url := "https://api.twitch.tv/kraken/games/top?limit=100"
          202         body, err := ReadAllUrl(url)
          203         if err != nil {
          204                 return nil, err
          205         }
          206 
          207         var v Games
          208         err = json.Unmarshal(body, &v)
          209         if err != nil {
          210                 return nil, err
          211         }
          212         return &v, nil
          213 }
          214 
          215 func GetGame(game string) (*Game, error) {
          216         s := fmt.Sprintf("https://api.twitch.tv/kraken/streams?game=%s", url.QueryEscape(game))
          217         body, err := ReadAllUrl(s)
          218         if err != nil {
          219                 return nil, err
          220         }
          221         var v Game
          222         err = json.Unmarshal(body, &v)
          223         if err != nil {
          224                 return nil, err
          225         }
          226         return &v, nil
          227 }
          228 
          229 func GetVideosByGame(gameid string) (*Videos, error) {
          230         s := fmt.Sprintf("https://api.twitch.tv/helix/videos?first=100&game_id=%s", url.QueryEscape(gameid))
          231         body, err := ReadAllUrl(s)
          232         if err != nil {
          233                 return nil, err
          234         }
          235         var v Videos
          236         err = json.Unmarshal(body, &v)
          237         if err != nil {
          238                 return nil, err
          239         }
          240         return &v, nil
          241 }
          242 
          243 func GetVideosByUser(userid string) (*Videos, error) {
          244         s := fmt.Sprintf("https://api.twitch.tv/helix/videos?first=100&user_id=%s",
          245                 url.QueryEscape(userid))
          246         body, err := ReadAllUrl(s)
          247         if err != nil {
          248                 return nil, err
          249         }
          250         var v Videos
          251         err = json.Unmarshal(body, &v)
          252         if err != nil {
          253                 return nil, err
          254         }
          255         return &v, nil
          256 }
          257 
          258 func GetUseridByName(name string) (*Users, error) {
          259         s := fmt.Sprintf("https://api.twitch.tv/helix/users?login=%s",
          260                 url.QueryEscape(name))
          261         body, err := ReadAllUrl(s)
          262         if err != nil {
          263                 return nil, err
          264         }
          265         var u Users
          266         err = json.Unmarshal(body, &u)
          267         if err != nil {
          268                 return nil, err
          269         }
          270         return &u, nil
          271 }