You are on page 1of 9

MINI PROJECT

TITLE OF PROJECT:
Make a Twitter Bot in Python

REGISTER NUMBER:
192210322

NAME:
S.Nithish

CONSULTANT PROFESSOR

[DR.S.PANDIARAJ]

1
TABLE OF

1
CONTENT
S Abstract 4

2
Introduction 5

3
About Python 5

4
6
Flow Chart

5
Code 7-10

6 11
Output Screen

7 12
Conclusion

2
ABSTRACT

Twitter bots are everywhere on Twitter, making us


laugh, annoying us, and occasionally spitting
out profound truths. These bots
are made by artists and activists, scholars and
spammers. And as it turns out, building a Twitter bot
is a fun and productive way to
introduce yourself to basic programming in Python.
We have provided five sample scripts that work with
pretty minimal set-up,
along with instructions and suggestions for
customizing the scripts.
This tutorial is based on the LACUNY Emerging
Technologies Committee’s “Build Your Own Twitter
Bot” day in December
2015, which was billed as a gentle introduction to
programming in Python. Below, we will expand on
some of our insights and
examples from this workshop.

STUDENT
SIGNATURE

[STUDENT NAME]

3
INRODUCTION:
-Twitter bot in Python using Tweepy, a Python library for accessing the
official Twitter API.

You will be creating a Reply to mentions bot, which will send a reply to
everyone's tweet who has mentioned it with a specific keyword.

The reply will be in the form of an image that you will generate and put some
text over it. This text will be a quote that you will fetch from a third-party API.

ABOUT PYTHON :
-> Python is a high-level, general-purpose, and interpreted programming language used
in various sectors including machine learning, artificial intelligence, data analysis, web
development, and many more.
-> Python is known for its ease of use,powerful standard library, and dynamic semantics. It
also has a large community of developers who keep on contributing towards its growth.
-> The major focus behind creating it is making it easier for developers to read and understand,
also reducing thelines of code.
-> Python has plenty of features that make it the most demanding and more popular. Let’s
read about a few of the best features that Python has:

• Easy to read and understand


• Interpreted language
• Object-oriented programming language
• Free and open-source
• Versatile and Extensible
• Multi-platform
• Hundreds of libraries and frameworks.
• Flexible, supports GUI
• Dynamically typed

4
CODE
:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Original script (kept up to date): https://github.com/robincamille/bot-tutorial/blob/


master/mybot.py
# Twitter Bot Starter Kit: Bot 1
# This bot tweets three times, waiting 15 seconds between tweets.
# If you haven't changed credentials.py yet with your own Twitter
# account settings, this script will tweet at twitter.com/lacunybot
# Housekeeping: do not edit
import tweepy, time
from credentials import *
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)
# What the bot will tweet
tweetlist = ['Test tweet one!', 'Test tweet two!', 'Test tweet three!']
for line in tweetlist:
api.update_status(line)
print line
print '...'
time.sleep(15) # Sleep for 15 seconds
print "All done!”

5
Tweeting lines from a text file:
#!/usr/bin/env python

# -*- coding: utf-8 -*-

# Original script (kept up to date): https://github.com/robincamille/bot-tutorial/

blob/master/mybot2.py

# Twitter Bot Starter Kit: Bot 2

# This bot tweets a text file line by line,

waiting a # given period of time between

tweets.

# Download a Project Gutenberg "Plain Text UTF-

8" file, # open it in Notepad, remove junk at beginning,

# and replace all double-linebreaks with single

linebreaks. # Housekeeping: do not edit

import tweepy,

time from credentials

import *

auth = tweepy.OAuthHandler(CONSUMER_KEY,

CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN,

ACCESS_SECRET)

api = tweepy.API(auth)

# What the bot will tweet

filename = open('twain.txt','r')

tweettext = filename.readlines()

filename.close()

for line in tweettext[0:5]: #Will only write first 5 lines

api.update_status(line)

print line

time.sleep(15) # Sleep for 15 seconds


Using JSON for madlibs:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Original script (kept up to date): https://github.com/robincamille/bot-tutorial/blob/master/
mashup_madlib.py
# Mad Lib Maker!
# This script will generate mad-libs based off of a William Carlos
# Williams poem, 'The Red Wheelbarrow.' Each poem will then be
# tweeted by your bot account.
# Housekeeping: do not edit
import json, io, tweepy, time, urllib2
from random import randint
from credentials import *
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)
# Housekeeping: opening JSON files
# Find more item lists at https://github.com/dariusk/corpora/tree/master/data
# ***Click "Raw" button, copy URL***
list1file = urllib2.urlopen('https://raw.githubusercontent.com/dariusk/corpora/master/data/objects/
objects.json')
list1read = list1file.read()
list2file = urllib2.urlopen('https://raw.githubusercontent.com/dariusk/corpora/master/data/foods/
menuItems.json')
list2read = list2file.read()
list3file = urllib2.urlopen('https://raw.githubusercontent.com/dariusk/corpora/master/data/
humans/occupations.json')
list3read = list3file.read()
# Create Python-readable lists of items in JSON files
list1 = json.loads(list1read)['objects'] # Change 'objects' to the title of the list
list2 = json.loads(list2read)['menuItems']
list3 = json.loads(list3read)['occupations']
# Repeatable poem-generator
poemlist = []
counter = 0
# As written below, this script will generate 2 poems, and tweet them all 15 seconds apart.
while counter < 2: # Change 2 to however many poems you want to produce

7
Picks random numbers
list1num = randint(0, len(list1) - 1)
list2num = randint(0, len(list2) -
1) list3num = randint(0, len(list3)
- 1)
# Chooses random items from each list using random numbers
first = list1[list1num] # Syntax: list[number]
second = list2[list2num].lower()
third = list3[list3num].lower() + 's'
# Fills in the blanks of the poem
poem = 'so much depends\nupon\n\na\n%s\n\nglazed with\n%s\n\nbeside
the\n%s\n\n' \
% (first, second, third)
print poem
poemlist.append(poem) # Adds poem to long list of poems
counter = counter + 1
# Line up tweets for bot
for line in poemlist:
api.update_status(line)
#print line
time.sleep(15) # Sleep for 15 seconds
print '[All done!]’

8
CONCLUSION:
In this tutorial, you went through the complete process of developing and
deploying a Twitter bot in Python.

You also learned about Tweepy, how to sign up as a Twitter developer to


use its API, use Tweepy to invoke the Twitter API, and configure an AWS Elastic
Beanstalk environment to deploy your Python application.

You might also like