twitter_kotoba 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,63 @@
1
+ require 'typhoeus'
2
+ require 'oj'
3
+
4
+ class TwitterConnection
5
+
6
+ attr_accessor :user
7
+ attr_reader :rate_limit_exceeded
8
+
9
+ def initialize(user)
10
+ @user = user
11
+ @rate_limit_exceeded = false
12
+ end
13
+
14
+ def get_latest_1000
15
+
16
+ requests = run_requests
17
+ tweets = process_requests(requests)
18
+
19
+ return tweets.flatten
20
+ end
21
+
22
+ def run_requests
23
+ # You can only request 200 tweets per page
24
+ # so we need to get 5 pages for a total of 1000 tweets.
25
+ requests = []
26
+
27
+ # The typhoeus hydra will run all its queued requests in parallel.
28
+ # Way faster than using Patron or HTTParty and running 5 requests in series
29
+ twitter_hydra = Typhoeus::Hydra.new
30
+
31
+ (0..4).each do |req|
32
+ requests[req] = Typhoeus::Request.new("http://twitter.com/statuses/user_timeline.json?screen_name=#{@user}&count=200&page=#{req+1}")
33
+ requests[req].timeout = 10000 # 10 seconds max
34
+ twitter_hydra.queue requests[req]
35
+ end
36
+ twitter_hydra.run
37
+
38
+ return requests
39
+ end
40
+
41
+ def process_requests(requests)
42
+ # array to hold all the tweets
43
+ tweets = []
44
+
45
+ requests.each do |req|
46
+ unless req.response.code == 400
47
+ begin
48
+ resp = Oj.load(req.response.body) unless req.response.code != 200
49
+ rescue
50
+ # occasionaly the response causes Oj.load problems
51
+ # this needs to be looked at further
52
+ end
53
+ unless !resp || resp.empty?
54
+ tweets << resp
55
+ end
56
+ else
57
+ @rate_limit_exceeded = true
58
+ end
59
+ end
60
+ return tweets
61
+ end
62
+
63
+ end
@@ -0,0 +1,79 @@
1
+ require_relative 'twitter_kotoba/twitter_connection'
2
+
3
+ class TwitterKotoba
4
+
5
+ attr_accessor :user
6
+ attr_reader :latest_tweets
7
+ attr_reader :sorted_words
8
+ attr_reader :rate_limit_exceeded
9
+
10
+ def initialize(user)
11
+ @user = user
12
+ @latest_tweets = []
13
+ @sorted_words = []
14
+ @rate_limit_exceeded = false
15
+ end
16
+
17
+ def word_list
18
+ if update
19
+ word_hash = sanitized_tweets
20
+ @sorted_words = word_hash.sort_by{ |key,value| value }
21
+ output_words
22
+ elsif @rate_limit_exceeded
23
+ puts "\nTwitter rate limit exceeded.\n"
24
+ # Twitter's limit is 150 req/hr so...
25
+ # you can only make 150/5 = 30 req/hr
26
+ puts "You may only make 30 requests per hour.\n\n"
27
+ end
28
+
29
+ return nil
30
+ end
31
+
32
+ private
33
+
34
+ def update
35
+ # query the twitter api
36
+ twitter_connection = TwitterConnection.new(@user)
37
+ # store latest 1000 tweets in @latest_tweets
38
+ # latest_1000 = twitter_connection.get_latest_1000
39
+ @latest_tweets = twitter_connection.get_latest_1000
40
+ unless @latest_tweets.empty?
41
+ return true
42
+ else
43
+ @rate_limit_exceeded = true if twitter_connection.rate_limit_exceeded
44
+ return false
45
+ end
46
+ end
47
+
48
+ def sanitized_tweets
49
+ # make a hash that defaults to 0 to store words with their frequency
50
+ word_hash = Hash.new(0)
51
+ @latest_tweets.each do |tweet|
52
+ # remove non-word characters from tweet and make it an array of words
53
+ begin
54
+ sanitized_tweet = tweet['text'].gsub(/(\W|\d)+/," ").split(" ")
55
+ sanitized_tweet.each_with_object(word_hash){ |word,hash| hash[word] += 1}
56
+ rescue
57
+ # some weird things in tweets cause errors
58
+ # you should log these
59
+ end
60
+ end
61
+ return word_hash
62
+ end
63
+
64
+ def output_words
65
+ # In an effort not to overwhelm the shell's scrollback history
66
+ # I'm only outputting the top 500 words
67
+ @sorted_words.length < 500 ? from = 0 : from = @sorted_words.length - 500
68
+
69
+ @sorted_words[from..-1].each do |w|
70
+ puts "#{w[1].to_s.rjust(4)}: #{w[0]}"
71
+ end
72
+
73
+ unless from == 0
74
+ puts "\nThis is the top 500 words.\n"
75
+ puts "Call \"sorted_words\" for the entire list.\n"
76
+ end
77
+ end
78
+
79
+ end
@@ -0,0 +1,20 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'twitter_kotoba'
3
+ s.version = '0.1.0'
4
+ s.date = '2012-09-01'
5
+ s.summary = "Shows word frequency of a user's last 1000 tweets."
6
+ s.description = "Twitter Kotoba gets a user's last 1000 tweets and prints the frequency of each word in descending order."
7
+ s.authors = ["Kenny Smith"]
8
+ s.email = 'kjsthree@gmail.com'
9
+ s.homepage = 'https://github.com/kjs3/twitter_kotoba'
10
+
11
+ s.add_runtime_dependency('typhoeus', "~> 0.4")
12
+ s.add_runtime_dependency('oj', "~> 1.3")
13
+
14
+ s.files = %w[
15
+ twitter_kotoba.gemspec
16
+ lib/twitter_kotoba.rb
17
+ lib/twitter_kotoba/twitter_connection.rb
18
+ ]
19
+
20
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: twitter_kotoba
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kenny Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-09-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: typhoeus
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.4'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '0.4'
30
+ - !ruby/object:Gem::Dependency
31
+ name: oj
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ description: Twitter Kotoba gets a user's last 1000 tweets and prints the frequency
47
+ of each word in descending order.
48
+ email: kjsthree@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - twitter_kotoba.gemspec
54
+ - lib/twitter_kotoba.rb
55
+ - lib/twitter_kotoba/twitter_connection.rb
56
+ homepage: https://github.com/kjs3/twitter_kotoba
57
+ licenses: []
58
+ post_install_message:
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 1.8.24
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: Shows word frequency of a user's last 1000 tweets.
80
+ test_files: []