You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
3.8 KiB
111 lines
3.8 KiB
from requests_futures.sessions import FuturesSession
|
|
|
|
|
|
class Twitch(object):
|
|
def __init__(self, client_id, oauth_token):
|
|
self.client_id = client_id
|
|
self.oauth_token = oauth_token
|
|
|
|
def fetch_videos(self, channel_id):
|
|
if not channel_id:
|
|
return []
|
|
session = FuturesSession()
|
|
def get_videos(offset, limit):
|
|
url = 'https://api.twitch.tv/v5/channels/{0}/videos'.format(channel_id)
|
|
params = dict(
|
|
client_id=self.client_id,
|
|
offset=offset,
|
|
limit=limit)
|
|
return session.get(url, params=params)
|
|
request = get_videos(0, 1)
|
|
total = request.result().json().get('_total')
|
|
if not total:
|
|
return []
|
|
requests = []
|
|
limit = 100
|
|
for offset in range(0, total, limit):
|
|
requests.append(get_videos(offset, limit))
|
|
result = []
|
|
for request in requests:
|
|
result.extend(request.result().json().get('videos', []))
|
|
return result
|
|
|
|
def fetch_comments(self, videos):
|
|
if not videos:
|
|
return []
|
|
session = FuturesSession(max_workers=len(videos))
|
|
def get_comments(video_id, cursor):
|
|
url = 'https://api.twitch.tv/v5/videos/{0}/comments'.format(video_id)
|
|
params = dict(
|
|
client_id=self.client_id,
|
|
cursor=cursor)
|
|
return session.get(url, params=params)
|
|
pairs = [(video_id, '') for video_id in videos]
|
|
result = []
|
|
while pairs:
|
|
requests = []
|
|
for video_id, cursor in pairs:
|
|
requests.append(get_comments(video_id, cursor))
|
|
videos, _ = zip(*pairs)
|
|
pairs = []
|
|
for video_id, request in zip(videos, requests):
|
|
data = request.result().json()
|
|
result.extend(data.get('comments', []))
|
|
cursor = data.get('_next')
|
|
if cursor:
|
|
pairs.append((video_id, cursor))
|
|
return result
|
|
|
|
def fetch_emotes(self, channel_id):
|
|
if not channel_id:
|
|
return []
|
|
session = FuturesSession()
|
|
def get_emotes():
|
|
url = 'https://api.twitch.tv/v5/users/{0}/emotes'.format(channel_id)
|
|
params = dict(client_id=self.client_id)
|
|
headers = dict(authorization='OAuth {0}'.format(self.oauth_token))
|
|
return session.get(url, params=params, headers=headers)
|
|
request = get_emotes()
|
|
data = request.result().json()
|
|
result = []
|
|
for val in data.get('emoticon_sets', {}).values():
|
|
result.extend(val)
|
|
return result
|
|
|
|
def fetch_clips(self, channel_name):
|
|
if not channel_name:
|
|
return []
|
|
session = FuturesSession()
|
|
def get_clips(cursor):
|
|
url = 'https://api.twitch.tv/v5/clips/top'
|
|
params = dict(
|
|
client_id=self.client_id,
|
|
channel=channel_name,
|
|
period='all',
|
|
limit=100,
|
|
cursor=cursor)
|
|
return session.get(url, params=params)
|
|
cursor = ''
|
|
result = []
|
|
while True:
|
|
request = get_clips(cursor)
|
|
data = request.result().json()
|
|
result.extend(data.get('clips', []))
|
|
cursor = data.get('_cursor')
|
|
if not cursor:
|
|
break
|
|
return result
|
|
|
|
def fetch_events(self, channel_id):
|
|
if not channel_id:
|
|
return []
|
|
session = FuturesSession()
|
|
def get_events():
|
|
url = 'https://api.twitch.tv/v5/channels/{0}/events'.format(channel_id)
|
|
params = dict(client_id=self.client_id)
|
|
return session.get(url, params=params)
|
|
request = get_events()
|
|
data = request.result().json()
|
|
result = data.get('events', [])
|
|
return result
|