9 | 9 |
# http://7habitsofhighlyeffectivehackers.blogspot.com.au/2012/05/using-twitter-to-build-password.html
|
10 | 10 |
#
|
11 | 11 |
# Author:: Robin Wood ([email protected])
|
12 | |
# Copyright:: Copyright (c) Robin Wood 2012
|
|
12 |
# Copyright:: Copyright (c) Robin Wood 2014
|
13 | 13 |
# Licence:: Creative Commons Attribution-Share Alike 2.0
|
14 | 14 |
#
|
15 | 15 |
|
16 | |
require 'rubygems'
|
17 | |
require 'json'
|
18 | |
require 'net/http'
|
|
16 |
require 'yaml'
|
|
17 |
require 'twitter'
|
19 | 18 |
require 'getoptlong'
|
20 | 19 |
|
21 | 20 |
opts = GetoptLong.new(
|
22 | 21 |
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
|
|
22 |
[ '--config', GetoptLong::REQUIRED_ARGUMENT ],
|
23 | 23 |
[ '--count', '-c', GetoptLong::NO_ARGUMENT ],
|
24 | 24 |
[ '--min_word_length', "-m" , GetoptLong::REQUIRED_ARGUMENT ],
|
25 | 25 |
[ '--term_file', "-T" , GetoptLong::REQUIRED_ARGUMENT ],
|
|
29 | 29 |
[ '--verbose', "-v" , GetoptLong::NO_ARGUMENT ]
|
30 | 30 |
)
|
31 | 31 |
|
|
32 |
def sample_config
|
|
33 |
puts "The config file \"#{@config_file}\" is missing or invalid, please create a config file in the format:"
|
|
34 |
puts "options:
|
|
35 |
api_key: <YOUR KEY>
|
|
36 |
api_secret: <YOUR SECRET>
|
|
37 |
|
|
38 |
To get your keys you must register with Twitter at: https://apps.twitter.com/
|
|
39 |
"
|
|
40 |
exit
|
|
41 |
end
|
|
42 |
|
32 | 43 |
def usage
|
33 | |
puts 'twofi 1.0 Robin Wood ([email protected]) (www.digininja.org)
|
34 | |
twofi - Twitter Words Of Interest
|
35 | |
|
36 | |
Usage: twofi [OPTIONS]
|
|
44 |
puts 'twoif 2.0-beta Robin Wood ([email protected]) (www.digininja.org)
|
|
45 |
twoif - Twitter Words of Interest
|
|
46 |
|
|
47 |
Usage: twoif [OPTIONS]
|
37 | 48 |
--help, -h: show help
|
|
49 |
--config <file>: config file, default is twofi.yml
|
38 | 50 |
--count, -c: include the count with the words
|
39 | 51 |
--min_word_length, -m: minimum word length
|
40 | |
--term_file, -T file: a file containing a list of terms
|
41 | |
--terms, -t: comma separated usernames
|
|
52 |
--term_file, -T <file>: a file containing a list of terms
|
|
53 |
--terms, -t: comma separated search terms
|
42 | 54 |
quote words containing spaces, no space after commas
|
43 | |
--user_file, -U file: a file containing a list of users
|
44 | |
--users, -u: comma separated search terms
|
|
55 |
--user_file, -U <file>: a file containing a list of users
|
|
56 |
--users, -u: comma separated usernames
|
45 | 57 |
quote words containing spaces, no space after commas
|
46 | 58 |
--verbose, -v: verbose
|
47 | 59 |
|
|
49 | 61 |
exit
|
50 | 62 |
end
|
51 | 63 |
|
52 | |
def twitter_search(query, results=500)
|
53 | |
url = "http://search.twitter.com/search.json?q=" + URI.encode(query) + "&rpp=" + results.to_s
|
54 | |
resp = Net::HTTP.get_response(URI.parse(url))
|
55 | |
data = resp.body
|
56 | |
|
57 | |
# Should probably do some error handling here but not really sure
|
58 | |
# what errors could come back
|
59 | |
result = JSON.parse(data)
|
60 | |
|
61 | |
return result
|
|
64 |
# Default this to nil and it is then created
|
|
65 |
# when first needed in the search
|
|
66 |
|
|
67 |
@twitter_client = nil
|
|
68 |
|
|
69 |
def twitter_search(query)
|
|
70 |
if @twitter_client.nil?
|
|
71 |
@twitter_client = Twitter::REST::Client.new do |config|
|
|
72 |
config.consumer_key = @api_key
|
|
73 |
config.consumer_secret = @api_secret
|
|
74 |
unless @bearer_token.nil?
|
|
75 |
config.bearer_token = @bearer_token
|
|
76 |
end
|
|
77 |
end
|
|
78 |
end
|
|
79 |
|
|
80 |
begin
|
|
81 |
data = @twitter_client.search(query, :result_type => "recent")
|
|
82 |
rescue Twitter::Error::Forbidden, Twitter::Error::Unauthorized
|
|
83 |
puts "The authentication with Twitter failed, please check your API keys."
|
|
84 |
puts "If there is a bearer_token entry in your config file try removing that.\n\n"
|
|
85 |
exit
|
|
86 |
end
|
|
87 |
|
|
88 |
return data
|
62 | 89 |
end
|
63 | 90 |
|
64 | 91 |
users=[]
|
65 | 92 |
terms=[]
|
66 | 93 |
min_word_length=3
|
67 | 94 |
show_count=false
|
|
95 |
@config_file = "twofi.yml"
|
68 | 96 |
|
69 | 97 |
begin
|
70 | 98 |
opts.each do |opt, arg|
|
71 | 99 |
case opt
|
|
100 |
when "--config"
|
|
101 |
@config_file = arg
|
72 | 102 |
when '--count'
|
73 | 103 |
show_count = true
|
74 | 104 |
when '--help'
|
|
122 | 152 |
usage
|
123 | 153 |
end
|
124 | 154 |
|
|
155 |
# Check the config file exits then parse out of it
|
|
156 |
# the stuff that we need
|
|
157 |
|
|
158 |
if File.exists?(@config_file)
|
|
159 |
config = YAML.load_file(@config_file)
|
|
160 |
if config == false
|
|
161 |
sample_config
|
|
162 |
end
|
|
163 |
else
|
|
164 |
sample_config
|
|
165 |
end
|
|
166 |
|
|
167 |
@api_key = nil
|
|
168 |
@api_secret = nil
|
|
169 |
@bearer_token = nil
|
|
170 |
|
|
171 |
if config.include?"options"
|
|
172 |
if config["options"].include?"api_key" and config["options"].include?"api_secret"
|
|
173 |
@api_key = config["options"]["api_key"]
|
|
174 |
@api_secret = config["options"]["api_secret"]
|
|
175 |
else
|
|
176 |
sample_config
|
|
177 |
end
|
|
178 |
|
|
179 |
if @api_key == "<YOUR KEY>"
|
|
180 |
sample_config
|
|
181 |
end
|
|
182 |
|
|
183 |
if config["options"].include?"bearer_token"
|
|
184 |
@bearer_token = config["options"]["bearer_token"]
|
|
185 |
else
|
|
186 |
@bearer_token = nil
|
|
187 |
end
|
|
188 |
else
|
|
189 |
sample_config
|
|
190 |
end
|
|
191 |
|
125 | 192 |
results = []
|
126 | 193 |
|
127 | |
#puts terms.inspect
|
128 | 194 |
terms.each do |term|
|
129 | |
data = twitter_search(term, 500)
|
130 | |
results += data['results']
|
|
195 |
data = twitter_search(term)
|
|
196 |
results += data.to_a
|
131 | 197 |
end
|
132 | 198 |
|
133 | 199 |
if results.count == 0
|
|
135 | 201 |
else
|
136 | 202 |
wordlist = {}
|
137 | 203 |
results.each do |result|
|
138 | |
text = result['text']
|
|
204 |
# have to .dup the text as it comes in frozen
|
|
205 |
text = result.full_text.dup
|
139 | 206 |
# Strip any non word type characters
|
140 | 207 |
text.gsub!(/[^\w \s \d]/, ' ')
|
141 | 208 |
words = text.split(/\s/)
|
|
161 | 228 |
end
|
162 | 229 |
end
|
163 | 230 |
end
|
|
231 |
|
|
232 |
# Write out the bearer token, this saves making unnecessary
|
|
233 |
# requests next time
|
|
234 |
unless @twitter_client.bearer_token.nil?
|
|
235 |
config['options']["bearer_token"] = @twitter_client.bearer_token.to_s
|
|
236 |
File.open(@config_file,'w') do |h|
|
|
237 |
h.write config.to_yaml
|
|
238 |
end
|
|
239 |
end
|