-
Notifications
You must be signed in to change notification settings - Fork 1
/
Twitter.py
149 lines (105 loc) · 5.66 KB
/
Twitter.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
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
import wget
import requests
import sys
import getopt
import os
import pandas as pd
import numpy as np
ACCESS_TOKEN = '2587900573-WV2E2gGjgLd990tQiciw8Ze34Z9mzSa3ARW6Ra8'
ACCESS_TOKEN_SECRET = 'NN0gJ0RF5KmWgj3De3T6mNWecV7FCbfdxJocp0p6SIS1U'
CONSUMER_KEY = 'y03mYHkiCCCmkAJxcFIIAQzv2'
CONSUMER_SECRET = 'Zfo3nqNAFkgsYp541sCSX12O6EqdcQTUuoAcgdSzGS9KQ1FToK'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
class TweetListener(StreamListener):
def on_data(self, data):
print (data)
return True
def on_error(self, status):
print (status)
def info(username):
api = tweepy.API(auth)
twitterStream = Stream(auth,TweetListener())
user = api.get_user(username)
path="{}".format(username)
os.mkdir(path)
file1 = open('{}/{}.txt'.format(username,username),"w")
file1.write(str(u"Name:{} \n".format(user.name).encode('utf-8')))
file1.write(str(u"Location:{} \n".format(user.location).encode('utf-8')))
file1.write(str(u"Description:{} \n".format(user.description).encode('utf-8')))
file1.write(str(u"Created on: {} \n".format(user.created_at).encode('utf-8')))
file1.write(str(u"Url Associated: {} \n".format(user.url).encode('utf-8')))
file1.write(str(u"Profile Image: {} \n".format(user.profile_image_url).encode('utf-8')))
file1.write(str(u"Total Followers: {} \n".format(user.followers_count).encode('utf-8')))
file1.write(str(u"Status count: {} \n".format(user.statuses_count).encode('utf-8')))
file1.write(str(u"Total following:{} \n".format(user.friends_count).encode('utf-8')))
file1.close()
print(u"Name:{} \n".format(user.name).encode('utf-8'))
print(u"Location:{} ".format(user.location).encode('utf-8'))
print(u"Description:{} ".format(user.description).encode('utf-8'))
print(u"Created on: {} ".format(user.created_at).encode('utf-8'))
print(u"Url Associated: {} ".format(user.url).encode('utf-8'))
print(u"Profile Image: {} ".format(user.profile_image_url).encode('utf-8'))
print(u"Total Followers: {} ".format(user.followers_count).encode('utf-8'))
print(u"Status count: {} ".format(user.statuses_count).encode('utf-8'))
print(u"Total following:{} ".format(user.friends_count).encode('utf-8'))
target_path='{}/{}.jpg'.format(username,username)
profile_pic=wget.download(user.profile_image_url,target_path)
print("\nCreated {}.txt and profile pic of {}".format(username, username))
def twiiter_media(username):
api = tweepy.API(auth)
tweets = api.user_timeline(screen_name=username,
count=200, include_rts=False,
exclude_replies=False)
id=[]
date=[]
tweet_text=[]
image_url=[]
count = 1
for status in tweets:
media = status.entities.get('media', [])
user_mention = status.entities.get('user_mentions', [])
if(len(media) > 0):
print("--------------------------------------------------###################################################------------------------------------")
print("{}.".format(count))
count=count+1
id.append(status.id)
date.append(status.created_at)
tweet_text.append(status.text.encode("utf-8"))
image_url.append(media[0]['media_url'])
print("Id : {}".format(status.id))
print("Date And Time: {}".format(status.created_at))
print(u"Tweet is : {} ".format(status.text).encode("utf-8"))
print("Image Url : {}".format(media[0]['media_url']))
img_url = media[0]['media_url']
target_path='{}/{}.jpg'.format(username,status.id)
profile_pic=wget.download(img_url,target_path)
for i in range(len(user_mention)):
print("Mention Username are : {}".format(user_mention[i]['screen_name']))
print("\n")
def tweets_to_data_frame(username):
api = tweepy.API(auth)
tweets = api.user_timeline(screen_name=username,
count=20 )
df = pd.DataFrame(data=[tweet.id for tweet in tweets], columns=['id'])
df['date'] = np.array([tweet.created_at for tweet in tweets])
df['len'] = np.array([len(tweet.text) for tweet in tweets])
df['source'] = np.array([tweet.source for tweet in tweets])
df['likes'] = np.array([tweet.favorite_count for tweet in tweets])
df['Tweets'] = np.array([tweet.text.encode("utf-8").decode("utf-8") for tweet in tweets])
return df
if __name__ == '__main__':
def main(username):
info(username)
print("#------------------------ Media Files-----------------------------# ")
twiiter_media(username)
print("#----------------------- Tweet Records--------------------------#")
df = tweets_to_data_frame(username)
print(df)
path_for_csv='{}/{}.csv'.format(username,username)
df.to_csv(path_for_csv, index=False,encoding='utf-8')