Skip to content

Instantly share code, notes, and snippets.

@thiemok
Last active November 30, 2016 14:42
Show Gist options
  • Save thiemok/e1b452fc01bcae68f050fb48f44df93f to your computer and use it in GitHub Desktop.
Save thiemok/e1b452fc01bcae68f050fb48f44df93f to your computer and use it in GitHub Desktop.
Twitch live channels Widget for Dashing

##Preview

##Description

Simple Smashing widget to display followed and currently live twitch streams.

##Dependencies and Installation

twitch

Add it to smashing's gemfile:

gem 'twitch', '~> 0.1.0' 

and run bundle install.

Create a twitch api app

Run smashing install e1b452fc01bcae68f050fb48f44df93f, or copy twitch.haml, twitch.coffee, and twitch.sass into the /widgets/twitch directory and put the twitch.rb file in your /jobs folder.

Go to the base folder of your smashing project and run:

wget https://gist.githubusercontent.com/thiemok/e1b452fc01bcae68f050fb48f44df93f/raw/5c6d540da7bbc7d7999f0c5c841b59ad063c8094/authorize_twitch.rb
ruby authorize_twitch.rb

Follow the prompts and the widget should be ready to use.

If you want the twitch logo as a background image, grab this package from the twitch brand assets. And put the contained png file in the /publicfolder of your project.

##Usage

To include the widget in a dashboard, add the following snippet to the dashboard layout file:

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="twitch" data-view="Twitch"></div>
</li>
require 'twitch'
puts 'Please enter your client id'
@client_id = gets.chomp
puts 'Please enter your secret key'
@secret_key = gets.chomp
puts 'Please enter your redirect uri'
@redirect_uri = gets.chomp
@scope = ["user_read", "channel_read"]
@twitch = Twitch.new({
client_id: @client_id,
secret_key: @secret_key,
redirect_uri: @redirect_uri,
scope: @scope
})
puts 'Open this link in your browser to authorize your app'
puts @twitch.link
puts 'Copy the resulting code here, you will find it in the parameters of the new url your browser was redirected to'
@code = gets.chomp
@data = @twitch.auth(@code)
puts 'This is your auth token. Put it as SMASHING_TWITCH_AUTH in your environment variables'
puts @data[:body]["access_token"]

The MIT License (MIT)

Copyright (c) 2016 Thiemo Krause

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class Dashing.Twitch extends Dashing.Widget
%img(class="icon-background" src="Glitch_White_RGB.png" alt="Glitch White")
%ul
%li(data-foreach-item="items")
%span(class="name" data-bind="item.name")
%span(class="game" data-bind="item.game")
%span(class="viewers" data-bind="item.viewers")
%p(class="updated-at" data-bind="updatedAtMessage")
require 'twitch'
token = ENV['SMASHING_TWITCH_AUTH']
client = Twitch.new(access_token: token)
SCHEDULER.every "5m", first_in: 0 do |job|
followed_streams = client.followed_streams(stream_type: 'live')[:body]['streams']
streams = []
followed_streams.each do |stream|
streams << {name: stream['channel']['display_name'], game: stream['channel']['game'], viewers: stream['viewers']}
end
send_event('twitch', {items: streams})
end
$background-color: #2983E0
$update-at-color: #fff
.widget-twitch
background-color: $background-color
h1
height: 141px
li
text-align: left
font-size: 12px
padding: 2px
height: 34px
span
display: inline-block
vertical-align: middle
text-overflow: ellipsis
overflow: hidden
white-space: nowrap
font-weight: bold
&.name
position: absolute
left: 28px
right: 75px
padding: 2px 0 0 0
min-width: 195px
&.game
position: absolute
left: 28px
right: 0px
padding: 19px 0 0 0
min-width: 120px
&.viewers
position: absolute
right: 15px
width: 55px
padding: 2px 0 0 0
text-align: right
p
&.updated-at
text-transform: uppercase
letter-spacing: 1px
font-size: 10px
color: $update-at-color
.icon-background
pointer-events: none
width: 80% !important
height: 67%
position: absolute
left: 12.5%
top: 12.5%
opacity: 0.1
font-size: 275px
text-align: center
margin-top: 10px
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment