Home Forums Programming General Advice please - how to train up on API integrations RE: Advice please - how to train up on API integrations

  • Here is a basic example of how easy it is to use Python to look at tweet feed.

    This link walks you through Python, the Twitter API and even how to read/convert JSON.

    Twitter API Tutorial

    # Import the necessary package to process data in JSON format

    try:

    import json

    except ImportError:

    import simplejson as json

    # Import the necessary methods from "twitter" library

    from twitter import Twitter, OAuth, TwitterHTTPError, TwitterStream

    # Variables that contains the user credentials to access Twitter API

    ACCESS_TOKEN = 'YOUR ACCESS TOKEN"'

    ACCESS_SECRET = 'YOUR ACCESS TOKEN SECRET'

    CONSUMER_KEY = 'YOUR API KEY'

    CONSUMER_SECRET = 'ENTER YOUR API SECRET'

    oauth = OAuth(ACCESS_TOKEN, ACCESS_SECRET, CONSUMER_KEY, CONSUMER_SECRET)

    # Initiate the connection to Twitter Streaming API

    twitter_stream = TwitterStream(auth=oauth)

    # Get a sample of the public data following through Twitter

    iterator = twitter_stream.statuses.sample()

    # Print each tweet in the stream to the screen

    # Here we set it to stop after getting 1000 tweets.

    # You don't have to set it to stop, but can continue running

    # the Twitter API to collect data for days or even longer.

    tweet_count = 1000

    for tweet in iterator:

    tweet_count -= 1

    # Twitter Python Tool wraps the data returned by Twitter

    # as a TwitterDictResponse object.

    # We convert it back to the JSON format to print/score

    print json.dumps(tweet)

    # The command below will do pretty printing for JSON data, try it out

    # print json.dumps(tweet, indent=4)

    if tweet_count <= 0:

    break

    In this example, it's basically looping through the first 1000 Twitter statuses of I think the global feed. It then prints the result via the JSON module to screen. In your case, you may want to instead convert the data from JSON to CSV and then save it as a flat file for SQL Server to ingest with an SSIS package.

    Pretty simple, easy and all in one TwitterAPI.py file.