useful_scripts/twitch/gettwurl.sh

30 lines
2.0 KiB
Bash
Executable File

#!/bin/bash
# twitch api credentials
client_id="ib8heiflx9p0u12j9p5wybh6wjnhn2"
client_secret="u3ieq39z3e7lo1ewb69l9oueexxe2l"
grant_type="client_credentials"
# get access token
access_token=$(curl -s -X POST https://id.twitch.tv/oauth2/token -d "client_id=$client_id" -d "client_secret=$client_secret" -d "grant_type=$grant_type" | awk -F '[\"\"|:|,]' '{print $5}')
read -p 'type in username >' login
# get user id
user_id=$(curl -s -X GET "https://api.twitch.tv/helix/users?login=$login" -H "Authorization: Bearer $access_token" -H "client-id: $client_id" | awk -F '[\"\"|:|,]' '{print $8}')
# save json with first page of results for videos tied to the user id we just got
curl -s -X GET "https://api.twitch.tv/helix/videos?user_id=$user_id" -H "Authorization: Bearer $access_token" -H "client-id: $client_id" > /tmp/streamarchives.txt
# loop through the json
range=$(grep -Po "url\":\"[a-zA-Z0-9 \\+|\!#\$@.\-\[\]{}=,*/<>_:'()]+\"" /tmp/streamarchives.txt | awk 'BEGIN {FS="\""} END{print NR}')
for x in $(eval echo "{1..$range}"); do
titles=$(echo -e $(cat /tmp/streamarchives.txt) | grep -Po "title\":\"[a-zA-Z0-9 %\\+|\!#\$@.\-\[\]{}=,*/<>_:'()]+\"" | awk -v x=$x 'BEGIN{FS="\""} FNR==x {print $3}')
url=$(grep -Po "url\":\"[a-zA-Z0-9 \\+|\!#\$@.\-\[\]{}=,*/<>_:'()]+\"" /tmp/streamarchives.txt | awk -v x=$x 'BEGIN{FS="\""} FNR==x {print $3}')
duration=$(grep -Po "duration\":\"[a-zA-Z0-9 %\\+|\!#\$@.\-\[\]{}=,*/<>_:'()]+\"" /tmp/streamarchives.txt | awk -v x=$x 'BEGIN{FS="\""} FNR==x {print $3}')
#echo "$url $duration $titles"
echo "$x $titles $duration" >> /tmp/titles.txt
done
#read -p "choose a vid for clipboard or leave empty to quit >" choice
choice=$(cat /tmp/titles.txt | dmenu -c -l 10 -bw 3 | awk '{print $1}') && rm /tmp/titles.txt
[ -z $choice ] && exit || grep -Po "url\":\"[a-zA-Z0-9 \\+|\!#\$@.\-\[\]{}=,*/<>_:'()]+\"" /tmp/streamarchives.txt | awk -v choice=$choice 'BEGIN {FS="\""} FNR==choice {print $3}' | xclip -r -selection clipboard && rm /tmp/streamarchives.txt