useful_scripts/twitch/twitch-getvideo.py

32 lines
1.5 KiB
Python
Raw Normal View History

2023-03-24 14:40:17 -03:00
from requests import *
import os
def titles(jsonwithids):
"""gets the value of the key 'title' of the first 5 items in the json dict, adds them to a list and returns that list"""
titles=[]
durations=[]
for x in range(5):
titles.append(jsonwithids[x].get('title'))
durations.append(jsonwithids[x].get('duration'))
return titles, durations
def choice(titles, durations):
"""prints the titles and prompts the user to choose one, returns the choice as a number from 0 to 4"""
for x in range(5):
print(titles[x], durations[x])
choice=int(input('Type a number from 1 to 5 to choose video >'))-1
return choice
def main():
"""runs the program"""
params1={'client_id':'ib8heiflx9p0u12j9p5wybh6wjnhn2', 'client_secret':'u3ieq39z3e7lo1ewb69l9oueexxe2l', 'grant_type':'client_credentials'}
access_token=post('https://id.twitch.tv/oauth2/token', params=params1).json().get('access_token')
heads={'Authorization':'Bearer %s'% access_token,'client-id':params1.get('client_id')}
user_id=get('https://api.twitch.tv/helix/users', params={'login':input('Type in the channel name >')}, headers=heads).json().get('data')[0].get('id')
lastvideos=get('https://api.twitch.tv/helix/videos', params={'user_id':user_id}, headers=heads).json().get('data')
a,b=titles(lastvideos)
os.system('streamlink '+lastvideos[choice(a,b)].get('url')+'?t='+input('Type time in xhxmxs format (optional)>')+' 720p60,720p')
if __name__ == '__main__':
main()