· 6 years ago · Jan 14, 2019, 03:14 PM
1#importing different modules that enable to code to be executed
2import twitter,json,csv
3
4# == Twitter individual key to access twitter though my account ==
5CONSUMER_KEY = 'PmOcRgo03MFQ8GKxVNnGfKr5y'
6CONSUMER_SECRET = 'EeslSctsuHoo7XHXwu0NrJOjQnYp6OLHazepfeZnUZEd3nU4Ne'
7
8# == OAuth Authentication ==
9OAUTH_TOKEN = '835791178314493952-uHNNh2C0JjZQVB8tCQ85JNYI82O99cr'
10OAUTH_TOKEN_SECRET = '2Dt5E71f6JyUCoYWh1X5dsomaSXCA5Kf4vEU5lQhAUKZQ'
11
12#defining the variable auth, it accesses the twitter module and grasps the oath variable.
13auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET,
14 CONSUMER_KEY, CONSUMER_SECRET)
15
16#==defining the Twitter API and calling on the function==#
17twitter_api = twitter.Twitter(auth=auth)
18
19# setup a file to write to called IMD_tweets_extended.scv, it has only writing permission and overwrrites old file if new file is created
20csvfile = open('IMD_tweets_extended.csv', 'w')
21csvwriter = csv.writer(csvfile, delimiter='|') #this function will create delimited strings of data, delimited by |
22
23# heres a function that takes out characters that can break
24# our import into Excel and replaces them with spaces
25# it also does the unicode bit
26def getVal(val):
27 clean = ""
28 if val:
29 val = val.replace('|', ' ')
30 val = val.replace('\n', ' ')
31 val = val.replace('\r', ' ')
32 clean = val.encode('utf-8')
33 return clean
34
35
36q = "InternationalMigrantsDay" # defining the hashtag or query I want
37print 'Filtering the public timeline for track="%s"' % (q,) #prints out "Filtering the public timeline for keyword=InternationalMigrantsDay "%s" is replaced by the value following % of #InternationalMigrantsDay .
38
39twitter_stream = twitter.TwitterStream(auth=twitter_api.auth)
40
41stream = twitter_stream.statuses.filter(track=q)#telling the Twitter streaming API to track the query.
42
43for tweet in stream:#checks through all tweets in the Twiiter streaming API for every one with the query asked for
44
45 if tweet['truncated']:#if the tweet is truncated or short
46 tweet_text = tweet['extended_tweet']['full_text'] #then it has to make sure to tweet the extended text
47 else:
48 tweet_text = tweet['text'] #otherwise print tweet normally
49
50 # write the values to file
51 #getVal is used since screename, tweet text and location may have unique characters that need to be cleaned up, other values are very straight forward
52 csvwriter.writerow([
53 tweet['created_at'], #when the tweet was created
54 getVal(tweet['user']['screen_name']), #profile name
55 getVal(tweet_text),
56 getVal(tweet['user']['location']),#profile location
57 tweet['user']['statuses_count'],#number of tweets the user wrote
58 tweet['user']['followers_count'],#number of followers
59 tweet['user']['friends_count'], #number of following
60 tweet['user']['created_at'] #when the account was created
61 ])
62 # print user's screen name and tweet text.
63 print tweet['user']['screen_name'].encode('utf-8'), tweet['text'].encode('utf-8')