<commit_before>import requests
import json
import re
import random
import m3u8
import subprocess


USHER_API = 'http://usher.twitch.tv/api/channel/hls/{channel}.m3u8?player=twitchweb' +\
    '&token={token}&sig={sig}&$allow_audio_only=true&allow_source=true' + \
    '&type=any&p={random}'
TOKEN_API = 'http://api.twitch.tv/api/channels/{channel}/access_token'
TWITCH_URL = 'https://www.twitch.tv'


def get_client_id():
    r = requests.get(TWITCH_URL)
    if r.status_code >= 400:
        raise Exception('Error fetching twitch site.')
    script_matches = re.findall(
        r'<script src="(/assets/global-fe\w*.js)" type="text/javascript"></script>', r.text)
    if len(script_matches) != 1:
        raise Exception('Error finding frontend script on twitch page.')
    # Get the twitch frontend script that contains the Client-ID we need.
    r = requests.get(TWITCH_URL + script_matches[0])
    if r.status_code >= 400:
        raise Exception('Error fetching twitch frontend script.')

    # Find the client ID with a regex that totally wont match anything else /s
    client_ids = re.findall(r'var n={},r="(\w*)";', r.text)
    if len(client_ids) != 1:
        raise Exception(
            'Error finding client ID in twitch frontend script. Got {}'.format(client_ids))
    return client_ids[0]


def get_token_and_signature(channel, client_id):
    url = TOKEN_API.format(channel=channel)
    headers = {'Client-ID': client_id}
    r = requests.get(url, headers=headers)
    if r.status_code >= 400:
        raise Exception('Error requesting token from twitch: {}'.format(r.text))
    data = r.json()
    return data['token'], data['sig']


def get_live_stream(channel):
    client_id = get_client_id()
    token, sig = get_token_and_signature(channel, client_id)
    url = USHER_API.format(channel=channel, sig=sig, token=token, random=random.randint(0, 1E7))
    r = requests.get(url)
    m3u8_obj = m3u8.loads(r.text)
    return m3u8_obj
<commit_msg>Twitch updated their frontend script name ":)"<commit_after>import requests
import json
import re
import random
import m3u8
import subprocess


USHER_API = 'http://usher.twitch.tv/api/channel/hls/{channel}.m3u8?player=twitchweb' +\
    '&token={token}&sig={sig}&$allow_audio_only=true&allow_source=true' + \
    '&type=any&p={random}'
TOKEN_API = 'http://api.twitch.tv/api/channels/{channel}/access_token'
TWITCH_URL = 'https://www.twitch.tv'


def get_client_id():
    r = requests.get(TWITCH_URL)
    if r.status_code >= 400:
        raise Exception('Error fetching twitch site.')
    script_matches = re.findall(
        r'<script src="(/assets/global-\w*.js)" type="text/javascript"></script>', r.text)
    if len(script_matches) != 1:
        raise Exception('Error finding frontend script on twitch page.')
    # Get the twitch frontend script that contains the Client-ID we need.
    r = requests.get(TWITCH_URL + script_matches[0])
    if r.status_code >= 400:
        raise Exception('Error fetching twitch frontend script.')

    # Find the client ID with a regex that totally wont match anything else /s
    client_ids = re.findall(r'var n={},r="(\w*)";', r.text)
    if len(client_ids) != 1:
        raise Exception(
            'Error finding client ID in twitch frontend script. Got {}'.format(client_ids))
    return client_ids[0]


def get_token_and_signature(channel, client_id):
    url = TOKEN_API.format(channel=channel)
    headers = {'Client-ID': client_id}
    r = requests.get(url, headers=headers)
    if r.status_code >= 400:
        raise Exception('Error requesting token from twitch: {}'.format(r.text))
    data = r.json()
    return data['token'], data['sig']


def get_live_stream(channel):
    client_id = get_client_id()
    token, sig = get_token_and_signature(channel, client_id)
    url = USHER_API.format(channel=channel, sig=sig, token=token, random=random.randint(0, 1E7))
    r = requests.get(url)
    m3u8_obj = m3u8.loads(r.text)
    return m3u8_obj

{'fraction_non_alphanumeric': 0.13552465233881164, 'fraction_numerical': 0.013147914032869785, 'mean_word_length': 3.806804374240583, 'pattern_counts': {'":': 1, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6768761', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1319, 'n_words': 312}