<commit_before>import json
import xmltodict
from kodihelper import KodiHelper
helper = KodiHelper()

class Widevine(object):
    license_url = helper.c.config['settings']['drmProxy']

    def get_kid(self, mpd_url):
        # TODO: parse this with ElementTree instead
        mpd_data = helper.c.make_request(mpd_url, 'get')
        mpd_dict = xmltodict.parse(mpd_data)
        for adapt_set in mpd_dict['MPD']['Period']['AdaptationSet']:
            if 'ContentProtection' in adapt_set.keys():
                for cenc in adapt_set['ContentProtection']:
                    if '@cenc:default_KID' in cenc.keys():
                        kid = cenc['@cenc:default_KID']
                        return kid

    def get_drm_info(self, challenge):
        """Converts the Widevine challenge to the format used by the C More Widevine proxy."""
        drm_info = []
        for byte in bytearray(challenge):
            drm_info.append(byte)
        return drm_info

    def get_license(self, mpd_url, challenge, token):
        post_data = {
            'drm_info': self.get_drm_info(challenge),
            'kid': self.get_kid(mpd_url),
            'token': token
        }
        license = helper.c.make_request(self.license_url, 'post', payload=json.dumps(post_data))

        return license<commit_msg>Cleanup Widevine.py<commit_after>import json
import xmltodict
from kodihelper import KodiHelper
helper = KodiHelper()

class Widevine(object):
    license_url = helper.c.config['settings']['drmProxy']

    def get_kid(self, mpd_url):
        # TODO: parse this with ElementTree instead
        mpd_data = helper.c.make_request(mpd_url, 'get')
        mpd_dict = xmltodict.parse(mpd_data)
        for adapt_set in mpd_dict['MPD']['Period']['AdaptationSet']:
            if 'ContentProtection' in adapt_set.keys():
                for cenc in adapt_set['ContentProtection']:
                    if '@cenc:default_KID' in cenc.keys():
                        kid = cenc['@cenc:default_KID']
                        return kid

    def get_license(self, mpd_url, wv_challenge, token):
        post_data = {
            'drm_info': [x for x in bytearray(wv_challenge)],  # convert challenge to a list of bytes
            'kid': self.get_kid(mpd_url),
            'token': token
        }
        license = helper.c.make_request(self.license_url, 'post', payload=json.dumps(post_data))

        return license
{'fraction_non_alphanumeric': 0.10672757475083057, 'fraction_numerical': 0.0, 'mean_word_length': 2.4761904761904763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '891375', 'n_tokens_mistral': 770, 'n_tokens_neox': 723, 'n_words': 168}