Skip to content

Commit

Permalink
record: Use GQL to find VOD
Browse files Browse the repository at this point in the history
Helix API now blocks requests with Twitch's Client-ID header.
  • Loading branch information
TheDrHax committed Nov 5, 2021
1 parent 2722764 commit 2c091e5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 44 deletions.
35 changes: 2 additions & 33 deletions twitch_utils/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,38 +273,6 @@ def record(vod_id: str, stream: Stream, vod: Stream, parts: int = 0) -> int:
return parts


def find_vod(oauth: str, channel: str) -> str:
api = TwitchAPI(oauth)

print(f'Checking if channel `{channel}` is active...')

status = api.helix('streams', user_login=channel)['data']

if not status:
print('ERR: Channel is offline')
sys.exit(1)
else:
status = status[0]

print('Attempting to find ID of the live VOD...')

vods = api.helix('videos', user_id=status['user_id'],
first=1, type='archive')['data']

if len(vods) == 0:
print('ERR: No VODs found on channel')
sys.exit(1)

stream_date = dateparser.isoparse(status['started_at'])
vod_date = dateparser.isoparse(vods[0]['created_at'])

if vod_date < stream_date:
print('ERR: Live VOD is not available yet')
sys.exit(1)

return vods[0]["id"]


def main(argv=None):
args = docopt(__doc__, argv=argv)

Expand All @@ -320,7 +288,8 @@ def main(argv=None):
channel = args['<channel>']

if args['--oauth'] and not args['<vod>']:
v = find_vod(args['--oauth'], channel)
api = TwitchAPI(args['--oauth'])
v = api.find_vod(channel)
else:
print('Assuming that stream is online and VOD is correct')
v = args['<vod>']
Expand Down
37 changes: 26 additions & 11 deletions twitch_utils/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,42 @@ class TwitchAPI:
@staticmethod
def _session(token: str) -> Session:
s = Session()
s.headers['Acccept'] = 'application/vnd.twitchtv.v5+json'
s.headers['Client-ID'] = 'kimne78kx3ncx6brgo4mv6wki5h1ko'
s.headers['Authorization'] = f'Bearer {token}'
return s

def __init__(self, oauth: str):
self.session = self._session(oauth)

def get(self, namespace: str, method: str, **payload):
url = f'https://api.twitch.tv/{namespace}/{method}'

if len(payload.keys()) > 0:
params = '&'.join([f'{k}={v}' for k, v in payload.items()])
url += '?' + params

res = self.session.get(url)
def gql(self, query: str) -> dict:
res = self.session.post('https://gql.twitch.tv/gql', json={'query': query})

if res.status_code == 200:
return res.json()
else:
raise Exception(res.text)

def helix(self, method: str, **payload):
return self.get('helix', method, **payload)
def find_vod(self, user: str) -> str:
res = self.gql(f'''
query {{
user(login: "{user}") {{
stream {{
archiveVideo {{
id
}}
}}
}}
}}
''')

user = res['data']['user']

if user is None:
raise Exception('Channel not found')

stream = user['stream']

if stream is None:
raise Exception('Stream appears to be offline')

return stream['archiveVideo']['id']

0 comments on commit 2c091e5

Please sign in to comment.