root/platform/pragger/Feed/nicovideo_feed.rb

Revision 28861, 2.9 kB (checked in by yasuyuki, 13 months ago)

次のページがあるかどうかの判別条件を修正

Line 
1## make feed nicovideo source.
2##
3## - module: Feed::nicovideo_feed
4##   config:
5##     cache: cache file.
6##
7## Copyright (C) 2007, TADA Tadashi <sho@spc.gr.jp>,
8## 2007-2008 INOUE Yasuyuki <inoue.yasuyuki0@gmail.com>
9## Original version is written by TADA Tadashi <sho@spc.gr.jp>
10## You can redistribute it and/or modify it under GPL3 or any later version.
11
12begin
13        require 'rubygems'
14rescue LoadError
15end
16require 'hpricot'
17require 'rss'
18require 'yaml'
19require 'nicovideo/thumbnail_info'
20
21def nicovideo_feed(config, data)
22        updated_at = Time::now
23  found = false
24  client = ::NicoVideo::ThumbnailInfo.new
25
26        items, raw_items = load(config)
27 
28        data.each do |html|
29                doc = Hpricot( html )
30                base_tag = doc / "base"
31                base = ""
32                base = base_tag.first['href'] if base_tag
33                (doc / 'a').each do |link|
34      next unless link['href'] && link['href'].include?("watch/")
35      found = true
36      id = link['href'][%r|watch/(.+)$|, 1]
37                        key = base + link['href']
38
39                        unless items[key] then
40        begin
41          thumb = client.get(id)
42        rescue Errno::ENOENT
43          # ignore "Nice boat." movie.
44          next
45        end
46        items[key] = get_rss_item(key, thumb)
47        raw_items[key] = get_rank_item(key, thumb) if config['rank']
48                        end
49                end
50        end
51 
52  # exit if nothing fetched
53  exit unless found
54       
55        return_items = items.values.sort {|a, b|
56    a.date == b.date ? (a.link.scan(/\d+$/)[0].to_i <=> b.link.scan(/\d+$/)[0].to_i) : (a.date <=> b.date)
57  }.reverse
58       
59  save(config, items, return_items, raw_items, updated_at)
60 
61        return_items
62end
63
64def get_rss_item(key, thumb)
65  item = RSS::RDF::Item::new
66  item.link = key
67  item.title = thumb['title']
68  item.description = "<a href=\"#{key}\"><img border=\"0\" src=\"#{thumb['thumbnail_url']}\"/></a><br/>"
69  item.description += thumb['description'] if thumb['description']
70  item.date = Time.parse(thumb['first_retrieve'])
71  item
72end
73
74def get_rank_item(key, thumb)
75  cache = {}
76  cache['link'] = key
77  cache['uploaded_at'] = Time.parse(thumb['first_retrieve'])
78  cache
79end
80
81def load(config)
82        items = {}
83        raw_items = {}
84        begin
85                if config['cache'] then
86                        open( config['cache'] ) {|f| items = YAML.load( f ) }
87                end
88        rescue Errno::ENOENT
89        end
90        begin
91                if config['rank'] then
92                        open( "#{config['rank']}.dump" ) {|f| raw_items = YAML.load( f ) }
93                end
94        rescue Errno::ENOENT
95        end
96  return items, raw_items
97end
98
99def save(config, items, return_items, raw_items, updated_at)
100        if config['cache'] then
101          oldest_cache_item = items.size > 800 && return_items[800].date
102                cache_items = (items.size > 800) ? items.reject {|a, b| b.date < oldest_cache_item} : items.dup
103                open( config['cache'], 'w' ) do |f|
104                        YAML.dump(cache_items, f)
105                end
106        end
107        if config['rank'] then
108                open( "#{config['rank']}.dump", 'w' ) do |f|
109                        YAML.dump(raw_items, f)
110                end
111                open( "#{config['rank']}.last_fetched", 'w' ) do |f|
112                        YAML.dump(updated_at, f)
113                end
114        end
115end
Note: See TracBrowser for help on using the browser.