-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstreaming.py
More file actions
71 lines (55 loc) · 2.04 KB
/
streaming.py
File metadata and controls
71 lines (55 loc) · 2.04 KB
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
#!/bin/sh
# streaming.py
#
#
# Created by Amber Shen on 15/6/3.
#
import tweepy
import csv
import random
import sys
#setting up Twitter API
consumer_key = ' '
consumer_secret = ' '
access_key = ' '
access_secret = ' '
#method to get a user's tweets
#def get_tweets(id_str):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
def get_tweets(username):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
number_of_tweets = 500
#get tweets
tweets = api.user_timeline(screen_name = username,count = number_of_tweets)
#create array of tweet information: username, tweet id, date/time, text
tweets_for_csv = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in tweets]
#write to a new csv file from the array of tweets
print "writing to {0}_tweets.csv".format(username)
with open("{0}_tweets.csv".format(username) , 'w+') as file:
writer = csv.writer(file, delimiter='|')
writer.writerows(tweets_for_csv)
def followee_tweets(username):
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
#get followee ids
friends = api.friends_ids(username)
for friend in friends:
number_of_tweets = 180
tweets = api.user_timeline(id = friend, count = number_of_tweets)
tweets_for_csv = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")] for tweet in tweets]
print"writing to {0}_followee_tweets.csv".format(username)
with open("{0}_followee_tweets.csv".format(username), 'w+') as file:
writer = csv.writer(file, delimiter = '|')
writer.writerows(tweets_for_csv)
if __name__ == '__main__':
#get tweets for username passed at command line
if len(sys.argv) == 2:
get_tweets(sys.argv[1])
followee_tweets(sys.argv[1])
else:
print "Error: enter one username"