Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- more changes

  • Loading branch information...
commit 549e2fbf42340e4cb603125a9ca8568b0d9080fa 1 parent c597722
@carimura authored
View
21 app/controllers/twitter_controller.rb
@@ -22,20 +22,7 @@ def update_klout_serially
u.delete if u.twitter_username.nil?
puts "Getting score for #{u.twitter_username}"
-
- begin
- # Get the score!!
- response = RestClient.get 'http://api.klout.com/1/klout.json', {:params => {:key => "srss75s63y9bejb6s9ar3xwr", :users => u.twitter_username}}
- parsed = JSON.parse(response)
- score = parsed["users"][0]["kscore"] #if parsed["users"] && parsed["users"][0]
- u.klout_score = score
- u.save
-
- puts "Found Score --> #{score}"
- rescue
- # Klout throws a 404 if the user can't be found.. whatever.
- puts "There was an error for #{u.twitter_username}... skipping."
- end
+ u.get_klout_score
x += 1
end
@@ -50,8 +37,8 @@ def update_klout_serially
def update_klout_parallel
worker = KloutBatchQb.new
worker.db_settings = MONGOID[Rails.env]
- worker.queue
-
+ worker.queue(:priority => 2)
+
flash[:success] = "Parallel running"
redirect_to :action => :index
end
@@ -65,7 +52,7 @@ def delete_klouts
u.save
end
t2 = Time.now
-
+
flash[:success] = "Klouts deleted for #{users.count} users in #{t2 - t1} seconds"
redirect_to :action => :index
end
View
5 app/controllers/users_controller.rb
@@ -22,6 +22,11 @@ def show
end
+ def klout_score
+ u = User.find(params[:id])
+ puts "returning klout score #{u.klout_score} for user #{u.twitter_username}"
+ render :text => u.klout_score
+ end
end
View
17 app/models/user.rb
@@ -26,5 +26,20 @@ def self.transfer_to_mongo(users)
users.count
end
-
+
+ def get_klout_score
+ begin
+ # Get the score!!
+ response = RestClient.get 'http://api.klout.com/1/klout.json', {:params => {:key => "srss75s63y9bejb6s9ar3xwr", :users => self.twitter_username}}
+ parsed = JSON.parse(response)
+ score = parsed["users"][0]["kscore"] #if parsed["users"] && parsed["users"][0]
+ self.klout_score = score
+ self.save
+ puts "Found Score --> #{score}"
+ rescue
+ # Klout throws a 404 if the user can't be found.. whatever.
+ puts "There was an error for #{self.twitter_username}... skipping."
+ end
+ end
+
end
View
13 app/views/layouts/application.html.erb
@@ -3,7 +3,18 @@
<head>
<title>Mongo1</title>
<%= stylesheet_link_tag :all %>
- <%= javascript_include_tag :defaults %>
+
+ <script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAhes0f80sBcwL-h5xCNkkgxQBmiBpQeSpIciQPfZ5Ss-a60KXIRQOVvqzsNpqzhmG9tjky_5rOuaeow"></script>
+
+ <script type="text/javascript">
+ google.load("jquery", "1");
+ google.load("jqueryui", "1");
+ </script>
+
+ <%= javascript_include_tag "application",
+ "jquery.timer"
+ %>
+
<%= csrf_meta_tag %>
</head>
<body>
View
28 app/views/twitter/index.html.erb
@@ -26,13 +26,37 @@
<% @users.each do |u| %>
<tr>
- <td>[<strong><%= u.klout_score.round unless u.klout_score.nil? %></strong>]</td>
+ <td>[<span id="score_<%= u.id %>" style="font-weight: bold;"><%= u.klout_score.round unless u.klout_score.nil? %></span>]
+ </td>
<td><%= u.first_name + " " + u.last_name %></td>
<td>(<%= link_to u.twitter_username || "no twitter name yet", "http://www.twitter.com/#{u.twitter_username}", :target => :new %>)</td>
</tr>
<% end %>
</table>
-
</div>
+
+
+<script type="text/javascript">
+
+
+ //$.timer(10000, function (timer) {
+ <% @users.each do |u| %>
+ //display_klout("<%= u.id %>");
+ <% end %>
+ //});
+
+
+ function display_klout(user_id) {
+ $.ajax({
+ type: "GET",
+ url: "/users/klout_score/" + user_id,
+ success: function(data) {
+ $('#score_' + user_id).html(data);
+ }
+ });
+ }
+
+</script>
+
View
24 app/workers/klout_batch_qb.rb
@@ -2,35 +2,41 @@ class KloutBatchQb < SimpleWorker::Base
merge "../models/user"
- merge_worker "klout_batch_worker", "KloutBatchWorker"
+ merge_worker File.join(File.dirname(__FILE__), "klout_batch_worker.rb"), "KloutBatchWorker"
attr_accessor :db_settings
def run
log "Running Klout Batch Quarterback!"
init_mongohq
-
+
users = User.all
users_chunk = []
i=0
users.each do |u|
- i>=31 ? break : i+=1
-
+ #i>=32 ? break : i+=1
+
log "Adding user #{u.twitter_username}"
- users_chunk << u.twitter_username
+ users_chunk << u
- if users_chunk.size % 10 == 0
+ if users_chunk.size % 15 == 0
log "Creating worker with #{users_chunk.inspect}"
- kbw = KloutBatchWorker.new
- kbw.users = users_chunk
- kbw.queue
+ queue_kbw(users_chunk)
users_chunk = []
end
end
+ queue_kbw(users_chunk) unless users_chunk.empty?
+ end
+
+ def queue_kbw(users_chunk)
+ kbw = KloutBatchWorker.new
+ kbw.users = users_chunk
+ kbw.db_settings = db_settings
+ kbw.queue(:priority => 2)
end
View
41 app/workers/klout_batch_worker.rb
@@ -1,16 +1,49 @@
class KloutBatchWorker < SimpleWorker::Base
- attr_accessor :users
+ merge "../models/user"
+
+ attr_accessor :users, :db_settings
def run
- log "Running Klout Batch Worker with users --> #{users.inspect}!"
+ log "Running Klout Batch Worker"
+ init_mongohq
+
+ log "Found users --> #{users.inspect} "
users.each do |u|
- log "Found user #{u}!"
+ log "Getting score for #{u["twitter_username"]}"
+ get_klout_score(u["twitter_username"])
+ end
+ end
+
+
+ def get_klout_score(username, retries=0)
+ begin
+ response = RestClient.get 'http://api.klout.com/1/klout.json', {:params => {:key => "srss75s63y9bejb6s9ar3xwr", :users => username}}
+ parsed = JSON.parse(response)
+
+ score = parsed["users"][0]["kscore"] #if parsed["users"] && parsed["users"][0]
+
+ u = User.first(conditions: {twitter_username: username})
+ u.klout_score = score
+ u.save
+ log "Found Score --> #{score}"
+ rescue => ex
+ log "Exception for #{username}: #{ex.inspect}"
+ sleep 1
+ return if retries > 5 || ex.to_s.include?("404")
+ retries += 1
+ get_klout_score(username, retries)
end
end
+ def init_mongohq
+ Mongoid.configure do |config|
+ config.database = Mongo::Connection.new(db_settings["host"], db_settings["port"]).db(db_settings["database"])
+ config.database.authenticate(db_settings["username"], db_settings["password"])
+ config.persist_in_safe_mode = false
+ end
+ end
-
end
View
3  config/routes.rb
@@ -5,6 +5,9 @@
root :to => "welcome#index"
+
+ match 'users/klout_score/:id' => 'users#klout_score'
+
match ':controller(/:action(/:id(.:format)))'
View
75 public/javascripts/jquery.timer.js
@@ -0,0 +1,75 @@
+/*
+ *
+ * jQuery Timer plugin v0.1
+ * Matt Schmidt [http://www.mattptr.net]
+ *
+ * Licensed under the BSD License:
+ * http://mattptr.net/license/license.txt
+ *
+ */
+
+ jQuery.timer = function (interval, callback)
+ {
+ /**
+ *
+ * timer() provides a cleaner way to handle intervals
+ *
+ * @usage
+ * $.timer(interval, callback);
+ *
+ *
+ * @example
+ * $.timer(1000, function (timer) {
+ * alert("hello");
+ * timer.stop();
+ * });
+ * @desc Show an alert box after 1 second and stop
+ *
+ * @example
+ * var second = false;
+ * $.timer(1000, function (timer) {
+ * if (!second) {
+ * alert('First time!');
+ * second = true;
+ * timer.reset(3000);
+ * }
+ * else {
+ * alert('Second time');
+ * timer.stop();
+ * }
+ * });
+ * @desc Show an alert box after 1 second and show another after 3 seconds
+ *
+ *
+ */
+
+ var interval = interval || 100;
+
+ if (!callback)
+ return false;
+
+ _timer = function (interval, callback) {
+ this.stop = function () {
+ clearInterval(self.id);
+ };
+
+ this.internalCallback = function () {
+ callback(self);
+ };
+
+ this.reset = function (val) {
+ if (self.id)
+ clearInterval(self.id);
+
+ var val = val || 100;
+ this.id = setInterval(this.internalCallback, val);
+ };
+
+ this.interval = interval;
+ this.id = setInterval(this.internalCallback, this.interval);
+
+ var self = this;
+ };
+
+ return new _timer(interval, callback);
+ };
Please sign in to comment.
Something went wrong with that request. Please try again.