<commit_before># Tweepy
# Copyright 2009 <NAME>
# See LICENSE

from urllib2 import Request, urlopen
import base64

import oauth
from error import TweepError

class AuthHandler(object):

  def apply_auth(self, url, method, headers, parameters):
    """Apply authentication headers to request"""
    raise NotImplemented

class BasicAuthHandler(AuthHandler):

  def __init__(self, username, password):
    self._b64up = base64.b64encode('%s:%s' % (username, password))

  def apply_auth(self, url, method, headers, parameters):
    headers['Authorization'] = 'Basic %s' % self._b64up

"""OAuth authentication handler"""
class OAuthHandler(AuthHandler):

  REQUEST_TOKEN_URL = 'http://twitter.com/oauth/request_token'
  AUTHORIZATION_URL = 'http://twitter.com/oauth/authorize'
  ACCESS_TOKEN_URL = 'http://twitter.com/oauth/access_token'

  def __init__(self, consumer_key, consumer_secrete):
    self._consumer = oauth.OAuthConsumer(consumer_key, consumer_secrete)
    self._sigmethod = oauth.OAuthSignatureMethod_HMAC_SHA1()
    self.request_token = None
    self.access_token = None

  def apply_auth(self, url, method, headers, parameters):
    request = oauth.OAuthRequest.from_consumer_and_token(self._consumer,
        http_url=url, http_method=method, token=self.access_token, parameters=parameters)
    request.sign_request(self._sigmethod, self._consumer, self.access_token)
    headers.update(request.to_header())

  def _get_request_token(self):
    try:
      request = oauth.OAuthRequest.from_consumer_and_token(self._consumer, http_url = self.REQUEST_TOKEN_URL)
      request.sign_request(self._sigmethod, self._consumer, None)
      resp = urlopen(Request(self.REQUEST_TOKEN_URL, headers=request.to_header()))
      return oauth.OAuthToken.from_string(resp.read())

    except Exception, e:
      raise TweepError(e)

  def get_authorization_url(self, callback=None):
    try:
      # get the request token
      self.request_token = self._get_request_token()

      # build auth request and return as url
      request = oauth.OAuthRequest.from_token_and_callback(
          token=self.request_token, callback=callback, http_url=self.AUTHORIZATION_URL)
      return request.to_url()

    except Exception, e:
      raise TweepError(e)

  def get_access_token(self, verifier):
    try:
      # build request
      request = oauth.OAuthRequest.from_consumer_and_token(self._consumer,
          token=self.request_token, http_url=self.ACCESS_TOKEN_URL)
      request.set_parameter('oauth_verifier', verifier)
      request.sign_request(self._sigmethod, self._consumer, self.request_token)

      # send request
      resp = urlopen(Request(self.ACCESS_TOKEN_URL, headers=request.to_header()))
      self.access_token = oauth.OAuthToken.from_string(resp.read())
    except Exception, e:
      raise TweepError(e)
      

<commit_msg>Added support for oauth callbacks.<commit_after># Tweepy
# Copyright 2009 <NAME>
# See LICENSE

from urllib2 import Request, urlopen
from urllib import quote
import base64

import oauth
from error import TweepError

class AuthHandler(object):

  def apply_auth(self, url, method, headers, parameters):
    """Apply authentication headers to request"""
    raise NotImplemented

class BasicAuthHandler(AuthHandler):

  def __init__(self, username, password):
    self._b64up = base64.b64encode('%s:%s' % (username, password))

  def apply_auth(self, url, method, headers, parameters):
    headers['Authorization'] = 'Basic %s' % self._b64up

"""OAuth authentication handler"""
class OAuthHandler(AuthHandler):

  REQUEST_TOKEN_URL = 'http://twitter.com/oauth/request_token'
  AUTHORIZATION_URL = 'http://twitter.com/oauth/authorize'
  ACCESS_TOKEN_URL = 'http://twitter.com/oauth/access_token'

  def __init__(self, consumer_key, consumer_secrete, callback=None):
    self._consumer = oauth.OAuthConsumer(consumer_key, consumer_secrete)
    self._sigmethod = oauth.OAuthSignatureMethod_HMAC_SHA1()
    self.request_token = None
    self.access_token = None
    self.callback = callback

  def apply_auth(self, url, method, headers, parameters):
    request = oauth.OAuthRequest.from_consumer_and_token(self._consumer,
        http_url=url, http_method=method, token=self.access_token, parameters=parameters)
    request.sign_request(self._sigmethod, self._consumer, self.access_token)
    headers.update(request.to_header())

  def _get_request_token(self):
    try:
      request = oauth.OAuthRequest.from_consumer_and_token(self._consumer, http_url = self.REQUEST_TOKEN_URL)
      if self.callback:
        request.set_parameter('oauth_callback', self.callback)
      request.sign_request(self._sigmethod, self._consumer, None)
      resp = urlopen(Request(self.REQUEST_TOKEN_URL, headers=request.to_header()))
      return oauth.OAuthToken.from_string(resp.read())

    except Exception, e:
      raise TweepError(e)

  def get_authorization_url(self):
    """Get the authorization URL to redirect the user"""
    try:
      # get the request token
      self.request_token = self._get_request_token()

      # build auth request and return as url
      request = oauth.OAuthRequest.from_token_and_callback(
          token=self.request_token, http_url=self.AUTHORIZATION_URL)
      return request.to_url()

    except Exception, e:
      raise TweepError(e)

  def get_access_token(self, verifier):
    """After user has authorized the request token, get access token with user supplied verifier."""
    try:
      # build request
      request = oauth.OAuthRequest.from_consumer_and_token(self._consumer,
          token=self.request_token, http_url=self.ACCESS_TOKEN_URL)
      request.set_parameter('oauth_verifier', str(verifier))
      request.sign_request(self._sigmethod, self._consumer, self.request_token)

      # send request
      resp = urlopen(Request(self.ACCESS_TOKEN_URL, headers=request.to_header()))
      self.access_token = oauth.OAuthToken.from_string(resp.read())
    except Exception, e:
      raise TweepError(e)
      


{'fraction_non_alphanumeric': 0.10013351134846461, 'fraction_numerical': 0.0053404539385847796, 'mean_word_length': 4.365264100268576, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4689310', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1837, 'n_words': 377}