Skip to content

Instantly share code, notes, and snippets.

@maddingue
Last active May 1, 2016 16:54
Show Gist options
  • Save maddingue/f170ab46eb85ce4f6a16e188fc98e72a to your computer and use it in GitHub Desktop.
Save maddingue/f170ab46eb85ce4f6a16e188fc98e72a to your computer and use it in GitHub Desktop.
twitch-stream
#!/usr/bin/perl
use v5.10;
use strict;
use warnings;
use Data::Dump qw< dd pp >;
use JSON::XS;
use LWP::Simple;
use URI::Escape;
my $channel = lc(shift // "nolife");
my $quality = lc(shift // "chunked");
my $json = decode_json
get "http://api.twitch.tv/api/channels/$channel/access_token";
my $token = $json->{token};
my $sig = $json->{sig};
my $random = int rand 1_000_000;
my $m3u8 = get "http://usher.twitch.tv/api/channel/hls/$channel.m3u8?"
. "token=" . uri_escape($token) . "&sig=$sig"
. "&&allow_source=true&allow_spectre=false&p=$random";
my @qualities = $m3u8 =~ /VIDEO="([a-z]+?)"/g;
my @hls_urls = grep { !/^#/ } split /\n/, $m3u8;
my %streams; @streams{@qualities} = @hls_urls;
say $streams{$quality};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment