Last active
July 11, 2017 18:42
-
-
Save robalni/90723f4af82e2fed9c79884a5ff7e910 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # Initialize some variables | |
| function twitch-setup | |
| { | |
| twitch_dir=~/.twitch | |
| if ! [ -e $twitch_dir ]; then | |
| echo "The Twitch directory $twitch_dir doesn't exist." | |
| echo "Please create it and add these files: " | |
| echo " username: your username on Twitch" | |
| echo " .<username>.oauth: your oauth token" | |
| return | |
| fi | |
| twitch_name=$(< $twitch_dir/username ) | |
| twitch_oauth=$(< $twitch_dir/.$twitch_name.oauth ) | |
| twitch_clientid='mpw1gvfyzd1mtnm7rzoln5icmkdecys' | |
| } | |
| twitch-setup | |
| # Show or change your Twitch username | |
| function twitchname | |
| { | |
| if [ $# -eq 0 ]; then | |
| echo "$twitch_name" | |
| elif [ $# -eq 1 ]; then | |
| echo "$1" > $twitch_dir/username | |
| twitch-setup | |
| else | |
| echo "Usage: twitchname [name]" | |
| fi | |
| } | |
| # Search for streams | |
| function twitchs | |
| { | |
| if [[ $# -eq 0 ]]; then | |
| echo "Usage: twitchs search_string [page_number]" | |
| return | |
| fi | |
| page=1 | |
| limit=7 | |
| if [[ $# -eq 2 ]]; then | |
| page=$2 | |
| fi | |
| page=$(echo "($page - 1) * $limit" | bc) | |
| q_url=$(echo -n $1 | perl -MURI::Escape -le 'print(uri_escape(<STDIN>))') | |
| s_res=$(kraken "search/streams?q=$q_url&offset=$page&limit=$limit") | |
| lines=$(echo $s_res \ | |
| | jq -r ".streams[] | .channel.name,.game,.channel.status") | |
| i=0 | |
| IFS=$'\n' | |
| for line in $lines; do | |
| if [[ "$i" -eq 0 ]]; then | |
| echo "$line" | |
| else | |
| echo " $line" | |
| fi | |
| if [[ "$i" -eq 2 ]]; then | |
| i=0 | |
| else | |
| let i++ | |
| fi | |
| done | |
| } | |
| # Show or update your Twitch status and game | |
| # Update like this: twitchu "<game>" "<status>" | |
| function twitchu | |
| { | |
| status='' | |
| game='' | |
| if [ $# -eq 0 ]; then | |
| code=$(kraken "channels/$twitch_name") | |
| display_name=$(echo $code | jq -e -r .display_name) | |
| game=$(echo $code | jq -e -r .game) | |
| status=$(echo $code | jq -e -r .status) | |
| echo "Name: $display_name" | |
| echo "Game: $game" | |
| echo "Status: $status" | |
| return | |
| fi | |
| if [ $# -ge 1 ]; then | |
| game="channel[game]=$1" | |
| fi | |
| if [ $# -ge 2 ]; then | |
| status="channel[status]=$2" | |
| fi | |
| curl -s --data-urlencode "$status" \ | |
| --data-urlencode "$game" \ | |
| -X PUT https://api.twitch.tv/kraken/channels/$twitch_name \ | |
| -H "Authorization: OAuth $twitch_oauth" \ | |
| -H "Client-ID: $twitch_clientid" \ | |
| | jq -r ".display_name,.game,.status" | |
| } | |
| # Watch a stream | |
| function twitchv | |
| { | |
| if [ "$2" == "" ] | |
| then | |
| q='source' | |
| else | |
| q=$2 | |
| fi | |
| #livestreamer --player "mpv --no-keepaspect-window --title 'Stream: $1'" twitch.tv/$1 $q & | |
| #ffplay -window_title "Stream: $1" "$(youtube-dl -g https://twitch.tv/$1 $q)" & | |
| qa='' | |
| if [ -n "$q" ]; then | |
| qa="--ytdl-format=$q" | |
| fi | |
| mpv --no-keepaspect-window --title "Stream: $1" https://twitch.tv/$1 $qa & | |
| } | |
| # Open chat for a stream | |
| function twitchc | |
| { | |
| channels='' | |
| if [ "$1" != "" ] | |
| then | |
| channels=":$1" | |
| fi | |
| xterm -T "Chat: $1" -e "irssi -c 'Twitch$channels'" & | |
| wait %xterm | |
| if [[ $? -eq 1 ]] | |
| then | |
| irssi -c "Twitch$channels" | |
| fi | |
| } | |
| # Check the channels in $twitch_dir/following | |
| function twitch-following | |
| { | |
| ( | |
| channels=$(sort -R $twitch_dir/following) | |
| for ch in $channels | |
| do | |
| code=$(kraken "streams/$ch") | |
| status=$(echo $code | jq -e -r .stream.channel.status) | |
| name=$(echo $code | jq -e -r .stream.channel.display_name) | |
| if [[ $? -eq 0 ]] | |
| then | |
| echo "$name : $status" | |
| fi | |
| done | |
| ) | |
| } | |
| # Check channels or watch a stream (with chat) | |
| function twitch | |
| { | |
| if [ $# -eq 0 ] | |
| then | |
| twitch-following | |
| return | |
| elif [ $# -eq 1 ] | |
| then | |
| q='source' | |
| else | |
| q=$2 | |
| fi | |
| twitchv $1 $q | |
| twitchc $1 | |
| } | |
| # Convert seconds to h:m:s | |
| function sec2time | |
| { | |
| if [[ $# -ne 1 ]]; then | |
| echo "sec2time: I need 1 argument: seconds" 1>&2 | |
| return 1 | |
| fi | |
| d=$1 | |
| h=$((d/3600)) | |
| d=$((d - d/3600*3600)) | |
| m=$(tail -c 3 <<< 0$((d/60))) | |
| d=$((d - d/60*60)) | |
| s=$(tail -c 3 <<< 0$d) | |
| echo "$h:$m:$s" | |
| } | |
| # Calculate the number of seconds between two dates | |
| function date_diff | |
| { | |
| if [[ $# -ne 2 ]]; then | |
| echo "date_diff: I need 2 arguments: date1, date2" 1>&2 | |
| return 1 | |
| fi | |
| sec1=$(date -d $1 +%s) | |
| sec2=$(date -d $2 +%s) | |
| echo $((sec2-sec1)) | |
| } | |
| # Show how long time someone has been streaming | |
| function streamed-time | |
| { | |
| if [[ $# -ne 1 ]]; then | |
| echo "streamed-time: I need 1 argument: channel" 1>&2 | |
| return 1 | |
| fi | |
| stream_info=$(kraken "streams/$1") | |
| stream_started=$(echo $stream_info | jq -e -r .stream.created_at) | |
| if [[ $? -ne 0 ]]; then | |
| echo "No stream found." | |
| return 1 | |
| fi | |
| stream_name=$(echo $stream_info | jq -r .stream.channel.display_name) | |
| while true; do | |
| diff_s=$(date_diff $stream_started @$(date +%s)) | |
| time_stamp=$(sec2time $diff_s) | |
| clear | |
| echo "$stream_name has been streaming for $time_stamp" | |
| sleep 1 | |
| done | |
| } | |
| # Manually browse the Twitch API | |
| function kraken | |
| { | |
| str=$1 | |
| if [ -t 1 ]; then | |
| curl -s "https://api.twitch.tv/kraken/$str" \ | |
| -H "Client-ID: $twitch_clientid" \ | |
| -H "Authorization: OAuth $twitch_oauth" \ | |
| | jq -C . | less -R | |
| else | |
| curl -s "https://api.twitch.tv/kraken/$str" \ | |
| -H "Client-ID: $twitch_clientid" \ | |
| -H "Authorization: OAuth $twitch_oauth" | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment