PageRenderTime 36ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/tasks/update.rake

https://bitbucket.org/andey/bestofama
Ruby | 100 lines | 83 code | 12 blank | 5 comment | 8 complexity | 5f0c38a1a2cfa8b56d3a5313568df318 MD5 | raw file
  1. namespace :update do
  2. # Update Ops rankings statistics
  3. task :op => :environment do
  4. # select Op
  5. @op = Op.where(:comment_karma => 0, :wikipedia_hits => 0).first
  6. @op ||= Op.order(:updated_at).first
  7. @op.touch
  8. puts "UPDATE ENTITY: #{@op.name}"
  9. url = @op.links.where(:site_id => 1).first
  10. #acquire
  11. if url
  12. require 'api/stats.grok.se'
  13. slug = url.link.match(/([^\/]*)$/)[1]
  14. grokse = Grokse.new
  15. stats = grokse.latest90(slug)
  16. if stats
  17. sum = stats['daily_views'].values.sum
  18. @op.update_attribute(:wikipedia_hits, sum)
  19. end
  20. end
  21. #update Reddit users link and comment karma
  22. require 'api/reddit.com'
  23. link_karma = 0
  24. comment_karma = 0
  25. @op.users.uniq.each do |user|
  26. reddit = Reddit.new
  27. json = reddit.getUser(user.username)
  28. link_karma += json["data"]["link_karma"].to_i
  29. comment_karma += json["data"]["comment_karma"].to_i
  30. user.update_attributes(:link_karma => json["data"]["link_karma"], :comment_karma => json["data"]["comment_karma"])
  31. end
  32. @op.update_attributes(:link_karma => link_karma, :comment_karma => comment_karma)
  33. end
  34. task :ama, [:hours] => [:environment] do |t, args|
  35. @ama = Ama.where("date > ?", Time.now - args[:hours].to_i.hours).order(:updated_at).first
  36. @ama.fetch() unless !@ama
  37. end
  38. task :old_ama => :environment do
  39. @ama = Ama.order(:updated_at).first
  40. puts @ama.title
  41. @ama.fetch() unless !@ama
  42. end
  43. task :upcoming => :environment do
  44. google = 'http://www.google.com/calendar/feeds/amaverify@gmail.com/public/full?orderby=starttime&max-results=100&singleevents=true&sortorder=ascending&futureevents=true&alt=json'
  45. http = Net::HTTP.new("www.google.com")
  46. request = Net::HTTP::Get.new(google)
  47. response = http.request(request)
  48. json = JSON.parse(response.read_body)
  49. json["feed"]["entry"].each do |entry|
  50. url = entry['id']['$t'].to_s
  51. title = entry['title']['$t'].to_s
  52. content = entry['content']['$t'].to_s
  53. date = entry['gd$when'][0]['startTime']
  54. Upcoming.create(:title => title, :description => content, :date => date, :url => url)
  55. end
  56. end
  57. # Update Tag with DuckDuckGo abstract definition
  58. task :tag => :environment do
  59. require 'api/duckduckgo.com'
  60. @tag = Tag.where(:meaningless => nil, :description => nil).first
  61. if @tag
  62. begin
  63. puts "----------------------------"
  64. puts "Updating Tag: #{@tag.name}"
  65. duckduckgo = DuckDuckGo.new
  66. result = duckduckgo.search(@tag.name)
  67. if result["AbstractSource"] == 'Wikipedia'
  68. puts "Set the AbstractSource"
  69. @tag.update_attribute(:wikipedia_url, result["AbstractURL"])
  70. end
  71. if result["AbstractText"] != ''
  72. puts "Used AbstractText"
  73. @tag.update_attribute(:description, result["AbstractText"].truncate(250, :omission => "..."))
  74. elsif result["RelatedTopics"] && result["RelatedTopics"][0]
  75. puts "Used Related Topics"
  76. @tag.update_attribute(:description, result["RelatedTopics"][0]["Text"].truncate(250, :omission => "..."))
  77. else
  78. puts "??????????"
  79. @tag.update_attribute(:meaningless, true)
  80. end
  81. rescue
  82. @tag.update_attribute(:meaningless, true)
  83. ensure
  84. puts "done"
  85. end
  86. end
  87. end
  88. end