Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Visual Studio Code – Connect to Twitter with Python

0.00/5 (No votes)
12 Dec 2016 1  
How to use Visual Studio Code to edit and run Python code and access Twitter API

Visual Studio Code is a cross platform editor that supports multiple programming languages. Combining with Python, and its associated 3rd party packages that wrap Twitter’s API, we can easy connect to Twitter, read and use the data in just few lines of code.

Why Twitter?

Twitter is a popular social network where users can share short SMS-like messages called tweets. Users share thoughts, links and pictures on Twitter, journalists comment on live events. The list of different ways to use Twitter could be really long, and with 500 millions of tweets per day, there’s a lot of data to play with.

Using an excellent Python library – TwitterAPI as a minimal wrapper to native Twitter API calls, we can have a tool to extract the information we need.

Getting Started

At first, you will need to create a Twitter account and then a Twitter application. Once you have an account, you’ll have to go to their developer’s site, in the Application Management area and create a new application. After your application is created, you will need to get your API keys (or generate some) and also generate access tokens.

Download and install first Python 3 and then Visual Studio Code. Our example will work with minor changes also for Python 2.

Once you first open the VS Code, open the Extensions list and install Python extension.

Visual Studio Code - Python plugin

Download the code (or from GitHub - click to download) and extract the archive to a local folder. Then use the option File -> Open Folder in VSCode.

VSCode Python Twitter

To install the Twitter library, open the command prompt and run each of the next install commands. You could also run these lines within VSCode. It has integrated the command prompt, and you can easy access it by clicking on the menu item: View -> Integrated terminal.

pip install TwitterAPI
pip install configparse

Connecting to Twitter

To be able to do this, we will use the TwitterAPI library. With credentials from Twitter, we can access the Twitter data:

from TwitterAPI import TwitterAPI
connectTwitter = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret

Then, we can search and extract results in successive batches or pages. In this example, searching after ‘#webapistring, retrieving the results in English, in batches of 10 results.

twitterPage = TwitterRestPager(connectTwitter, 'search/tweets', \
                               {'q':'#webapi', 'count':10, 'lang': 'en'}

And in the end, we can iterate through search results:

for item in twitterPage.get_iterator():
   print('Tweet: ', item['text'], ' from ', item['user']['name']

To summarize, here is the full code allowing to connect, and retrieve the details from Twitter:

from TwitterAPI import TwitterAPI
from TwitterAPI import TwitterRestPager

configValues = configparser.RawConfigParser()
configValues.read(r'.\config.real.ini')

# connecting to Twitter - replace parameters with values
connectTwitter = TwitterAPI(consumer_key, consumer_secret,  
                            access_token_key, access_token_secret)

# search tweets
twitterPage = TwitterRestPager(connectTwitter, \
                               'search/tweets', \
                               {'q':'#webapi', 'count':10, 'lang': 'en'})

responseValues = []

# if connected successfully, retrieve paginated search results
for item in twitterPage.get_iterator():
    if 'user' in item and 'text' in item:
        responseValues.append({'username': item['user']['name'],
                               'screen_name': '@{}'.format(item['user']['screen_name']),
                               'profile_image': item['user']['profile_image_url_https'],
                               'profile_description': item['user']['description'],
                               'text': item['text']})
        print('Tweet ', len(responseValues), ' from  ', item['user']['name'])
    elif 'message' in item and item['code'] == 88:
        print('SUSPEND, RATE LIMIT EXCEEDED: %s\n' % item['message'])
        break

    # stop after first 50 tweets, to not exceed the limit too fast
    if len(responseValues) > 50:
        brea

Save the Results to a json File

Saving in json format brings many way to effectively use the data. This could be done using a simple command from json package.

import json
with open(r'.\twitter.txt', 'w') as outfile:
    json.dump(responseValues, outfile, indent=4

Load the Credentials from a Configuration File

Loading the connection credentials from a configuration file could be easily done in Python. Here is a sample configuration sample:

[TwitterSettings]
consumer_key = aaa
consumer_secret = bbb
access_token_key = ccc
access_token_secret = dd

To read any of the associated configuration keys, we run the next commands:

import configparser

configValues = configparser.RawConfigParser()
configValues.read(r'.\config.ini')

print configValues.get('TwitterSettings', 'consumer_key'

Calling the TwitterAPI constructor using the settings from a configuration file becomes:

connectTwitter = TwitterAPI(configValues.get('TwitterSettings', 'consumer_key'),
                            configValues.get('TwitterSettings', 'consumer_secret'),
                            configValues.get('TwitterSettings', 'access_token_key'),
                            configValues.get('TwitterSettings', 'access_token_secret')

Debug the Code with VS Code

We can run the above code and easily debug in VS Code. To be easier, download the code and open the VS Code debugger, selecting Python as language.

VSCode Python Debug

Python plug in for VSCode comes with linting support, which is source code, bug and quality checker, following the style recommended by Python style guide (PEP-8). Pylint quickly highlights the errors and it shows recommendations on how to write the code.

Or Run Directly

The other quick way to run the code, without debug, would be to use the combination CTRL-B. This uses the configuration file task.json, saved in folder .vscode and automatically runs the code file using Python.

{
    "command": "python",
    "showOutput": "always",
    "windows": {
        "command": "python.exe" 
    },
    "args": ["${file}"]

Final Results

Using the above code, we could retrieve tweets like the ones displayed below:

[
    {
        "profile_image": "https://pbs.twimg.com/profile_images/802976662187114496/gif5G5oY_normal.jpg",
        "text": "RT @techjunkiejh: Chart Widgets With Server Side Data In MVC 
         Using #AngularJS And #WebAPI https://t.co/MfW9zi431f #javascript https://t.co/\u2026",
        "screen_name": "@Phunick",
        "profile_description": "Software engineer | Consulting | Architect | 
         Developer Expert: MCP, MCSD, MSSQL,BigData, AngularJS,TypeScript,Nodejs,Reactjs.Telerik&...",
        "username": "Nick"
    }

Project Available in GitHub

This code could be access also from GitHub – https://github.com/fpetru/python-twitter-vscode.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here