-
Notifications
You must be signed in to change notification settings - Fork 3
/
user_tweet_history.py
60 lines (46 loc) · 1.53 KB
/
user_tweet_history.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
# importing all the libraries
import tweepy
import pandas as pd
import sys # to take arguments from the terinla
from login_function import login
import json
# declaring the credentials
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''
# handling the authentication process of the twitter api
api = login(consumer_key, consumer_secret, access_token, access_secret)
DATA_DIR = str(sys.argv[1]) # add the location for the source file
OUT_DIR_JSON = str(sys.argv[2]) # add the location for the output file in JSON file
# get the user_ids as a python list
def get_id(filepath):
df = pd.read_json(filepath, lines=True)
return df.id.to_list()
user_ids = get_id(DATA_DIR)
# retrieving the user tweets and storing them in a jsonstream file
# dyanmically doing it, and using file handling for it
for i in user_ids:
try:
data = {}
timeline = tweepy.Cursor(api.user_timeline, user_id=i, ).items()
data[i] = []
for status in timeline:
obj = status._json
tweet = {}
tweet['tweet_id'] = obj['id']
tweet['created_at'] = obj['created_at']
tweet['text'] = obj['text']
tweet['retweeted'] = obj['retweeted']
tweet['retweet_count'] = obj['retweet_count']
tweet['hashtags'] = len(obj['entities']['hashtags'] )
tweet['user_mentions'] = len(obj['entities']['user_mentions'])
data[i].append(tweet)
j = json.dumps(data)
f = open(OUT_DIR_JSON, 'a')
f.write(j)
f.close()
print("no. of tweets of the user",len(data[i]))
print('\n')
except tweepy.error.TweepError:
print('error')