Skip to content

Instantly share code, notes, and snippets.

@hlb
Forked from hungtatai/gist:4636786
Last active December 11, 2015 17:48
Show Gist options
  • Save hlb/4636803 to your computer and use it in GitHub Desktop.
Save hlb/4636803 to your computer and use it in GitHub Desktop.
# encoding: utf-8
require 'nokogiri'
require 'open-uri'
require 'pp'
base = 'http://www.ptt.cc'
url = 'http://www.ptt.cc/bbs/joke/index.html'
yesterday = Date.today - 1
stop_flag = false
topics = []
until stop_flag do
res = open(url)
doc = Nokogiri::HTML(res.read)
prev_page = doc.search('h2 a')[1]['href']
doc.search('div#prodlist tr').reverse.each do |tr|
a = tr.search('td')[-1].children[0]
like_count = tr.search('td')[2].content.to_i
post_time = tr.search('td')[3].content.to_s.strip.split('/')
post_time = Date.new(Date.today.year, post_time[0].to_i, post_time[1].to_i)
title = a.content
href = base + a['href']
if yesterday < post_time then
next
elsif yesterday == post_time
if like_count > 10 then
#topic_content = Nokogiri::HTML(HTTParty.get(href).to_s.force_encoding('utf-8')).search('div#mainContent pre').to_s
topic_content = "test"
topics.push({
:post_time => post_time,
:title => title,
:like_count => like_count,
:href => href,
:topic_content => topic_content
})
puts post_time.to_s+"\t"+title+"\t"+like_count.to_s+"\t"+href
#puts topic_content
end
else
stop_flag = true
break
end
end
url = base + prev_page
end
require 'rss'
require 'time'
rss_content = RSS::Maker.make('2.0') do |maker|
maker.channel.author = "Honda"
maker.channel.title = "Joke "+yesterday.to_s
maker.channel.link = base
maker.channel.updated = Time.now.to_s
maker.channel.description = "desc"
topics.each do |topic|
item = maker.items.new_item
item.title = "[${topic[:like_count]}]"+topic[:title]
item.link = topic[:href]
item.description = topic[:topic_content]
item.date = Time.parse(topic[:post_time].to_s)
end
end
#puts rss_content.encoding.name
#puts rss_content.to_s.encode("big5")
File.open('joke.rss', 'w') do |file|
file.write(rss_content.to_s)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment