<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> from random import getrandbits <EOL> from time import time <EOL> from . util import PY_3_OR_HIGHER <EOL> try : <EOL> import urllib . parse as urllib_parse <EOL> from urllib . parse import urlencode <EOL> except ImportError : <EOL> import urllib2 as urllib_parse <EOL> from urllib import urlencode <EOL> import hashlib <EOL> import hmac <EOL> import base64 <EOL> from . auth import Auth , MissingCredentialsError <EOL> def write_token_file ( filename , oauth_token , oauth_token_secret ) : <EOL> """<STR_LIT>""" <EOL> oauth_file = open ( filename , '<STR_LIT:w>' ) <EOL> print ( oauth_token , file = oauth_file ) <EOL> print ( oauth_token_secret , file = oauth_file ) <EOL> oauth_file . close ( ) <EOL> def read_token_file ( filename ) : <EOL> """<STR_LIT>""" <EOL> f = open ( filename ) <EOL> return f . readline ( ) . strip ( ) , f . readline ( ) . strip ( ) <EOL> class OAuth ( Auth ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , token , token_secret , consumer_key , consumer_secret ) : <EOL> """<STR_LIT>""" <EOL> self . token = token <EOL> self . token_secret = token_secret <EOL> self . consumer_key = consumer_key <EOL> self . consumer_secret = consumer_secret <EOL> if token_secret is None or consumer_secret is None : <EOL> raise MissingCredentialsError ( <EOL> '<STR_LIT>' ) <EOL> def encode_params ( self , base_url , method , params ) : <EOL> params = params . copy ( ) <EOL> if self . token : <EOL> params [ '<STR_LIT>' ] = self . token <EOL> params [ '<STR_LIT>' ] = self . consumer_key <EOL> params [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> params [ '<STR_LIT>' ] = '<STR_LIT:1.0>' <EOL> params [ '<STR_LIT>' ] = str ( int ( time ( ) ) ) <EOL> params [ '<STR_LIT>' ] = str ( getrandbits ( <NUM_LIT:64> ) ) <EOL> enc_params = urlencode_noplus ( sorted ( params . items ( ) ) ) <EOL> key = self . consumer_secret + "<STR_LIT:&>" + urllib_parse . quote ( self . token_secret , safe = '<STR_LIT>' ) <EOL> message = '<STR_LIT:&>' . join ( <EOL> urllib_parse . quote ( i , safe = '<STR_LIT>' ) for i in [ method . upper ( ) , base_url , enc_params ] ) <EOL> signature = ( base64 . b64encode ( hmac . new ( <EOL> key . encode ( '<STR_LIT:ascii>' ) , message . encode ( '<STR_LIT:ascii>' ) , hashlib . sha1 ) <EOL> . digest ( ) ) ) <EOL> return enc_params + "<STR_LIT:&>" + "<STR_LIT>" + urllib_parse . quote ( signature , safe = '<STR_LIT>' ) <EOL> def generate_headers ( self ) : <EOL> return { } <EOL> def urlencode_noplus ( query ) : <EOL> if not PY_3_OR_HIGHER : <EOL> new_query = [ ] <EOL> TILDE = '<STR_LIT>' <EOL> for k , v in query : <EOL> if type ( k ) is unicode : k = k . encode ( '<STR_LIT:utf-8>' ) <EOL> k = str ( k ) . replace ( "<STR_LIT>" , TILDE ) <EOL> if type ( v ) is unicode : v = v . encode ( '<STR_LIT:utf-8>' ) <EOL> v = str ( v ) . replace ( "<STR_LIT>" , TILDE ) <EOL> new_query . append ( ( k , v ) ) <EOL> query = new_query <EOL> return urlencode ( query ) . replace ( TILDE , "<STR_LIT>" ) . replace ( "<STR_LIT:+>" , "<STR_LIT>" ) <EOL> return urlencode ( query , safe = '<STR_LIT>' ) . replace ( "<STR_LIT:+>" , "<STR_LIT>" ) </s>
