· 6 years ago · Jan 15, 2019, 06:06 AM
1import twitter,json,csv
2
3CONSUMER_KEY =
4CONSUMER_SECRET =
5OAUTH_TOKEN =
6OAUTH_TOKEN_SECRET =
7
8auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
9 CONSUMER_KEY, CONSUMER_SECRET)
10
11twitter_api = twitter.Twitter(auth=auth)
12
13# setup a file to write to
14csvfile = open('iocisono10.csv', 'w')
15csvwriter = csv.writer(csvfile, delimiter='|')
16
17# heres a function that takes out characters that can break
18# our import into Excel and replaces them with spaces
19# it also does the unicode bit
20def getVal(val):
21 clean = ""
22 if val:
23 val = val.replace('|', ' ')
24 val = val.replace('\n', ' ')
25 val = val.replace('\r', ' ')
26 clean = val.encode('utf-8')
27 return clean
28
29retweet_indicator = "RT".encode('utf-8', 'ignore').decode('utf-8')
30
31q = "#facciamorete" # Comma-separated list of terms can go here
32print 'Filtering the public timeline for track="%s"' % (q,)
33
34twitter_stream = twitter.TwitterStream(auth=twitter_api.auth)
35
36stream = twitter_stream.statuses.filter(track=q)
37
38for tweet in stream:
39
40
41
42 if 'retweeted_status' in tweet: #checking whether the tweet is a retweet
43 if 'extended_tweet' in tweet['retweeted_status']:
44 user_text = tweet['retweeted_status']['user']
45 [x.encode('utf-8') for x in user_text] #adding some encoding
46 tweet_text = "RT " + tweet['retweeted_status']['user']['screen_name'] + " " + tweet['retweeted_status']['extended_tweet']['full_text']
47 elif 'truncated' in tweet and tweet['truncated'] == True:
48 tweet_text = tweet['extended_tweet']['full_text']
49
50 else:
51 tweet_text = tweet['text']
52
53
54 # write the values to file
55 csvwriter.writerow([
56 tweet['created_at'],
57 getVal(tweet['user']['screen_name']),
58 getVal(tweet_text),
59 getVal(tweet['user']['location']),
60 getVal(tweet['source']),
61 tweet['user']['statuses_count'],
62 tweet['user']['followers_count'],
63 tweet['user']['friends_count'],
64 tweet['user']['created_at'],
65 tweet['lang'],
66 tweet['geo'],
67 ])
68
69 # print something to the screen, mostly so we can see what is going on...
70 print tweet['user']['screen_name'].encode('utf-8'), tweet['text'].encode('utf-8')