-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_tweets.py
executable file
·63 lines (57 loc) · 2.28 KB
/
get_tweets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/python
import argparse
import tweepy
import jsonpickle
import csv
import os
consumer_key = '32qgMUGN2OqjQpB0JSeroN7Jv'
consumer_secret = 'PQF5DGBWSIzM0MmYuOZQbv4ylkNaiAPqDa3qnhd4ZkSztUaR9g'
parser = argparse.ArgumentParser(description='Extract tweets from user')
parser.add_argument('--username', '-u', default='TheTweetOfGod',
help='username of tweet account to extract its tweets')
args = parser.parse_args()
searchQuery = '@' + args.username
maxTweets = 400
auth = tweepy.AppAuthHandler(consumer_key, consumer_secret)
auth.secure = True
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
retweet_filter='-filter:retweets'
max_id = -1L
q=searchQuery+retweet_filter
tweetsPerQry = 100
fName = 'tweets.txt'
sinceId = None
tweetCount = 0
print("Downloading max {0} tweets".format(maxTweets))
with open(fName, 'w') as f:
while tweetCount < maxTweets:
try:
if (max_id <= 0):
if (not sinceId):
new_tweets = api.search(q=searchQuery, count=tweetsPerQry)
else:
new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
since_id=sinceId)
else:
if (not sinceId):
new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
max_id=str(max_id - 1))
else:
new_tweets = api.search(q=searchQuery, count=tweetsPerQry,
max_id=str(max_id - 1),
since_id=sinceId)
if not new_tweets:
print("No more tweets found")
break
for tweet in new_tweets:
if (not tweet._json['retweeted']) and ('RT @' not in tweet._json['text']):
f.write(jsonpickle.encode(tweet._json, unpicklable=False) +
'\n')
tweetCount += 1
print("Downloaded {0} tweets".format(tweetCount))
max_id = new_tweets[-1].id
except tweepy.TweepError as e:
# Just exit if any error
print("some error : " + str(e))
break
print ("Downloaded {0} tweets, Saved to {1}".format(tweetCount, fName))