Skip to content

Instantly share code, notes, and snippets.

@4lexLammers
Last active June 30, 2020 11:11
Show Gist options
  • Save 4lexLammers/907b0ccd888d0c0c2367ebd3af88bf23 to your computer and use it in GitHub Desktop.
Save 4lexLammers/907b0ccd888d0c0c2367ebd3af88bf23 to your computer and use it in GitHub Desktop.
Tweepy Project for Collecting Tweets
#Libraries
import tweepy
from textblob import TextBlob
import time
from prettytable import PrettyTable
from datetime import datetime
import sqlite3
###############################################################################
"""
Search hashtag
"""
targettwitterhashtag = '#EconTwitter'
"""
Create SQL Table Database
"""
conn = sqlite3.connect(''+targettwitterhashtag+'.db')
c = conn.cursor()
try:
c.execute('CREATE TABLE '+targettwitterhashtag[1:]+' (n int, dbtweettext text, dbtweetlen int, dbtweetid int PRIMARY KEY, dbtweetdate date, dbtweetsource text, dbtweetlikes int, dbtweetretweets int, polarity real)')
except:
pass
###############################################################################
"""
Authentification
"""
consumer_key =
consumer_secret =
access_token =
access_token_secret =
#Consumer key and consumer secret to Tweepy's user authentication handler
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
#Access token and access secret to Tweepy's user authentication handler
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
#Getting Geo ID for Germany
places = api.geo_search(query="GERMANY", granularity="country")
#Copy Germany id: fdcd221ac44fa326
place_id = places[0].id
print('Germany id is: ',place_id)
#Switching to application authentication
auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
#Setting up new api wrapper, using authentication only
api = tweepy.API(auth, wait_on_rate_limit=True,wait_on_rate_limit_notify=True)
###############################################################################
x = PrettyTable()
x.field_names = ["Count", "Text", "Length", "ID", "Date", "Source", "Likes", "Retweets", "Polarity"]
n = 1
for tweet in tweepy.Cursor(api.search, tweet_mode='extended', q='#EconTwitter -filter:retweets', lang="en").items(10):
print(tweet.full_text)
tweetid = tweet.id
tweetdate = tweet.created_at
source = tweet.source
likes = tweet.favorite_count
retweets = tweet.retweet_count
tweettext = tweet.full_text
length = len(tweettext)
polarity = round(TextBlob(tweettext).sentiment.polarity,4)
x.add_row([n, tweettext, len, tweetid, tweetdate, source, likes, retweets, polarity])
c.execute("insert into "+targettwitterhashtag[1:]+" values (?,?,?,?,?,?,?,?,?)", (n, tweettext, length, tweetid, tweetdate, source, likes, retweets, polarity))
n+=1
print(x)
conn.commit()
c.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment