Skip to content

Instantly share code, notes, and snippets.

@geraldoam
Created September 4, 2022 23:11
Show Gist options
  • Save geraldoam/4b42557f5544f095722d358ac139cc74 to your computer and use it in GitHub Desktop.
Save geraldoam/4b42557f5544f095722d358ac139cc74 to your computer and use it in GitHub Desktop.
import requests
import json
import openpyxl
import pandas as pd
# Token da API do Twitter - Bearer Token
BEARER_TOKEN = ""
query = "" # Coloca aqui a palavra/frase que quer procurar.
def search_tweets(query, bearer_token=BEARER_TOKEN):
headers = {"Authorization": "Bearer {}".format(bearer_token)}
tweet_fields = "tweet.fields=text,author_id,created_at,id"
url_tweets = "https://api.twitter.com/2/tweets/search/recent?query={}&{}&max_results=100".format(
query, tweet_fields
)
response_tweets = requests.request("GET", url_tweets, headers=headers)
if response_tweets.status_code != 200:
raise Exception(response_tweets.status_code, response_tweets.text)
return response_tweets.json()
def clean_tweets_json(json_tweets):
json_tweets = search_tweets(
query=query, bearer_token=BEARER_TOKEN)
len_json = len(json_tweets["data"])
author_id = []
created_at = []
text = []
for i in range(len_json):
text.append(json_tweets["data"][i]["text"])
author_id.append(json_tweets["data"][i]["author_id"])
created_at.append(json_tweets["data"][i]["created_at"])
return author_id, created_at, text, len_json
def search_user(users_ids, bearer_token=BEARER_TOKEN):
headers = {"Authorization": "Bearer {}".format(bearer_token)}
user_fields = "user.fields=description,name,url,username"
url_users = "https://api.twitter.com/2/users?ids={}&{}".format(
users_ids, user_fields
)
response_users = requests.request("GET", url_users, headers=headers)
if response_users.status_code != 200:
raise Exception(response_users.status_code, response_users.text)
return response_users.json()
def get_users_infos(author_id, len_json):
comma_separeted = ",".join(author_id)
json_users = search_user(users_ids=comma_separeted,
bearer_token=BEARER_TOKEN)
username = []
name = []
url = []
description = []
for i in range(len_json):
username.append(json_users["data"][i]["username"])
name.append(json_users["data"][i]["name"])
url.append(json_users["data"][i]["url"])
description.append(json_users["data"][i]["description"])
return username, name, url, description
def data_to_excel(name, username, url, description, text, created_at):
df = pd.DataFrame({"Autor": [], "@": [], "Tweet": [],
"Url": [], "Descrição": [], "Criado em": []})
df["Autor"] = name
df["@"] = username
df["Url"] = url
df["Descrição"] = description
df["Tweet"] = text
df["Criado em"] = created_at
df.to_excel("output.xlsx")
def main():
json_tweets = search_tweets(query=query)
author_id, created_at, text, len_json = clean_tweets_json(
json_tweets=json_tweets)
username, name, url, description = get_users_infos(
author_id=author_id, len_json=len_json)
data_to_excel(name, username, url, description, text, created_at)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment