· KLDP.org · KLDP.net · KLDP Wiki · KLDP BBS ·
lacovnk/kldp-new

소개

  • 완전히 읽지 않은 글만 보기 위한 스크립트입니다.
require 'rubygems'
require 'CGI'
require 'mechanize'
require 'hpricot'
 
KLDP_FORUM_DEFAULT = {
  6=>{:name=>" 설치 및 활용 QnA", :link=>"http://kldp.org/forum/6"},
  12=>{:name=>" 자유 게시판", :link=>"http://kldp.org/forum/12"},
  13=>{:name=>"멀티미디어 갤러리",:link=>"http://kldp.org/forum/13"},
  2797=>{:name=>" 강좌", :link=>"http://kldp.org/forum/2797"},
  16=>{:name=>"구인/구직, 장터", :link=>"http://kldp.org/forum/16"},
  31=>{:name=>"뉴스, 새소식", :link=>"http://kldp.org/forum/31"},
  32=>{:name=>"토론, 토의", :link=>"http://kldp.org/forum/32"},
  5=>{:name=>" 프로그래밍 QnA", :link=>"http://kldp.org/forum/5"}
}
KLDP_BASE_URL = 'http://kldp.org/'
KLDP_FORUM_URL = 'http://kldp.org/forum/'
 
class KLDPForum
  attr_reader :list_forum
 
  def initialize(username, passwd)
    @agent = WWW::Mechanize.new
    @agent.user_agent_alias = 'Windows IE 6' # 'Mac Safari'
    @username = username
    @passwd = passwd
 
    @list_forum = get_list_forum    
    raise "Login Failed" unless do_login
 
  end
 
  def do_login
    page = @agent.get(KLDP_BASE_URL)
    login_form = page.forms.first
    login_form.fields[0].value = @username
    login_form.fields[1].value = @passwd
    submit_result = @agent.submit(login_form)
    # puts submit_result.body
    (submit_result.body =~ /block-user-0/).nil?
  end
 
  def get_list_forum(use_default=true)
    return KLDP_FORUM_DEFAULT if use_default
    forums = {}
    page = @agent.get(KLDP_FORUM_URL)
    doc = Hpricot(page.body)
    (doc/"#forum//td[@class=forum]//a").each do |t|
      no = t['href'].gsub(/\/forum\//, '')
      forums[no] = {:link => KLDP_FORUM_URL + no, :name => CGI.unescape(t.inner_html)}
    end
    forums
  end
 
  def check_forum_all_page(forum_no, range=(0..0))
    links = []
    threads = []
    range.each do |page|
      cur_topics = get_new_topics(forum_no, page)
      links += cur_topics
      cur_topics.each do |topic|
        puts "<a href='#{topic[:link]}'>#{topic[:title]}(#{topic[:unread_comments]}/#{topic[:total_comments]})</a>"
        if false
          threads << Thread.new {@agent.get(topic[:link])}
          sleep(0.2)
        else
          @agent.get(topic[:link])
        end
      end
      # puts threads.size
    end
  end
 
  def get_new_topics(forum_no, page = 0)
    links = []
    return unless @list_forum.has_key? forum_no
    puts "Check new thread of #{@list_forum[forum_no][:name]}, page #{page}"
 
    url = @list_forum[forum_no][:link]
    url += "?page=#{page}" if page > 0
 
    page = @agent.get(url)
    doc = Hpricot(page.body)
 
    next if (doc/"#forum/table/tbody/tr").nil?
    (doc/"#forum/table/tbody/tr").each do |t|
      next if (t/"td[@class=icon]/a").size == 0
      a = (t/"td[@class=topic]/a").first
      link = a['href']
      title = CGI.unescape(a.inner_html)
 
      tc_txt = (t/"td[@class=replies]").first.inner_text
      rep = (t/"td[@class=replies]/a").first
      next if rep.nil? and tc_txt.strip != '0'
 
      if rep.nil?
        tc = uc = 0
      else
        uc_txt = rep.inner_text
        tc = tc_txt.gsub(uc_txt, '').to_i
        uc = uc_txt.gsub(tc_txt, '').gsub(/[^0-9]/, '').to_i
        next if tc != uc and tc != 0
      end
      links << {:link => KLDP_BASE_URL + link, :title => title, :total_comments => tc, :unread_comments => uc}
    end
    links
  end
end
 
if $0 == __FILE__
  username = "" # username
  passwd = "" # password
 
  kldp_forum = KLDPForum.new(username, passwd)
 
  # kldp_forum.check_forum_all_page(5, (0..20))  
  # kldp_forum.check_forum_all_page(6, (0..20))
  # kldp_forum.check_forum_all_page(12, (0..5))
 
  exit
  kldp_forum.list_forum.each do |forum_no, forum|
    puts "\t #{forum_no}: #{forum[:name]}"
  end
 
  STDIN.gets.strip.split.each do |forum_no|
    kldp_forum.check_forum_all_page(forum_no.to_i)
  end
end




sponsored by andamiro
sponsored by cdnetworks
sponsored by HP

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2008-09-27 18:20:26
Processing time 0.0054 sec