## make ranking nicovideo source. ## ## - module: Feed::nicovideo_rank ## config: ## cache: cache file base name. ## ## Copyright (C) 2007-2008, INOUE Yasuyuki ## Original version is written by TADA Tadashi ## You can redistribute it and/or modify it under GPL3 or any later version. begin require 'rubygems' rescue LoadError end require 'hpricot' require 'yaml' def nicovideo_rank(config, data) raw_items = {} last_fetched = 0 target_hour = 24 rank_items = [] open( "#{config['rank_data']}.last_fetched" ) {|f| last_fetched = YAML.load( f ) } FileUtils.mkpath(config['root_dir'] + last_fetched.strftime("/%Y/%m/%d")) open( "#{config['rank_data']}.dump" ) {|f| raw_items = YAML.load( f ) } begin open(config['root_dir'] + last_fetched.strftime("/%Y/%m/%d/") + "rank.yaml") {|f| rank_items = YAML.load( f ) } rescue Errno::ENOENT end if config['hour'] then target_hour = config['hour'].to_i end target_time = last_fetched - (target_hour * 60 * 60 - 1200) raw_items.keys.sort{|a, b| a.scan(/\d+$/)[0].to_i <=> b.scan(/\d+$/)[0].to_i}.reverse.each do |key| raw_item, uploaded_at, updated_at = raw_items[key] next if uploaded_at > target_time if updated_at < last_fetched || uploaded_at < target_time - 60 * 30 then raw_items.delete key next end rank_items << raw_item end open( "#{config['rank_data']}.dump", 'w' ) do |f| YAML.dump(raw_items, f) end open(config['root_dir'] + last_fetched.strftime("/%Y/%m/%d/") + "rank.yaml", 'w' ) do |f| YAML.dump(rank_items, f) end rank_items end