Skip to content

Commit 99b4ac5

Browse files
committed
Port oauth.py to Python 3.0 and update the twitter apps
There are a few more things to do - mostly encode and decode bits but the hard bits are done.
1 parent 9e8c3c1 commit 99b4ac5

8 files changed

Lines changed: 21 additions & 15 deletions

File tree

code3/hidden.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Keep this file separate
22

3+
# https://apps.twitter.com/
4+
# Create new App
5+
36
def oauth() :
47
return { "consumer_key" : "h7Lu...Ng",
58
"consumer_secret" : "dNKenAC3New...mmn7Q",

code3/oauth.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import time
2828
import random
2929
import urllib.parse
30-
import hmac
3130
import binascii
3231

3332

@@ -566,16 +565,17 @@ def build_signature(self, oauth_request, consumer, token):
566565
key, raw = self.build_signature_base_string(oauth_request, consumer,
567566
token)
568567

569-
# HMAC object.
570-
try:
571-
import hashlib # 2.5
572-
hashed = hmac.new(key, raw, hashlib.sha1)
573-
except:
574-
import sha # Deprecated
575-
hashed = hmac.new(key, raw, sha)
568+
# Compute the oauth hmac Python 3.0 style
569+
# http://stackoverflow.com/questions/1306550/calculating-a-sha-hash-with-a-string-secret-key-in-python
570+
import hashlib
571+
import base64
572+
import hmac
573+
hashed = hmac.new(bytearray(key, 'latin1'), bytearray(raw, 'latin1'), hashlib.sha1)
576574

577575
# Calculate the digest base 64.
578-
return binascii.b2a_base64(hashed.digest())[:-1]
576+
digest = hashed.digest()
577+
enc = base64.b64encode(digest).decode() # py3k-mode
578+
return enc
579579

580580

581581
class OAuthSignatureMethod_PLAINTEXT(OAuthSignatureMethod):

code3/twfriends.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
print('Retrieving account', acct)
4242
connection = urllib.request.urlopen(url)
4343
data = connection.read()
44-
headers = connection.info().dict
44+
headers = dict(connection.getheaders())
45+
4546
print('Remaining', headers['x-rate-limit-remaining'])
4647

4748
js = json.loads(data)

code3/twitter1.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
connection = urllib.request.urlopen(url)
1414
data = connection.read()
1515
print(data[:250])
16-
headers = connection.info().dict
16+
headers = dict(connection.getheaders())
1717
# print headers
1818
print('Remaining', headers['x-rate-limit-remaining'])

code3/twitter2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
print('Retrieving', url)
1414
connection = urllib.request.urlopen(url)
1515
data = connection.read()
16-
headers = connection.info().dict
16+
headers = dict(connection.getheaders())
1717
print('Remaining', headers['x-rate-limit-remaining'])
1818
js = json.loads(data)
1919
print(json.dumps(js, indent=4))

code3/twspider.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
print('Retrieving', url)
2727
connection = urllib.request.urlopen(url)
2828
data = connection.read()
29-
headers = connection.info().dict
29+
headers = dict(connection.getheaders())
30+
3031
print('Remaining', headers['x-rate-limit-remaining'])
3132
js = json.loads(data)
3233
# print json.dumps(js, indent=4)

code3/twtest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
connection = urllib.request.urlopen(url)
99
data = connection.read()
1010
print(data)
11-
headers = connection.info().dict
11+
12+
headers = dict(connection.getheaders())
1213
print(headers)

code3/twurl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ def test_me() :
2121
connection = urllib.request.urlopen(url)
2222
data = connection.read()
2323
print(data)
24-
headers = connection.info().dict
24+
headers = dict(connection.getheaders())
2525
print(headers)

0 commit comments

Comments
 (0)