Invading Twitter with Python 

Hi, I am Vipul Gupta

Available all over the web by vipulgupta2048

Student, Programmer, Writer, Open-Source Contributor,

Traveller, C&H Comics Collector

Tweet @vipulgupta2048

Resembling a mountain bluebird with a dash of hummingbird thrown in, the Twitter bird has a beak and body that point toward the sky in what Bowman called “the ultimate representation of freedom, hope, ​and limitless possibility.”

What is Twitter

for people living under a rock

Twitter API

Twitter dislikes spiders

Harnessing that API 

  • Using curl to request the API for information
  • Wreaking havoc with bots 
  • Getting your account banned
  • .. and a lot more. 

Bots, in general

  • Script/software fuelled by public API's 
  • Automates and reiterates tasks over and over. 
  • Imitates human patterns to avoid detection.

Examples

Examples

Examples

Examples

Examples

Examples

Examples

Examples

Let's build

  • Step 0 - The idea, basic requirements
  • Step 1 - Writing the script 
  • Step 2 - Testing & refactoring 
  • Step 3 - Deployment 

Idea & Basic Requirements

  • Ask yourself. Why?  
  • Explore your tools. What?
  • Data flowing through Public API's. How? 
  • Credentials for your Twitter Module. 

Writing the Script

Writing the Script

Installing Tweepy

# Install Tweepy for Python 
pip install tweepy

# Download Scripts from Vanilla Repository
wget https://github.com/vipulgupta2048/vanilla/raw/master/vanilla_bot/config.py
wget https://github.com/vipulgupta2048/vanilla/raw/master/vanilla_bot/vanilla.py

Oauth Dance - Not anymore

# Connecting/Authenticating our module with Twitter API
AUTH = tweepy.OAuthHandler(consumer_key, consumer_secret)
AUTH.set_access_token(access_token, access_token_secret)
API = tweepy.API(AUTH)

# Get access token
auth.get_access_token("verifier_value")

'''Doesn't get easier than this'''

Following the planet

for follower in tweepy.Cursor(api.followers).items():
    follower.follow()

This snippet will follow every follower of the authenticated user.

 

Following the planet

for follower in tweepy.Cursor(api.followers).items():
    follower.follow()

This snippet will follow every follower of the authenticated user.

 

Title Text

  • Bullet One
  • Bullet Two
  • Bullet Three

Testing and Refactoring

Limiting requests - #Respectthemaintainer
            

import time 

.
..
.

for tweet in tweepy.Cursor(API.search, q=searchterm):
    tweet.retweet()
    # limiting the number of requests sent
    time.sleep(15)
def limit_handled(cursor):
    while True:
        try:
            yield cursor.next()
        except tweepy.RateLimitError:
            time.sleep(15 * 60)

for follower in limit_handled(tweepy.Cursor(api.followers).items()):
    if follower.friends_count < 300:
        print follower.screen_name

Handling the rate limit

using cursors

def limit_handled(cursor):
    while True:
        try:
            yield cursor.next()
        except tweepy.RateLimitError:
            time.sleep(15 * 60)

for follower in limit_handled(tweepy.Cursor(api.followers).items()):
    if follower.friends_count < 300:
        print follower.screen_name

Handling the rate limit

using cursors

# Only iterate through the first 200 statuses
for status in tweepy.Cursor(api.user_timeline).items(200):
    process_status(status)

# Only iterate through the first 3 pages
for page in tweepy.Cursor(api.user_timeline).pages(3):
    process_page(page)

Pagination using cursors

Deployment

  • Heroku - recommended
  • Any hosting service...

What more can you do?

  • https://blog.twitter.com/developer/en_us.html
  • Have tons of fun, learn a new skillset 
  • Solve small real world problems
  • Run basic sentiment analysis projects using Twitter's data

The world is your oyster and the only thing that limits you is what you can see and imagine.

- Vipul Gupta

References

  • Embarrassing number of Wikipedia pages
  • Messy youtube history of how to give a great talk
  • Almost filled bookmark bar about conversation-fillers
  • and these....

References

  • Give a star to Tweepy on GitHub
  • Check out the PyCon India 2018 bot here
  • Blog of the amazing Twitter API team
  • Blog on freecodecamp, using Tkinter

Buy a lightsaber from someone but,

Rebuild your own with confidence

See what you like in other bots.

Keep what works, throw out what doesn't. 

 A bot is only as ethical as the person who programs it.

When you realize the

power of Twitter's API

Danke scheon !!

Questions? Any feedback

Did you like the talk? tweet about it. Retweets guaranteed !!

Vipul Gupta

Student, Programmer, Writer, Open-Source Contributor, Traveller, C&H Comics Collector

Tweet @vipulgupta2048

[PyDelhi] Invading Twitter with Python

By Vipul Gupta

[PyDelhi] Invading Twitter with Python

The zero to hero guide of making twitter bots using the Python Programming Language.

  • 819