root/platform/pragger/Feed/nicovideo_feed.rb @ 7816

Revision 7816, 3.1 kB (checked in by yasuyuki, 5 years ago)

platform/pragger/Feed/nicovideo_feed.rb: HTML構造の変更に影響されることを避けるため、動画ID以外をAPIで取得するよう修正 23

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      thumb = client.get(id)
39
40                        if items[key] then
41        raw_items[key] = get_rank_item(key, thumb, updated_at) if raw_items[key]
42      else
43        item = RSS::RDF::Item::new
44        item.link = key
45        if thumb then
46          item.title = thumb['title']
47          item.description = "<a href=\"#{key}\"><img border=\"0\" src=\"#{thumb['thumbnail_url']}\"/></a><br/>"
48        else
49          item.title = thumb['title']
50        end
51        item.description += thumb['description']
52        item.date = updated_at
53        items[key] = item
54        raw_items[key] = get_rank_item(key, thumb, updated_at)
55                        end
56                end
57        end
58 
59  # exit if nothing fetched
60  exit unless found
61       
62        return_items = items.values.sort {|a,b| a.date == b.date ? (a.link.scan(/\d+$/)[0].to_i <=> b.link.scan(/\d+$/)[0].to_i) : (a.date <=> b.date) }.reverse
63       
64  save(config, items, return_items, raw_items, updated_at)
65 
66        return_items
67end
68
69def get_rank_item(key, thumb, updated_at)
70  cache = {}
71  cache['link'] = key
72  cache['title'] = thumb['title']
73  cache['viewed'] = thumb['view_counter'].to_f
74  cache['mylisted'] = thumb['mylist_counter'].to_f
75  cache['uploaded_at'] = thumb['first_retrieve']
76  cache['updated_at'] = updated_at
77  cache
78end
79
80def load(config)
81        items = {}
82        raw_items = {}
83        begin
84                if config['cache'] then
85                        open( config['cache'] ) {|f| items = YAML.load( f ) }
86                end
87        rescue Errno::ENOENT
88        end
89        begin
90                if config['rank'] then
91                        open( "#{config['rank']}.dump" ) {|f| raw_items = YAML.load( f ) }
92                end
93        rescue Errno::ENOENT
94        end
95  return items, raw_items
96end
97
98def save(config, items, return_items, raw_items, updated_at)
99        if config['cache'] then
100          oldest_cache_item = items.size > 1200 && return_items[1200].link.scan(/\d+$/)[0].to_i
101                cache_items = (items.size > 1200) ? items.reject {|a, b| b.link.scan(/\d+$/)[0].to_i < oldest_cache_item} : items.dup
102                open( config['cache'], 'w' ) do |f|
103                        YAML.dump(cache_items, f)
104                end
105        end
106        if config['rank'] then
107                open( "#{config['rank']}.dump", 'w' ) do |f|
108                        YAML.dump(raw_items, f)
109                end
110                open( "#{config['rank']}.last_fetched", 'w' ) do |f|
111                        YAML.dump(updated_at, f)
112                end
113        end
114end
Note: See TracBrowser for help on using the browser.