Skip to content

Instantly share code, notes, and snippets.

@wgcv
Last active May 29, 2019 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wgcv/342807bbf92438c5b2d7c92c034605dd to your computer and use it in GitHub Desktop.
Save wgcv/342807bbf92438c5b2d7c92c034605dd to your computer and use it in GitHub Desktop.
Get Tweets
import csv
import twitter
import datetime
import time
api = twitter.Api(
consumer_key='',
consumer_secret='',
access_token_key='',
access_token_secret='',
tweet_mode="extended")
count = 0
old_id = None
date = datetime.datetime.now()
with open('tweets.csv', 'a+', encoding='utf-8') as csv_file:
csv_writer = csv.writer(csv_file)
for i in range(15):
for j in range(20):
print(date.strftime("%Y-%m-%d"))
print(old_id)
a = api.GetSearch(term="-filter:retweets", max_id=old_id, until=date.strftime("%Y-%m-%d"), geocode="-1.476158,-78.474710,400km", count=100, lang="es", return_json=True, result_type="recent")
for tweet in a['statuses']:
text = tweet['full_text'].replace('\n', ' ').replace('\r', '')
print(tweet['user']['screen_name'] + ": " + text)
reply_txt = ''
if(tweet['in_reply_to_status_id']):
try:
reply = api.GetStatus(tweet['in_reply_to_status_id']).AsDict()
print("Reply: " + reply['full_text'])
reply_txt = reply['full_text'].replace('\n', ' ').replace('\r', '')
except twitter.error.TwitterError as e:
print("Reply: Private tweet")
reply_txt = "Reply: Private tweet"
print("---")
row = [tweet["id"], tweet["created_at"], tweet["user"]["screen_name"],
reply_txt, text, 'NC'
]
csv_writer.writerow(row)
old_id = tweet["id"] - 1
count += 1
"""
if (count/100 > 160 ):
print("Waiting 15 mins from: " + datetime.datetime.now())
time.sleep(15*60)
"""
date = date - datetime.timedelta(1)
import csv
import twitter
import datetime
import time
import json
api = twitter.Api(
consumer_key='',
consumer_secret='',
access_token_key='',
access_token_secret='',
tweet_mode="extended")
count = 0
old_id = None
date = datetime.datetime.now()
with open('tweets_by_url.csv', 'a+', encoding='utf-8') as csv_file:
csv_writer = csv.writer(csv_file)
with open('list_tweets.csv', 'r') as csvFile:
reader = csv.reader(csvFile)
for row in reader:
id_of_tweet = row[0].split('/')[-1]
tweet = api.GetStatus(status_id=id_of_tweet)._json
print(tweet)
text = tweet['full_text'].replace('\n', ' ').replace('\r', '')
print(tweet['user']['screen_name'] + ": " + text)
reply_txt = ''
if(tweet['in_reply_to_status_id']):
try:
reply = api.GetStatus(tweet['in_reply_to_status_id']).AsDict()
print("Reply: " + reply['full_text'])
reply_txt = reply['full_text'].replace('\n', ' ').replace('\r', '')
except twitter.error.TwitterError as e:
print("Reply: Private tweet")
reply_txt = "Reply: Private tweet"
print("---")
tweet_row = [tweet["id"], tweet["created_at"], tweet["user"]["screen_name"],
reply_txt, text, 'M'
]
csv_writer.writerow(tweet_row)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment