iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 15
0
自我挑戰組

用 laravel 尻出自己形狀的 line bot,還要撐三十天!系列 第 15

【第十五天】突發任務:Twitch API

【第十五天】突發任務:Twitch API

在我們和自己的機器人對話之前,先把昨天提到的twitch實作吧!先熱熱手!
恩~看來不像想像中的順利,再仔細看的時候發現了這個!
https://ithelp.ithome.com.tw/upload/images/20171220/20107380HN3MiPQiEm.png

所以我們又找到了他的API
沒錯!又是一個新的玩具!那我們就用他提供的API來完成我們的需求吧!

看來進度又多Delay一天了

那就先來註冊拿Client吧! 點我
https://ithelp.ithome.com.tw/upload/images/20171220/20107380D8ovFP76rM.png
https://ithelp.ithome.com.tw/upload/images/20171220/20107380eQlcIaMsbF.png

接著!直接看這份文件
千萬別在新版API上逗留!他是時間小偷啊!
簡單說,以新版API我們無法從channelName就GET到我們想要的資訊!

新版本的串接方式請參考第二十四天

雖然他說Twitch API v5只支援到2018年年尾!但反過來說我們有整整一年都可以用這隻API用妥妥的!

所以API要怎麼打呢?

curl -H 'Client-ID: <你剛剛拿到的ClientID>' -X GET 'https://api.twitch.tv/kraken/streams/?channel=<channelName>'
  • channelName指的是這個
    • https://ithelp.ithome.com.tw/upload/images/20171220/201073804TnQGWI74e.png

以實況主nightblue3為例子,打出來的API會是長這樣!

{
  "_total": 1,
  "streams": [
    {
      "_id": 27030349504,
      "game": "League of Legends",
      "viewers": 13304,
      "video_height": 1080,
      "average_fps": 60,
      "delay": 0,
      "created_at": "2017-12-20T10:51:22Z",
      "is_playlist": false,
      "stream_type": "live",
      "preview": {
        "small": "https://static-cdn.jtvnw.net/previews-ttv/live_user_nightblue3-80x45.jpg",
        "medium": "https://static-cdn.jtvnw.net/previews-ttv/live_user_nightblue3-320x180.jpg",
        "large": "https://static-cdn.jtvnw.net/previews-ttv/live_user_nightblue3-640x360.jpg",
        "template": "https://static-cdn.jtvnw.net/previews-ttv/live_user_nightblue3-{width}x{height}.jpg"
      },
      "channel": {
        "mature": false,
        "partner": true,
        "status": "Unranked to Challenger on EUW | THE 5 TIME CHALLENGER | Unleash Your Inner Jungler Spirit o(≧▽≦)o @Nightbloo ",
        "broadcaster_language": "en",
        "display_name": "Nightblue3",
        "game": "League of Legends",
        "language": "en",
        "_id": 26946000,
        "name": "nightblue3",
        "created_at": "2011-12-21T18:18:40Z",
        "updated_at": "2017-12-20T14:35:32Z",
        "delay": null,
        "logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/a7eb6cdf-ed7e-43ab-a2ff-0036fd1d3d87-profile_image-300x300.png",
        "banner": null,
        "video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/7f5a699dc6b7ebdc-channel_offline_image-1920x1080.png",
        "background": null,
        "profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/d7c46e55218bd672-profile_banner-480.png",
        "profile_banner_background_color": "",
        "url": "https://www.twitch.tv/nightblue3",
        "views": 182460645,
        "followers": 2103031,
        "_links": {
          "self": "https://api.twitch.tv/kraken/channels/nightblue3",
          "follows": "https://api.twitch.tv/kraken/channels/nightblue3/follows",
          "commercial": "https://api.twitch.tv/kraken/channels/nightblue3/commercial",
          "stream_key": "https://api.twitch.tv/kraken/channels/nightblue3/stream_key",
          "chat": "https://api.twitch.tv/kraken/chat/nightblue3",
          "features": "https://api.twitch.tv/kraken/channels/nightblue3/features",
          "subscriptions": "https://api.twitch.tv/kraken/channels/nightblue3/subscriptions",
          "editors": "https://api.twitch.tv/kraken/channels/nightblue3/editors",
          "teams": "https://api.twitch.tv/kraken/channels/nightblue3/teams",
          "videos": "https://api.twitch.tv/kraken/channels/nightblue3/videos"
        }
      },
      "_links": {
        "self": "https://api.twitch.tv/kraken/streams/nightblue3"
      }
    }
  ],
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams?channel=nightblue3&limit=25&stream_type=live",
    "next": "https://api.twitch.tv/kraken/streams?channel=nightblue3&limit=25&offset=25&stream_type=live",
    "featured": "https://api.twitch.tv/kraken/streams/featured",
    "summary": "https://api.twitch.tv/kraken/streams/summary",
    "followed": "https://api.twitch.tv/kraken/streams/followed"
  }
}

那我們再用一個目前沒開實況的小熊為例子

{
  "_total": 0,
  "streams": [],
  "_links": {
    "self": "https://api.twitch.tv/kraken/streams?channel=yuniko0720&limit=25&stream_type=live",
    "next": "https://api.twitch.tv/kraken/streams?channel=yuniko0720&limit=25&offset=25&stream_type=live",
    "featured": "https://api.twitch.tv/kraken/streams/featured",
    "summary": "https://api.twitch.tv/kraken/streams/summary",
    "followed": "https://api.twitch.tv/kraken/streams/followed"
  }
}

這樣就可以分辨出是否有開實況的差別了!
那我們來試著整理出一份它的Service吧!

PR在此可供參考

今天成果大概是這樣
https://ithelp.ithome.com.tw/upload/images/20171221/20107380rOCP1RC0IL.png

今天時間都被小偷偷走了可惡!
等Twitch串接完後我們來試著用些方法來把我們寫的一些漫畫追蹤清單和實況追蹤清單(hard code)給處理掉吧!
處理的時候我們要先找找一些東西,有興趣的可以先參考:


上一篇
【第十四天】今天簡單做
下一篇
【第十六天】Slash Commands之前,做一些基本設施吧!
系列文
用 laravel 尻出自己形狀的 line bot,還要撐三十天!30

尚未有邦友留言

立即登入留言