File: twitch.lua

package info (click to toggle)
vlc 3.0.22-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208,324 kB
  • sloc: ansic: 443,399; cpp: 111,127; objc: 36,399; sh: 6,737; makefile: 6,623; javascript: 4,902; xml: 1,611; asm: 1,355; yacc: 644; python: 321; lex: 88; perl: 77; sed: 16
file content (204 lines) | stat: -rw-r--r-- 6,079 bytes parent folder | download | duplicates (9)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
--[[
Resolve Twitch channel and video URLs to the actual stream URL

 Copyright © 2017 the VideoLAN team

 Author: Marvin Scholz <epirat07 at gmail dot com>

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
--]]

-- Probe function
function probe()
    return (vlc.access == "http" or vlc.access == "https")
        and ( vlc.path:match("^www%.twitch%.tv/videos/.+") or
              vlc.path:match("^www%.twitch%.tv/.+") or
              vlc.path:match("^go%.twitch%.tv/.+") or 
              vlc.path:match("^go%.twitch%.tv/videos/.+") )
end

-- Download and parse a JSON document from the specified URL
-- Returns: obj, pos, err (see dkjson docs)
function parse_json(url)
    local json   = require("dkjson")
    local stream = vlc.stream(url)
    local string = ""
    local line   = ""

    if not stream then
        return nil, nil, "Failed creating VLC stream"
    end

    while true do
        line = stream:readline()

        if not line then
            break
        end

        string = string .. line
    end

    return json.decode(string)
end

-- Make a request to the Twitch API endpoint given by url
-- Returns: obj, err
function twitch_api_req(url)
    local obj, pos, err = parse_json(url .. "?client_id=kimne78kx3ncx6brgo4mv6wki5h1ko")

    if err then
        return nil, "Error getting JSON object: " .. err
    end

    -- In case of error, the API will return an object with
    -- error and message given
    if obj.error then
        local err = "Twitch API error: " .. obj.error
        if obj.message then
            err = err .. " (" .. obj.message .. ")"
        end
        return nil, err
    end

    return obj, nil
end

-- Parser for twitch video urls
function parse_video()
    local playlist = {}
    local item = {}
    local url, obj, err = nil

    -- Parse video id out of url
    local video_id = vlc.path:match("/videos/(%d+)")

    if video_id == nil then
        vlc.msg.err("Twitch: Failed to parse twitch url for video id")
        return playlist
    end

    vlc.msg.dbg("Twitch: Loading video url for " .. video_id)

    -- Request video token (required for the video stream)
    url = "https://api.twitch.tv/api/vods/" ..  video_id .. "/access_token"
    obj, err = twitch_api_req(url)

    if err then
        vlc.msg.err("Error getting request token from Twitch: " .. err)
        return playlist
    end

    -- Construct stream url
    local stream_url = "http://usher.twitch.tv/vod/" .. video_id
    stream_url = stream_url .. "?player=twitchweb"
    stream_url = stream_url .. "&nauth=" .. vlc.strings.encode_uri_component(obj.token)
    stream_url = stream_url .. "&nauthsig=" .. obj.sig
    stream_url = stream_url .. "&allow_audio_only=true&allow_source=true"

    item["path"]   = stream_url

    -- Set base information
    item["name"]   = "Twitch: " .. video_id

    -- Request video information
    url = "https://api.twitch.tv/kraken/videos/v" .. video_id
    obj, err = twitch_api_req(url)

    if err then
        vlc.msg.warn("Error getting video info from Twitch: " .. err)
        table.insert(playlist, item)
        return playlist
    end

    -- Overwrite with now obtained better info
    item["name"]        = "Twitch: " .. obj.title
    item["artist"]      = obj.channel.display_name
    item["description"] = obj.description
    item["url"]         = vlc.path

    table.insert(playlist, item)

    return playlist
end

-- Parser for twitch stream urls
function parse_stream()
    local playlist = {}
    local item = {}
    local url, obj, err = nil

    -- Parse channel name out of url
    local channel = vlc.path:match("/([a-zA-Z0-9_]+)")

    if channel == nil then
        vlc.msg.err("Twitch: Failed to parse twitch url for channel name")
        return playlist
    end

    vlc.msg.dbg("Twitch: Loading stream url for " .. channel)

    -- Request channel token (required for the stream m3u8)
    url = "https://api.twitch.tv/api/channels/" .. channel .. "/access_token"
    obj, err = twitch_api_req(url)

    if err then
        vlc.msg.err("Error getting request token from Twitch: " .. err)
        return playlist
    end

    -- Construct stream url
    local stream_url = "http://usher.twitch.tv/api/channel/hls/" .. channel .. ".m3u8"
    stream_url = stream_url .. "?player=twitchweb"
    stream_url = stream_url .. "&token=" .. vlc.strings.encode_uri_component(obj.token)
    stream_url = stream_url .. "&sig=" .. obj.sig
    stream_url = stream_url .. "&allow_audio_only=true&allow_source=true&type=any"

    item["path"]   = stream_url

    -- Set base information
    item["name"]   = "Twitch: " .. channel
    item["artist"] = channel

    -- Request channel information
    url = "https://api.twitch.tv/api/channels/" .. channel
    obj, err = twitch_api_req(url)

    if err then
        vlc.msg.warn("Error getting channel info from Twitch: " .. err)
        table.insert(playlist, item)
        return playlist
    end

    -- Overwrite with now obtained better info
    item["name"]        = "Twitch: " .. obj.display_name
    item["nowplaying"]  = obj.display_name .. " playing " .. obj.game
    item["artist"]      = obj.display_name
    item["description"] = obj.status
    item["url"]         = vlc.path

    table.insert(playlist, item)

    return playlist
end

-- Parse function
function parse()
    if vlc.path:match("/videos/.+") then
        return parse_video()
    else
        return parse_stream()
    end
end