root/platform/pragger/Feed/nicovideo_rank.rb @ 6787

Revision 6787, 1.6 kB (checked in by yasuyuki, 6 years ago)

platform/pragger: change file format to yaml.

Line 
1## make ranking nicovideo source.
2##
3## - module: Feed::nicovideo_rank
4##   config:
5##     cache: cache file base name.
6##
7## Copyright (C) 2007-2008, INOUE Yasuyuki <inoue.yasuyuki0@gmail.com>
8## Original version is written by TADA Tadashi <sho@spc.gr.jp>
9## You can redistribute it and/or modify it under GPL3 or any later version.
10
11begin
12        require 'rubygems'
13rescue LoadError
14end
15require 'hpricot'
16require 'yaml'
17
18def nicovideo_rank(config, data)
19        raw_items = {}
20        last_fetched = 0
21        target_hour = 24
22        rank_items = []
23
24        open( "#{config['rank_data']}.last_fetched" ) {|f| last_fetched = YAML.load( f ) }
25        FileUtils.mkpath(config['root_dir'] + last_fetched.strftime("/%Y/%m/%d"))
26        open( "#{config['rank_data']}.dump" ) {|f| raw_items = YAML.load( f ) }
27        begin
28                open(config['root_dir'] + last_fetched.strftime("/%Y/%m/%d/") + "rank.yaml") {|f| rank_items = YAML.load( f ) }
29        rescue Errno::ENOENT
30        end
31
32        if config['hour'] then
33          target_hour = config['hour'].to_i
34        end
35       
36        target_time = last_fetched - (target_hour * 60 * 60 - 1200)
37       
38        raw_items.keys.sort{|a, b| a.scan(/\d+$/)[0].to_i <=> b.scan(/\d+$/)[0].to_i}.reverse.each do |key|
39                raw_item, uploaded_at, updated_at = raw_items[key]
40                next if uploaded_at > target_time
41                if updated_at < last_fetched || uploaded_at < target_time - 60 * 30 then
42                  raw_items.delete key
43                  next
44                end
45
46                rank_items << raw_item
47        end
48        open( "#{config['rank_data']}.dump", 'w' ) do |f|
49                YAML.dump(raw_items, f)
50        end
51        open(config['root_dir'] + last_fetched.strftime("/%Y/%m/%d/") + "rank.yaml", 'w' ) do |f|
52                YAML.dump(rank_items, f)
53        end
54        rank_items
55end
Note: See TracBrowser for help on using the browser.