<commit_before>import hashlib
import simplejson
import urllib2
import cookielib

from pybrightcove.multipart import MultipartPostHandler
from pybrightcove import config, UserAgent
from pybrightcove.exceptions import BrightcoveClientError, BrightcoveError, BrightcoveGeneralError
from pybrightcove.video import Video
from pybrightcove.playlist import Playlist


    

class Connection(object):
    def __init__(self, url=None, read_token=None, write_token=None):
        if url:
            self.url = url
        elif config.has_option('Connection', 'url'):
            self.url = config.get('Connection', 'url')
        
        if read_token:
            self.read_token = read_token
        elif config.has_option('Connection', 'read_token'):
            self.read_token = config.get('Connection', 'read_token')
        
        if write_token:
            self.write_token = write_token
        elif config.has_option('Connection', 'write_token'):
            self.write_token = config.get('Connection', 'write_token')
            
    def _post_file(self, data, file_to_upload):
        cookies = cookielib.CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), MultipartPostHandler)
        params = { "JSONRPC" : simplejson.dumps(data), "filePath" : open(file_to_upload, "rb") }
        r = opener.open("http://api.brightcove.com/services/post", params)
        print r.read()
        

    def create_video(self, filename, video=None, do_checksum=True, create_multiple_renditions=True, preserve_source_rendition=True):
        data = {"method":"create_video",
                "params": {
                    "token": self.write_token, 
                    "create_multiple_renditions": create_multiple_renditions,
                    "preserve_source_rendition": preserve_source_rendition
                    }
                }
        if video:
            data['params']['video'] = video.to_dict()
        if do_checksum:
            m = hashlib.md5()
            m.update(open(filename, 'rb').read())
            data['params']['file_checksum'] = m.hexdigest()
        r = self._post_file(data=data, file_to_upload=filename)
        return r
<commit_msg>import has to be in the function for some reason<commit_after>import hashlib
import simplejson
import urllib2
import cookielib

from pybrightcove import config, UserAgent
from pybrightcove.exceptions import BrightcoveClientError, BrightcoveError, BrightcoveGeneralError
from pybrightcove.video import Video
from pybrightcove.playlist import Playlist


    

class Connection(object):
    def __init__(self, url=None, read_token=None, write_token=None):
        if url:
            self.url = url
        elif config.has_option('Connection', 'url'):
            self.url = config.get('Connection', 'url')
        
        if read_token:
            self.read_token = read_token
        elif config.has_option('Connection', 'read_token'):
            self.read_token = config.get('Connection', 'read_token')
        
        if write_token:
            self.write_token = write_token
        elif config.has_option('Connection', 'write_token'):
            self.write_token = config.get('Connection', 'write_token')
            
    def _post_file(self, data, file_to_upload):
        from pybrightcove.multipart import MultipartPostHandler
        cookies = cookielib.CookieJar()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookies), MultipartPostHandler)
        params = { "JSONRPC" : simplejson.dumps(data), "filePath" : open(file_to_upload, "rb") }
        r = opener.open("http://api.brightcove.com/services/post", params)
        print r.read()
        

    def create_video(self, filename, video=None, do_checksum=True, create_multiple_renditions=True, preserve_source_rendition=True):
        data = {"method":"create_video",
                "params": {
                    "token": self.write_token, 
                    "create_multiple_renditions": create_multiple_renditions,
                    "preserve_source_rendition": preserve_source_rendition
                    }
                }
        if video:
            data['params']['video'] = video.to_dict()
        if do_checksum:
            m = hashlib.md5()
            m.update(open(filename, 'rb').read())
            data['params']['file_checksum'] = m.hexdigest()
        r = self._post_file(data=data, file_to_upload=filename)
        return r

{'fraction_non_alphanumeric': 0.10177887863093897, 'fraction_numerical': 0.0018013960819635217, 'mean_word_length': 2.640983606557377, 'pattern_counts': {'":': 10, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '328561', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1262, 'n_words': 251}