<commit_before>import requests
import uuid

from constants import (FETCH_URL_ENDPOINT, FETCH_URL_LAZY_ENDPOINT,
                       FETCH_URL_STATUS_ENDPOINT, FILE_UPLOAD_ENDPOINT,
                       FILE_UPLOAD_STATUS_ENDPOINT, ACL, AWS_ACCESS_KEY_ID,
                       POLICY, SUCCESS_ACTION_STATUS, SIGNATURE, CONTENT_TYPE,
                       QUERY_ENDPOINT, CHECK_LINK_ENDPOINT)
from error import GfycatClientError


class GfycatClient(object):
    def __init__(self):
        # Will hold access tokens and auth credentials when Gfycat decides to
        # implement them.
        pass

    def upload_from_url(self, url):
        """
        Upload a GIF from a URL.
        """
        params = {'fetchUrl': url}
        r = requests.get(FETCH_URL_ENDPOINT, params=params)

        if r.status_code != 200:
            raise GfycatClientError('Error fetching the URL', r.status_code)

        response = r.json()
        if 'error' in response:
            raise GfycatClientError(response['error'])

        return response

    def upload_from_file(self, filename):
        """
        Upload a local file to Gfycat
        """
        key = str(uuid.uuid4())[:8]

        form = [('key', key),
                ('acl', ACL),
                ('AWSAccessKeyId', AWS_ACCESS_KEY_ID),
                ('success_action_status', SUCCESS_ACTION_STATUS),
                ('signature', SIGNATURE),
                ('Content-Type', CONTENT_TYPE),
                ('policy', POLICY)]

        with open(filename, 'rb') as upfile:
            form.append(('file', upfile))
            r = requests.post(FILE_UPLOAD_ENDPOINT, files=form)

        if r.status_code != 200:
            raise GfycatClientError('Error uploading the GIF', r.status_code)

        info = self.uploaded_file_info(key)
        if 'error' in info:
            raise GfycatClientError(info['error'])

        return info

    def uploaded_file_info(self, key):
        """
        Get information about an uploaded GIF.
        """
        r = requests.get(FILE_UPLOAD_STATUS_ENDPOINT + key)
        if r.status_code != 200:
            raise GfycatClientError('Unable to check the status',
                                    r.status_code)

        return r.json()

    def query_gfy(self, gfyname):
        """
        Query a gfy name for URLs and more information.<commit_msg>client: Send form data and file seperately via requests.<commit_after>import requests
import uuid

from constants import (FETCH_URL_ENDPOINT, FETCH_URL_LAZY_ENDPOINT,
                       FETCH_URL_STATUS_ENDPOINT, FILE_UPLOAD_ENDPOINT,
                       FILE_UPLOAD_STATUS_ENDPOINT, ACL, AWS_ACCESS_KEY_ID,
                       POLICY, SUCCESS_ACTION_STATUS, SIGNATURE, CONTENT_TYPE,
                       QUERY_ENDPOINT, CHECK_LINK_ENDPOINT)
from error import GfycatClientError


class GfycatClient(object):
    def __init__(self):
        # Will hold access tokens and auth credentials when Gfycat decides to
        # implement them.
        pass

    def upload_from_url(self, url):
        """
        Upload a GIF from a URL.
        """
        params = {'fetchUrl': url}
        r = requests.get(FETCH_URL_ENDPOINT, params=params)

        if r.status_code != 200:
            raise GfycatClientError('Error fetching the URL', r.status_code)

        response = r.json()
        if 'error' in response:
            raise GfycatClientError(response['error'])

        return response

    def upload_from_file(self, filename):
        """
        Upload a local file to Gfycat
        """
        key = str(uuid.uuid4())[:8]

        form = [('key', key),
                ('acl', ACL),
                ('AWSAccessKeyId', AWS_ACCESS_KEY_ID),
                ('success_action_status', SUCCESS_ACTION_STATUS),
                ('signature', SIGNATURE),
                ('Content-Type', CONTENT_TYPE),
                ('policy', POLICY)]
        data = dict(form)

        files = {'file': open(filename, 'rb')}
        r = requests.post(FILE_UPLOAD_ENDPOINT, data=data, files=files)

        if r.status_code != 200:
            raise GfycatClientError('Error uploading the GIF', r.status_code)

        info = self.uploaded_file_info(key)
        if 'error' in info:
            raise GfycatClientError(info['error'])

        return info

    def uploaded_file_info(self, key):
        """
        Get information about an uploaded GIF.
        """
        r = requests.get(FILE_UPLOAD_STATUS_ENDPOINT + key)
        if r.status_code != 200:
            raise GfycatClientError('Unable to check the status',
                                    r.status_code)

        return r.json()

    def query_gfy(self, gfyname):
        """
        Query a gfy name for URLs and more information.
{'fraction_non_alphanumeric': 0.09146213551499895, 'fraction_numerical': 0.0046150618837843506, 'mean_word_length': 2.0120025268477573, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1497619', 'n_tokens_mistral': 1430, 'n_tokens_neox': 1349, 'n_words': 341}