-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwitterMining.py
212 lines (131 loc) · 4.7 KB
/
TwitterMining.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#--------------------------------------Tweets------------------------
import tweepy
# these key is generated by Twiiter App
consumer_key = "---------------------"
consumer_secret = "--------"
access_key = "-------------"
access_secret = "----------"
# Function to extract tweets
def get_tweets(username):
# Authorization to consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# Access to user's access key and access secret
auth.set_access_token(access_key, access_secret)
# Calling api
api = tweepy.API(auth)
# 200 tweets to be extracted
number_of_tweets=200
tweets = api.user_timeline(screen_name=username)
# Empty Array
tmp=[]
# create array of tweet information: username,
# tweet id, date/time, text
tweets_for_csv = [tweet.text for tweet in tweets] # CSV file created
for j in tweets_for_csv:
# Appending tweets to the empty array tmp
tmp.append(j)
# Printing the tweets
print(tmp)
# Driver code
if __name__ == '__main__':
# Here goes the twitter handle for the user
# whose tweets are to be extracted.
get_tweets("TSovran")
# In[3]:
# ----------------------------No of tweets So far----------------
#------------------------------Account Date---------------
# Authorization to consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# Access to user's access key and access secret
auth.set_access_token(access_key, access_secret)
# Calling api
api = tweepy.API(auth)
# the ID of the user0 (my id)
#id = 909474641885855745
# fetching the user
#user = api.get_user(id)
# -------or---------------
# the screen name of the user
screen_name = "TSovran"
# fetching the user
user = api.get_user(screen_name)
# fetching the statuses_count attribute
statuses_count = user.statuses_count
print("The number of statuses the user has posted are : " + str(statuses_count))
#time twitter account is creted
created_at = user.created_at
print("Twitter Account was created at : " + str(created_at))
# In[4]:
# ---------------------------Date when Tweet(Given Status ID) is made---------------
# the ID of the status
id = 1276210447511764993
# fetching the status
user = api.get_status(id)
# fetching the created_at attribute
created_at = user.created_at
print("The status was created at : " + str(created_at))
# In[5]:
#---------------------Check Whether i like the tweet of anyone whose is is give--------------
# the ID of the status
id = 1276210447511764993
# fetching the status
status = api.get_status(id)
# fetching the favorited attribute
favorited = status.favorited
if favorited == True:
print("The authenticated user has liked the tweet.")
else:
print("The authenticated user has not liked the tweet.")
# In[36]:
#----------------------Accessing the Tweet-------------------------
# the ID of the status
id = 1276221606335897600
# fetching the status
status = api.get_status(id)
# fetching the text attribute
text = status.text
print("The text of the status is : \n\n" + text)
# In[6]:
#----------------------Tweet Source(Android,Iphone,Website) and language-------------------------
# the ID of the status
id = 1276221606335897600
# fetching the status
status = api.get_status(id)
# fetching the source attribute
source = status.source
print("The source of the status is : " + source)
lang = status.lang
print("The language of the status is : " + lang)
#-----------------------------X-----------------X------------------
id = 1273220141417816064
status = api.get_status(id)
lang = status.lang
print("The language of the status is : " + lang)
# In[37]:
# fetching the trends
trends = api.trends_available()
print("Some of the locations are : ")
for i in range(0, 100, 10):
print("Place : " + trends[i]['name'] +
", Country : " + trends[i]['country'])
# In[7]:
#--------------------------------Trending Tweets---------------------
# Access to user's access key and access secret
auth.set_access_token(access_key, access_secret)
# Calling api
api = tweepy.API(auth)
trends1 = api.trends_place(1) # from the end of your code
# trends1 is a list with only one element in it, which is a
# dict which we'll put in data.
data = trends1[0]
# grab the trends
trends = data['trends']
# grab the name from each trend
names = [trend['name'] for trend in trends]
# put all the names together with a ' ' separating them
trendsName = ' '.join(names)
print(trendsName)
# In[8]:
public_tweet=api.search('cricket')
for tweet in public_tweet:
print(tweet.text)