-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwitterAPI_test.py
More file actions
133 lines (105 loc) · 5.06 KB
/
TwitterAPI_test.py
File metadata and controls
133 lines (105 loc) · 5.06 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
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
#Practice Using Twitter API
#Sources
# https://grahamnic.wordpress.com/2013/09/15/python-using-the-twitter-api-to-datamine/
# http://simpledeveloper.com/twitter-search-api-example/
# http://simpledeveloper.com/how-to-use-python-to-see-what-people-are-saying-about-speaker-john-boehner/
# use the following line to install the python wrapper for the Twitter API
# sudo easy_install python-twitter
import twitter
####################------------------------------------------------####################
# include argparse so that we can just put in the arguments in the command line in terminal
import argparse
#Top Level
parser = argparse.ArgumentParser(prog='stwitter', description='Custom search of Twitter')
parser.add_argument('-c', '--count', metavar='num', default=100, type=int, help='The total number of tweets to return (before filters)')
parser.add_argument('-f', '--removefilters', action='store_true', help='Removes the tweet filters')
parser.add_argument('-d', '--removedirect', action='store_true', help='Filters direct tweets to our account')
sp = parser.add_subparsers(dest='command')
#Adventure Sub
sp_adventure = sp.add_parser('adventure', help='%(prog)s searches for adventure')
#MyHob Sub
sp_myhob = sp.add_parser('myhob', help='%(prog)s searches for my hobbies')
#Keyword Sub
sp_keyword = sp.add_parser('search', help='%(prog)s searches using a custom query')
sp_keyword.add_argument('term', type=str, help='A query for searching Twitter; must be in quotes')
####################------------------------------------------------####################
#Documentation for Twitter package
# https://code.google.com/p/python-twitter/
# https://dev.twitter.com/overview/documentation
# https://www.apichangelog.com/api/twitter
#Exploring the Twitter API -- API Console
# https://dev.twitter.com/rest/tools/console
# the following gives all the required authorization data to Twitter so they know who we are and we know who they are
api = twitter.Api(
consumer_key='rdoex8TSB5LYifzEF1xTRTItd',
consumer_secret='48HfWWfrzh5Z2WgSOVMuQxoqXxzCbEhaucRQs2lHnFfsKR9YAa',
access_token_key='2875906764-MWh7leZxkOVNcer85k0evxC2nbFwdih2R8eqC4C',
access_token_secret='kQPldSmsWnSX3rmyS3lUUkZWdL5T0GZZfrfoP6wbd8gWY'
)
# find all that info here:
# https://apps.twitter.com/
# simple twitter search by using the following code
#search = api.GetSearch(term='adventure', lang='en', result_type='recent', count=100, max_id='')
#for t in search:
# print t.user.screen_name + ' (' + t.created_at + ')'
# #Add the .encode to force encoding
# print t.text.encode('utf-8') + '\n'
####################------------------------------------------------####################
#How to set up Twitter Search
# go to the following link and do a sample search
# https://twitter.com/search
# and follow the query options:
# https://dev.twitter.com/rest/public/search
# note: the format should follow the 'URL encode' syntax:
# http://en.wikipedia.org/wiki/Percent-encoding
# also check out the Get/Search Documentation:
# https://dev.twitter.com/rest/reference/get/search/tweets
# check this link out for Streaming Public Tweets
# https://dev.twitter.com/streaming/public
####################------------------------------------------------####################
#Parse the Args
args = parser.parse_args()
if args.command != '':
#Custom looping so we can search more than 100 tweets
tweetID = ''
i = int(round(args.count -51, -2)) / 100 + 1
for x in range (0, i):
#Perform Find
if args.command == 'adventure':
print '------Searching Tweets about adventure ' + str(x * 100) + '/' + str(args.count) + '------'
search = api.GetSearch(term='"adventure" OR "space travel" OR "deep sea diving" OR "exploring"', lang='en', result_type='recent', count=(args.count - 100 * x), max_id=tweetID)
elif args.command == 'myhob':
print '------Searching Tweets about my hobbies ' + str(x * 100) + '/' + str(args.count) + '------'
search = api.GetSearch(term='"computers" OR "python" OR "Japanese" OR "Bento"', lang='en', result_type='recent', count=(args.count - 100 * x), max_id=tweetID)
elif args.command == 'search':
print '------Searching Tweets using \"' + args.term + '\"' + str(x * 100) + '/' + str(args.count) + '------'
search = api.GetSearch(term=args.term, lang='en', result_type='recent', count=(args.count - 100 * x), max_id=tweetID)
#Filter Results
for t in search:
#Filter the results by default
if args.removefilters == False:
if (
#Filters Twitter Account
t.user.screen_name != 'jerkface' and
t.user.screen_name != 'notniceguy' and
t.user.screen_name != 'spambot' and
t.user.screen_name != 'junkbot' and
#Filter Retweets
'RT @' not in t.text and
#Filter Direct Tweets
(args.removedirect == False or '@mytwittername' not in t.text) and
#Filter out words
'sex' not in t.text):
print ''
print t.user.screen_name + ' (' + t.created_at + ')'
#Add the .encode to force encoding
print t.text.encode('utf-8')
print ''
else:
print ''
print t.user.screen_name
print t.created_at
print t.text.encode('utf-8')
print ''
#Save the this tweet ID
tweetID = t.id