| 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 | require 'nicovideo/thumbnail_info'
|
|---|
| 20 |
|
|---|
| 21 | def 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
|
|---|
| 62 | end
|
|---|
| 63 |
|
|---|
| 64 | def 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
|
|---|
| 72 | end
|
|---|
| 73 |
|
|---|
| 74 | def get_rank_item(key, thumb)
|
|---|
| 75 | cache = {}
|
|---|
| 76 | cache['link'] = key
|
|---|
| 77 | cache['uploaded_at'] = Time.parse(thumb['first_retrieve'])
|
|---|
| 78 | cache
|
|---|
| 79 | end
|
|---|
| 80 |
|
|---|
| 81 | def 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
|
|---|
| 97 | end
|
|---|
| 98 |
|
|---|
| 99 | def 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
|
|---|
| 115 | end |
|---|