· 6 years ago · Jan 14, 2019, 04:04 PM
1import twitter,json,csv
2
3CONSUMER_KEY = 'PZBQNWFthc0kExKvCascYvwFr' #my consumer key
4CONSUMER_SECRET = 'E4IwsQdZPMF1MYCWbTcJLcbFy9mXmD3CI0zNtTd39CWim2XjHP' #my consumer secret
5OAUTH_TOKEN = '1365237002-6XENowGxqc0I0epaBjROPu9kBfFSlHW1BNYtDhV' #my oauth token
6OAUTH_TOKEN_SECRET = 'uN0ocsmOt9Mo0DBUhOlOF5DddvQ8DIiQpAkz1safCmEPr' #my 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('XXX9.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 = "giletsjaunes" # 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 if tweet['truncated']:
41 tweet_text = tweet['extended_tweet']['full_text']
42### that's the bit that I changed:
43 if 'retweeted_status' in tweet: #checking whether the tweet is a retweet
44 if 'extended_tweet' in tweet['retweeted_status']:
45 user_text = tweet['retweeted_status']['user']
46 [x.encode('utf-8') for x in user_text]
47 retweet_text = tweet['retweeted_status']['extended_tweet']['full_text']
48 [x.encode('utf-8') for x in retweet_text]
49 # tweet_text = "RT ".join(tweet['retweeted_status']['user'], tweet['retweeted_status']['text']))
50 tweet_text = "RT " + tweet['retweeted_status']['user']['screen_name'] + tweet['retweeted_status']['extended_tweet']['full_text']
51
52 else:
53 tweet_text = tweet['text']
54 else:
55 tweet_text = tweet['text']
56 # write the values to file
57 csvwriter.writerow([
58 tweet['created_at'],
59 getVal(tweet['user']['screen_name']),
60 getVal(tweet_text),
61 getVal(tweet['user']['location']),
62 getVal(tweet['source']),
63 tweet['user']['statuses_count'],
64 tweet['user']['followers_count'],
65 tweet['user']['friends_count'],
66 tweet['user']['created_at'],
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')
71 # if 'retweeted_status' in tweet:
72 # if 'extended_tweet' in tweet['retweeted_status']:
73 #
74 # # print json.dumps(tweet) + "||||||||||||||"
75 # print tweet['retweeted_status']['extended_tweet']['full_text']