Ticket #9003: 0001-Fixed-twitter-example-to-work-with-twitter-API-versi.patch

File 0001-Fixed-twitter-example-to-work-with-twitter-API-versi.patch, 4.1 KB (added by sambuddhabasu, 4 years ago)
  • docs/tutorials/to_russia_with_love.rst

    From a6b4ce055f2191afa926222b9f0059fdcdcd675a Mon Sep 17 00:00:00 2001
    From: Sambuddha Basu <sambuddhabasu1@gmail.com>
    Date: Sat, 18 Apr 2015 12:17:20 +0530
    Subject: [PATCH] Fixed twitter example to work with twitter API version 1.1
    
    ---
     docs/tutorials/to_russia_with_love.rst | 66 +++++++++++++++++++++++++++++++---
     1 file changed, 62 insertions(+), 4 deletions(-)
    
    diff --git a/docs/tutorials/to_russia_with_love.rst b/docs/tutorials/to_russia_with_love.rst
    index 75f9735..7d91f4b 100644
    a b Besides SocksiPy, you can also use `PycURL <http://pycurl.sourceforge.net/>`_ to 
    121121Reading Twitter
    122122---------------
    123123
    124 Now lets do somthing a little more interesting, and read a Twitter feed over Tor. This can be easily done `using thier API <https://dev.twitter.com/docs/api/1/get/statuses/user_timeline>`_ (**warning: Twitter has deprecated the API that this example uses,** :trac:`9003`)...
     124Now lets do somthing a little more interesting, and read a Twitter feed over Tor. This can be easily done `using thier API <https://dev.twitter.com/rest/reference/get/statuses/user_timeline>`_
    125125
    126126::
    127127
    128128  import json
    129129  import socket
    130130  import urllib
     131  import urllib2
     132  import time
     133  import binascii
     134  import hmac
     135  from hashlib import sha1, md5
    131136
    132137  import socks  # SockiPy module
    133138  import stem.process
    134139
    135140  SOCKS_PORT = 7000
    136   TWITTER_API_URL = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=%s&count=%i&include_rts=1"
     141  KEY_DICT = dict()
     142  TWITTER_API_URL = "https://api.twitter.com/1.1/statuses/user_timeline.json"
     143  CONSUMER_KEY = ""
     144  CONSUMER_SECRET = ""
     145  ACCESS_TOKEN = ""
     146  ACCESS_TOKEN_SECRET = ""
     147  HTTP_METHOD = "GET"
     148  OAUTH_VERSION = "1.0"
    137149
    138150  socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
    139151  socket.socket = socks.socksocket
    140152
    141153
     154  def init_key_dict():
     155    """
     156    Initializes KEY_DICT
     157    """
     158
     159    global KEY_DICT
     160    KEY_DICT['oauth_consumer_key'] = urllib.quote(CONSUMER_KEY, '')
     161    KEY_DICT['oauth_nonce'] = urllib.quote(md5(str(time.time())).hexdigest(), '')
     162    KEY_DICT['oauth_signature_method'] = urllib.quote("HMAC-SHA1", '')
     163    KEY_DICT['oauth_timestamp'] = urllib.quote(str(int(time.time())), '')
     164    KEY_DICT['oauth_token'] = urllib.quote(ACCESS_TOKEN, '')
     165    KEY_DICT['oauth_version'] = urllib.quote(OAUTH_VERSION, '')
     166
     167  def get_signature(values):
     168    """
     169    Generates KEY_DICT['oauth_signature']
     170    """
     171    for value in values:
     172      KEY_DICT[value] = urllib.quote(values[value], '')
     173    fin_key = ""
     174    for key in sorted(KEY_DICT.keys()):
     175      fin_key += key + "=" + KEY_DICT[key] + "&"
     176    fin_key =  fin_key[:-1]
     177    fin_key = HTTP_METHOD + "&" + urllib.quote(TWITTER_API_URL, '') + "&" + urllib.quote(fin_key, '')
     178    key = urllib.quote(CONSUMER_SECRET, '') + "&" + urllib.quote(ACCESS_TOKEN_SECRET, '')
     179    hashed = hmac.new(key, fin_key, sha1)
     180    fin_key = binascii.b2a_base64(hashed.digest())[:-1]
     181    KEY_DICT['oauth_signature'] = urllib.quote(fin_key, '')
     182
     183  def get_header_string():
     184    """
     185    Returns the header string
     186    """
     187    ret = "OAuth "
     188    key_list =['oauth_consumer_key', 'oauth_nonce', 'oauth_signature', 'oauth_signature_method', 'oauth_timestamp', 'oauth_token', 'oauth_version']
     189    for key in key_list:
     190      ret = ret + key + "=\"" + KEY_DICT[key] + "\", "
     191    ret = ret[:-2]
     192    return ret
     193
    142194  def poll_twitter_feed(user_id, tweet_count):
    143195    """
    144196    Polls Twitter for the tweets from a given user.
    145197    """
    146198
    147     api_url = TWITTER_API_URL % (user_id, tweet_count)
     199    init_key_dict()
     200    values = {'screen_name': user_id, 'count': str(tweet_count), 'include_rts': '1'}
     201    api_url = TWITTER_API_URL
     202    get_signature(values)
     203    headers = {'Authorization': get_header_string()}
     204    data = urllib.urlencode(values)
     205    api_request = urllib2.Request(api_url + "?" + data, headers= headers)
    148206
    149207    try:
    150       api_response = urllib.urlopen(api_url).read()
     208      api_response = urllib2.urlopen(api_request).read()
    151209    except:
    152210      raise IOError("Unable to reach %s" % api_url)
    153211