Skip to content

Instantly share code, notes, and snippets.

@febriliankr
Created April 8, 2021 09:22
Show Gist options
  • Save febriliankr/85eb6e8dc56882820297516245dd06d7 to your computer and use it in GitHub Desktop.
Save febriliankr/85eb6e8dc56882820297516245dd06d7 to your computer and use it in GitHub Desktop.
import tweepy
import csv
import pandas as pd
API_KEY = "xxapikeyandaxx"
API_KEY_SECRET = "xxapikeysecretandaxx"
ACCESS_TOKEN = "xxaccesstokenandaxx"
ACCESS_TOKEN_SECRET = "xxaccesstokensecretandaxx"
# Authenticate to Twitter
auth = tweepy.OAuthHandler(API_KEY, API_KEY_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
try:
print('\033[93m' + "[∙] Authenticating..." + '\033[0m')
api.verify_credentials()
print('\033[92m' + "[✔️] Authentication success!" + '\033[0m')
except:
print("Error during authentication")
# Gather Input
search_keyword = input("Search keyword [type any string]: ")
search_limit = int(input("Search limit amount [type any number]: "))
print("searching: '" + search_keyword +
" (limit: " + str(search_limit) + ")'")
csvFile = open(search_keyword+'.csv', 'a+', newline='', encoding="utf-8")
csvWriter = csv.writer(csvFile)
created = []
id_twitter = []
username = []
text = []
for tweet in tweepy.Cursor(api.search, q=search_keyword, count=300, lang='id', result_type="latest").items(search_limit):
print(tweet.created_at, tweet.id, tweet.user.name, tweet.text)
created.append(tweet.created_at)
id_twitter.append(tweet.id)
username.append(tweet.user.name)
text.append(tweet.text)
tweets = [tweet.created_at, tweet.id,
tweet.user.name, tweet.text]
csvWriter.writerow(tweets)
dictTweets = {"timestamp": created, "id_twitter": id_twitter,
"username": username, "text": text}
dataFrame = pd.DataFrame(dictTweets, columns=[
"timestamp", "id_twitter", "username", "text"])
dataFrame
openFile = open(search_keyword + ".csv", "r")
print(openFile.read())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment