<commit_before># -*- coding: utf-8 -*-
"""
A python wrapper for put.io APIv2

https://github.com/putdotio/putio-apiv2-python


Usage:

# TODO write usage

"""

import json
import logging
import requests
from urllib import urlencode

logger = logging.getLogger(__name__)

API_URL = 'https://put.io/v2'
ACCESS_TOKEN_URL = 'https://put.io/v2/oauth2/access_token'
AUTHENTICATION_URL = 'https://put.io/v2/oauth2/authenticate'


class AuthHelper(object):
    def __init__(self, client_id, client_secret, redirect_uri):
        self.client_id = client_id
        self.client_secret = client_secret
        self.callback_url = redirect_uri
    
    def get_authentication_url(self):
        params = {
            'client_id': self.client_id,
            'response_type': 'code',
            'redirect_uri': self.callback_url
        }
        query_str = urlencode(query)
        return AUTHENTICATION_URL + "?" + query_str
    
    def get_access_token(self, code):
        params = {
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'grant_type': 'authorization_code',
            'redirect_uri': self.callback_url,
            'code': code
        }
        r = requests.get(ACCESS_TOKEN_URL, params=params)
        d = json.loads(r.content)
        return d['access_token']


class Client(object):
    def __init__(self, access_token):
        self.access_token = access_token
        
        attributes = {'client': self}
        self.File = type('File', (_File,), attributes)
        #self.Transfer = type('Transfer', (_Transfer,), attributes)
    
    def request(self, path, method='GET', params=None, data=None, as_dict=True):
        if not params:
            params = {}
        params['oauth_token'] = self.access_token
        
        url = API_URL + path
        logger.debug('url: %s', url)
        r = requests.request(method, url, params=params, data=data, allow_redirects=True)
        logger.debug('response: %s', r)
        if not as_dict:
            return r.content
        
        logger.debug('content: %s', r.content)
        r = json.loads(r.content)
        if r['status'] == 'ERROR':
            raise Exception(r['error_type'])
        return r


class _BaseResource(object):
    def __init__(self, resource_dict):
        self.__dict__.update(resource_dict)
        
    def __str__(self):
        return self.name

    def __repr__(self):
        # shorten name for display
        name = self.name[:17] + '...' if len(self.name) > 20 else self.name
        return 'File(%s, "%s")' % (self.id, name)


class _File(_BaseResource):
    @classmethod
    def list(cls, parent_id=0):
        d = cls.client.request('/files/list', params=dict(parent_id=parent_id))
        files = d['files']
        files = [cls(f) for f in files]
        ids = [f.id for f in files]
        return dict(zip(ids, files))
    
    def download(self):
        return self.client.request('/files/%s' % self.id, as_dict=False)

    def delete(self):
        return self.client.request('/files/%s/delete' % self.id)<commit_msg>download<commit_after># -*- coding: utf-8 -*-
"""
A python wrapper for put.io APIv2

https://github.com/putdotio/putio-apiv2-python


Usage:

# TODO write usage

"""

import os
import re
import json
import logging
import requests
from urllib import urlencode
from pdb import set_trace as st

logger = logging.getLogger(__name__)

API_URL = 'https://put.io/v2'
ACCESS_TOKEN_URL = 'https://put.io/v2/oauth2/access_token'
AUTHENTICATION_URL = 'https://put.io/v2/oauth2/authenticate'


class AuthHelper(object):
    def __init__(self, client_id, client_secret, redirect_uri):
        self.client_id = client_id
        self.client_secret = client_secret
        self.callback_url = redirect_uri
    
    def get_authentication_url(self):
        params = {
            'client_id': self.client_id,
            'response_type': 'code',
            'redirect_uri': self.callback_url
        }
        query_str = urlencode(query)
        return AUTHENTICATION_URL + "?" + query_str
    
    def get_access_token(self, code):
        params = {
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'grant_type': 'authorization_code',
            'redirect_uri': self.callback_url,
            'code': code
        }
        r = requests.get(ACCESS_TOKEN_URL, params=params)
        d = json.loads(r.content)
        return d['access_token']


class Client(object):
    def __init__(self, access_token):
        self.access_token = access_token
        
        attributes = {'client': self}
        self.File = type('File', (_File,), attributes)
        #self.Transfer = type('Transfer', (_Transfer,), attributes)
    
    def request(self, path, method='GET', params=None, data=None, raw=False):
        if not params:
            params = {}
        params['oauth_token'] = self.access_token
        
        url = API_URL + path
        logger.debug('url: %s', url)
        
        r = requests.request(method, url, params=params, data=data, allow_redirects=True)
        logger.debug('response: %s', r)
        
        if raw:
            return r
        
        logger.debug('content: %s', r.content)
        r = json.loads(r.content)
        if r['status'] == 'ERROR':
            raise Exception(r['error_type'])
        
        return r


class _BaseResource(object):
    def __init__(self, resource_dict):
        '''Construct the object from a dict'''
        self.__dict__.update(resource_dict)
        
    def __str__(self):
        return self.name

    def __repr__(self):
        # shorten name for display
        name = self.name[:17] + '...' if len(self.name) > 20 else self.name
        return 'File(%s, "%s")' % (self.id, name)


class _File(_BaseResource):
    @classmethod
    def list(cls, parent_id=0):
        d = cls.client.request('/files/list', params=dict(parent_id=parent_id))
        files = d['files']
        files = [cls(f) for f in files]
        ids = [f.id for f in files]
        return dict(zip(ids, files))
    
    def download(self, dest='.'):
        r = self.client.request('/files/%s' % self.id, raw=True)
        print r
        filename = re.match('attachment; filename\="(.*)"', r.headers['Content-Disposition']).groups()[0]
        with open(os.path.join(dest, filename), 'wb') as f:
            for data in r.iter_content():
                f.write(data)

    def delete(self):
        return self.client.request('/files/%s/delete' % self.id)
{'fraction_non_alphanumeric': 0.1297148114075437, 'fraction_numerical': 0.004139834406623735, 'mean_word_length': 2.374547335747543, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1935171', 'n_tokens_mistral': 2036, 'n_tokens_neox': 2018, 'n_words': 448}