| 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 |
|
|---|
| 12 | begin
|
|---|
| 13 | require 'rubygems'
|
|---|
| 14 | rescue LoadError
|
|---|
| 15 | end
|
|---|
| 16 | require 'hpricot'
|
|---|
| 17 | require 'rss'
|
|---|
| 18 | require 'yaml'
|
|---|
| 19 |
|
|---|
| 20 | def nicovideo_feed(config,data)
|
|---|
| 21 | items = {}
|
|---|
| 22 | raw_items = {}
|
|---|
| 23 | updated_at = Time::now
|
|---|
| 24 | begin
|
|---|
| 25 | if config['cache'] then
|
|---|
| 26 | open( config['cache'] ) {|f| items = Marshal.load( f ) }
|
|---|
| 27 | end
|
|---|
| 28 | rescue Errno::ENOENT
|
|---|
| 29 | end
|
|---|
| 30 | begin
|
|---|
| 31 | if config['rank'] then
|
|---|
| 32 | open( "#{config['rank']}.dump" ) {|f| raw_items = YAML.load( f ) }
|
|---|
| 33 | end
|
|---|
| 34 | rescue Errno::ENOENT
|
|---|
| 35 | end
|
|---|
| 36 | data.each do |html|
|
|---|
| 37 | doc = Hpricot( html )
|
|---|
| 38 | base_tag = doc / "base"
|
|---|
| 39 | base = ""
|
|---|
| 40 | base = base_tag.first['href'] if base_tag
|
|---|
| 41 | (doc / 'div.thumb_frm').each do |v|
|
|---|
| 42 | item = RSS::RDF::Item::new
|
|---|
| 43 | key = (v / 'a').first['href']
|
|---|
| 44 | key = base + key
|
|---|
| 45 | if items[key] then
|
|---|
| 46 | cache = {}
|
|---|
| 47 | cache['link'] = key
|
|---|
| 48 | thumb = (v/ 'img.thumb_img').first
|
|---|
| 49 | cache['title'] = thumb['alt']
|
|---|
| 50 | cache['viewed'] = (v / 'p.TXT10 strong')[1].inner_html.tr(',', '').to_f
|
|---|
| 51 | cache['mylisted'] = (v / 'p.TXT10 strong')[3].inner_html.tr(',', '').to_f
|
|---|
| 52 | raw_items[key] = [cache, items[key].date, updated_at]
|
|---|
| 53 | next
|
|---|
| 54 | end
|
|---|
| 55 | item.link = key
|
|---|
| 56 | thumb = (v/ 'img.thumb_img').first
|
|---|
| 57 | if thumb then
|
|---|
| 58 | item.title = thumb['alt']
|
|---|
| 59 | item.description = "<img src=\"#{thumb['src']}\"/><br/>"
|
|---|
| 60 | else
|
|---|
| 61 | item.title = (v / 'p.TXT12 a').first.inner_html
|
|---|
| 62 | end
|
|---|
| 63 | item.description += (v / 'p.TXT12' ).inner_html.gsub( /<.*?>/, '' )
|
|---|
| 64 | item.date = updated_at
|
|---|
| 65 | items[key] = item
|
|---|
| 66 | end
|
|---|
| 67 | end
|
|---|
| 68 |
|
|---|
| 69 | 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
|
|---|
| 70 |
|
|---|
| 71 | if config['cache'] then
|
|---|
| 72 | oldest_cache_item = items.size > 1200 && return_items[1200].link.scan(/\d+$/)[0].to_i
|
|---|
| 73 | cache_items = (items.size > 1200) ? items.reject {|a, b| b.link.scan(/\d+$/)[0].to_i < oldest_cache_item} : items.dup
|
|---|
| 74 | open( config['cache'], 'w' ) do |f|
|
|---|
| 75 | f.write(Marshal.dump(cache_items))
|
|---|
| 76 | end
|
|---|
| 77 | end
|
|---|
| 78 | if config['rank'] then
|
|---|
| 79 | open( "#{config['rank']}.dump", 'w' ) do |f|
|
|---|
| 80 | YAML.dump(raw_items, f)
|
|---|
| 81 | end
|
|---|
| 82 | open( "#{config['rank']}.last_fetched", 'w' ) do |f|
|
|---|
| 83 | f.write(Marshal.dump(updated_at))
|
|---|
| 84 | end
|
|---|
| 85 | end
|
|---|
| 86 | return_items
|
|---|
| 87 | end
|
|---|
| 88 |
|
|---|