· 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


ID
Password
Join
Promptness is its own reward, if one lives by the clock instead of the sword.


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.0032 sec