<commit_before>import time
import json
from bitmovin.bitmovin_object import BitmovinObject
from bitmovin.errors import BitmovinApiError, InvalidStatusError
from bitmovin.rest import BitmovinHttpClient
from bitmovin.resources import ResourceResponse, Status, EncodingStatus
from bitmovin.services.parsing_utils import ParsingUtils
from bitmovin.utils import BitmovinJSONEncoder


class EncodingControlService(BitmovinObject):

    def __init__(self, http_client, relative_url):
        super().__init__()
        self.http_client = http_client  # type: BitmovinHttpClient
        self.parsing_utils = ParsingUtils()
        self.relative_url = relative_url

    def start(self, encoding_id):
        self.parsing_utils.check_arg_valid(encoding_id)
        url = self.relative_url + '/{}/start'.format(encoding_id)
        response = self.http_client.post_empty_body(url)

        if response.status == Status.ERROR.value:
            raise BitmovinApiError('Response was not successful', response)
        if response.status == Status.SUCCESS.value:
            created_resource = self.parsing_utils.parse_bitmovin_minimal_model_from_response(response=response)
            return ResourceResponse(response=response, resource=created_resource)
        raise InvalidStatusError('Unknown status {} received'.format(response.status))

    def stop(self, encoding_id):
        self.parsing_utils.check_arg_valid(encoding_id)
        url = self.relative_url + '/{}/stop'.format(encoding_id)
        response = self.http_client.post_empty_body(url)

        if response.status == Status.ERROR.value:
            raise BitmovinApiError('Response was not successful', response)
        if response.status == Status.SUCCESS.value:
            created_resource = self.parsing_utils.parse_bitmovin_minimal_model_from_response(response=response)
            return ResourceResponse(response=response, resource=created_resource)
        raise InvalidStatusError('Unknown status {} received'.format(response.status))

    def start_live(self, encoding_id):
        self.parsing_utils.check_arg_valid(encoding_id)
        url = self.relative_url + '/{}/live/start'.format(encoding_id)
        response = self.http_client.post_empty_body(url)

        if response.status == Status.ERROR.value:
            raise BitmovinApiError('Response was not successful', response)
        if response.status == Status.SUCCESS.value:
            created_resource = self.parsing_utils.parse_bitmovin_minimal_model_from_response(response=response)
            return ResourceResponse(response=response, resource=created_resource)
        raise InvalidStatusError('Unknown status {} received'.format(response.status))

    def stop_live(self, encoding_id):
        self.parsing_utils.check_arg_valid(encoding_id)
        url = self.relative_url + '/{}/live/stop'.format(encoding_id)
        response = self.http_client.post_empty_body(url)

        if response.status == Status.ERROR.value:
            raise BitmovinApiError('Response was not successful', response)
        if response.status == Status.SUCCESS.value:
            created_resource = self.parsing_utils.parse_bitmovin_minimal_model_from_response(response=response)
            return ResourceResponse(response=response, resource=created_resource)
        raise InvalidStatusError('Unknown status {} received'.format(response.status))

    def status(self, encoding_id):
        self.parsing_utils.check_arg_valid(encoding_id)
        url = self.relative_url + '/{}/status'.format(encoding_id)
        response = self.http_client.get(url)

        if response.status == Status.ERROR.value:
            raise BitmovinApiError('Response was not successful', response)
        if response.status == Status.SUCCESS.value:
            created_resource = self.parsing_utils.parse_bitmovin_resource_from_response(response=response,
                                                                                        class_=EncodingStatus)
            return ResourceResponse(response=response, resource=created_resource)
        raise InvalidStatusError('Unknown status {} received'.format(response.status))

    def wait_until_finished(self, encoding_id, check_interval=5):
        status_response = None
        encoding_status = EncodingStatus(None)

        while encoding_status.status != 'FINISHED' and encoding_status.status != 'ERROR':
            status_response = self.status(encoding_id=encoding_id)
            encoding_status = status_response.resource  # type: EncodingStatus
            self.logger.info("Encoding status: {}".format(encoding_status.status))
            self.logger.info("Will check again in {} seconds...".format(check_interval))
            time.sleep(check_interval)

        self.logger.info("Encoding Status: {}".format(json.dumps(obj=encoding_status, cls=BitmovinJSONEncoder)))

        if encoding_status.status == 'FINISHED':
            return True

        raise BitmovinApiError("Encoding with ID '{}' was not successfull! Status: {}".format(
            encoding_id, encoding_status.status), status_response)
<commit_msg>added wait_until_running (needed for livestreams)<commit_after>import time
import json
from bitmovin.bitmovin_object import BitmovinObject
from bitmovin.errors import BitmovinApiError, InvalidStatusError
from bitmovin.rest import BitmovinHttpClient
from bitmovin.resources import ResourceResponse, Status, EncodingStatus
from bitmovin.services.parsing_utils import ParsingUtils
from bitmovin.utils import BitmovinJSONEncoder


class EncodingControlService(BitmovinObject):

    def __init__(self, http_client, relative_url):
        super().__init__()
        self.http_client = http_client  # type: BitmovinHttpClient
        self.parsing_utils = ParsingUtils()
        self.relative_url = relative_url

    def start(self, encoding_id):
        self.parsing_utils.check_arg_valid_uuid(encoding_id)
        url = self.relative_url + '/{}/start'.format(encoding_id)
        response = self.http_client.post_empty_body(url)

        if response.status == Status.ERROR.value:
            raise BitmovinApiError('Response was not successful', response)
        if response.status == Status.SUCCESS.value:
            created_resource = self.parsing_utils.parse_bitmovin_minimal_model_from_response(response=response)
            return ResourceResponse(response=response, resource=created_resource)
        raise InvalidStatusError('Unknown status {} received'.format(response.status))

    def stop(self, encoding_id):
        self.parsing_utils.check_arg_valid_uuid(encoding_id)
        url = self.relative_url + '/{}/stop'.format(encoding_id)
        response = self.http_client.post_empty_body(url)

        if response.status == Status.ERROR.value:
            raise BitmovinApiError('Response was not successful', response)
        if response.status == Status.SUCCESS.value:
            created_resource = self.parsing_utils.parse_bitmovin_minimal_model_from_response(response=response)
            return ResourceResponse(response=response, resource=created_resource)
        raise InvalidStatusError('Unknown status {} received'.format(response.status))

    def start_live(self, encoding_id, stream_key):
        self.parsing_utils.check_arg_valid_uuid(encoding_id)
        self.parsing_utils.check_not_blank(stream_key)
        url = self.relative_url + '/{}/live/start'.format(encoding_id)
        response = self.http_client.post(url, {'streamKey': stream_key})

        if response.status == Status.ERROR.value:
            raise BitmovinApiError('Response was not successful', response)
        if response.status == Status.SUCCESS.value:
            created_resource = self.parsing_utils.parse_bitmovin_minimal_model_from_response(response=response)
            return ResourceResponse(response=response, resource=created_resource)
        raise InvalidStatusError('Unknown status {} received'.format(response.status))

    def stop_live(self, encoding_id):
        self.parsing_utils.check_arg_valid_uuid(encoding_id)
        url = self.relative_url + '/{}/live/stop'.format(encoding_id)
        response = self.http_client.post_empty_body(url)

        if response.status == Status.ERROR.value:
            raise BitmovinApiError('Response was not successful', response)
        if response.status == Status.SUCCESS.value:
            created_resource = self.parsing_utils.parse_bitmovin_minimal_model_from_response(response=response)
            return ResourceResponse(response=response, resource=created_resource)
        raise InvalidStatusError('Unknown status {} received'.format(response.status))

    def status(self, encoding_id):
        self.parsing_utils.check_arg_valid_uuid(encoding_id)
        url = self.relative_url + '/{}/status'.format(encoding_id)
        response = self.http_client.get(url)

        if response.status == Status.ERROR.value:
            raise BitmovinApiError('Response was not successful', response)
        if response.status == Status.SUCCESS.value:
            created_resource = self.parsing_utils.parse_bitmovin_resource_from_response(response=response,
                                                                                        class_=EncodingStatus)
            return ResourceResponse(response=response, resource=created_resource)
        raise InvalidStatusError('Unknown status {} received'.format(response.status))

    def wait_until_finished(self, encoding_id, check_interval=5):
        status_response = None
        encoding_status = EncodingStatus(None)

        while encoding_status.status != 'FINISHED' and encoding_status.status != 'ERROR':
            status_response = self.status(encoding_id=encoding_id)
            encoding_status = status_response.resource  # type: EncodingStatus
            self.logger.info("Encoding status: {}".format(encoding_status.status))
            self.logger.info("Will check again in {} seconds...".format(check_interval))
            time.sleep(check_interval)

        self.logger.info("Encoding Status: {}".format(json.dumps(obj=encoding_status, cls=BitmovinJSONEncoder)))

        if encoding_status.status == 'FINISHED':
            return True

        raise BitmovinApiError("Encoding with ID '{}' was not successfull! Status: {}".format(
            encoding_id, encoding_status.status), status_response)


    def wait_until_running(self, encoding_id, check_interval=5):
        status_response = None
        encoding_status = EncodingStatus(None)

        while encoding_status.status != 'RUNNING' and encoding_status.status != 'ERROR':
            status_response = self.status(encoding_id=encoding_id)
            encoding_status = status_response.resource  # type: EncodingStatus
            self.logger.info("Encoding status: {}".format(encoding_status.status))
            self.logger.info("Will check again in {} seconds...".format(check_interval))
            time.sleep(check_interval)

        self.logger.info("Encoding Status: {}".format(json.dumps(obj=encoding_status, cls=BitmovinJSONEncoder)))

        if encoding_status.status == 'RUNNING':
            return True

        raise BitmovinApiError("Encoding with ID '{}' was not successfull! Status: {}".format(
            encoding_id, encoding_status.status), status_response)

{'fraction_non_alphanumeric': 0.09127195316657796, 'fraction_numerical': 0.00026609898882384245, 'mean_word_length': 3.4635787806809186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5523210', 'n_tokens_mistral': 2989, 'n_tokens_neox': 2938, 'n_words': 552}