-
Notifications
You must be signed in to change notification settings - Fork 0
/
hatena_feed.rb
executable file
·89 lines (70 loc) · 2.1 KB
/
hatena_feed.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#! /usr/bin/env ruby
require 'uri'
require 'net/http'
require 'rss'
ICON = '📡'.freeze
# Please uncommentout the genre you like
FEED_URLS = [
# # entry
# 'http://feeds.feedburner.com/hatena/b/hotentry',
# 'http://b.hatena.ne.jp/entrylist.rss',
# # social
# 'http://b.hatena.ne.jp/hotentry/social.rss',
# 'http://b.hatena.ne.jp/entrylist/social.rss',
# # economics
# 'http://b.hatena.ne.jp/hotentry/economics.rss',
# 'http://b.hatena.ne.jp/entrylist/economics.rss',
# # life
# 'http://b.hatena.ne.jp/hotentry/life.rss',
# 'http://b.hatena.ne.jp/entrylist/life.rss',
# # knowledge
# 'http://b.hatena.ne.jp/hotentry/knowledge.rss',
# 'http://b.hatena.ne.jp/entrylist/knowledge.rss',
# # it
# 'http://b.hatena.ne.jp/hotentry/it.rss',
# 'http://b.hatena.ne.jp/entrylist/it.rss',
# # entertainment
# 'http://b.hatena.ne.jp/hotentry/entertainment.rss',
# 'http://b.hatena.ne.jp/entrylist/entertainment.rss',
# # game
# 'http://b.hatena.ne.jp/hotentry/game.rss',
# 'http://b.hatena.ne.jp/entrylist/game.rss',
# # fun
# 'http://b.hatena.ne.jp/hotentry/fun.rss',
# 'http://b.hatena.ne.jp/entrylist/fun.rss',
# # video
# 'http://feeds.feedburner.com/hatena/b/video',
].freeze
def feeds
FEED_URLS.map do |url|
parsed_url = URI.parse(url)
{ header: generate_header(parsed_url), items: fetch_feed(parsed_url).items }
end
end
def generate_header(uri)
name = uri.path.gsub(/\A\/|\.\w \z/, '').split('/').join('_')
url = uri.to_s.gsub(/\.\w \z/, '')
{ name: name, url: url }
end
def fetch_feed(uri)
option = { 'user-agent' => 'Chrome/54.0.2840.98' }
response = Net::HTTP.start(uri.host) do |http|
http.get(uri.path, option)
end
RSS::Parser.parse(response.body)
end
def print(feed)
puts "#{feed[:header][:name]} | href=#{feed[:header][:url]}"
feed[:items].each do |item|
title = item.title.gsub('|', '/')
puts "--#{title} | href=#{item.link}"
end
end
# --------------------------------------
# Display
# --------------------------------------
puts ICON
puts '---'
feeds.each { |feed| print(feed) }
puts '---'
puts 'reload | color=red refresh=true'