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