Tip: Click lines to highlight, hold ctrl/cmd to multi-select

http://codedumper.com/imupo (5-Oct @ 14:57)

Syntax Highlighted Code

  1. require 'net/http'
  2. require 'hpricot'
  3. require 'rss/maker'
  4. require 'uri'
  5.  
  6. class EMB
  7.     attr_writer :realm, :user, :password
  8.  
  9.     def initialize ( realm = 'chs_programming', user='zichun', password='paksux' )
  10.         @realm, @user, @password = realm, user, password
  11.         @cookies = []
  12.         @cookie_string = ''
  13.     end
  14.    
  15.     def get_cookie
  16.         http = Net::HTTP.new('smb.chs.edu.sg', 80)
  17.         resp,data = http.post('/cgi-bin/emb/login.pl',
  18.                     "userid=#{@user}&password=#{@password}&login=+++Login+++",
  19.                     {'referer'=>'http://smb.chs.edu.sg/emb/'+@realm+'/',
  20.                      'user_agent'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008051206 Firefox/3.0'
  21.                      })
  22.         resp.get_fields('set-cookie').each { |x|
  23.             @cookies << x
  24.             @cookie_string += x.split("\; ")[0] + "\;"
  25.         }
  26.         @cookie_string
  27.     end
  28.     def get_main
  29.         http = Net::HTTP.new('smb.chs.edu.sg', 80)
  30.         http.get('/cgi-bin/emb/view.pl?date', {'Cookie'=>@cookie_string})
  31.     end
  32.     def get_msg (link)
  33.         url = URI.parse(link)
  34.         http = Net::HTTP.new('smb.chs.edu.sg',80)
  35.         http.get( url.path+'?'+url.query, {'Cookie'=>@cookie_string} )
  36.     end
  37. end
  38.  
  39. emb_client = EMB.new
  40. emb_client.get_cookie
  41. res = emb_client.get_main
  42.  
  43. version = "2.0"
  44.  
  45. content = RSS::Maker.make(version) { |m|
  46.     m.channel.title = "EMB CHS_PROGRAMMING"
  47.     m.channel.link = "http://smb.chs.edu.sg/emb/chs_programming"
  48.     m.channel.description = "Extracted messages from chs_programming EMB"
  49.     m.items.do_sort = true
  50.  
  51.     doc = Hpricot(res.body)
  52.      
  53.     for i in 2...(doc/"form").length
  54.         item = m.items.new_item
  55.         x = (doc/"form")[i]
  56.         item.title = (x/"a").text
  57.         item.link = "http://smb.chs.edu.sg/emb/chs_programming/"
  58.         item.date = Time.parse(((x/"font")[0]).innerHTML.strip)
  59.         emb_body = Hpricot(emb_client.get_msg( x.at("a")['href'] ).body)
  60.         item.description = emb_body.at("pre")
  61.         item.author = (x/"td")[2].innerHTML.strip    
  62.     end
  63. }
  64.  
  65. $stdout.write(content)
  66.  

Plain Code

require 'net/http'
require 'hpricot' 
require 'rss/maker'
require 'uri'

class EMB 
    attr_writer :realm, :user, :password

    def initialize ( realm = 'chs_programming', user='zichun', password='paksux' )
        @realm, @user, @password = realm, user, password
        @cookies = []
        @cookie_string = ''
    end
    
    def get_cookie
        http = Net::HTTP.new('smb.chs.edu.sg', 80)
        resp,data = http.post('/cgi-bin/emb/login.pl',
                    "userid=#{@user}&password=#{@password}&login=+++Login+++", 
                    {'referer'=>'http://smb.chs.edu.sg/emb/'+@realm+'/',
                     'user_agent'=>'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9) Gecko/2008051206 Firefox/3.0'
                     })
        resp.get_fields('set-cookie').each { |x|
            @cookies << x
            @cookie_string += x.split("\; ")[0] + "\;"
        }
        @cookie_string
    end
    def get_main
        http = Net::HTTP.new('smb.chs.edu.sg', 80)
        http.get('/cgi-bin/emb/view.pl?date', {'Cookie'=>@cookie_string})
    end
    def get_msg (link)
        url = URI.parse(link)
        http = Net::HTTP.new('smb.chs.edu.sg',80)
        http.get( url.path+'?'+url.query, {'Cookie'=>@cookie_string} )
    end
end

emb_client = EMB.new
emb_client.get_cookie
res = emb_client.get_main

version = "2.0"

content = RSS::Maker.make(version) { |m|
    m.channel.title = "EMB CHS_PROGRAMMING"
    m.channel.link = "http://smb.chs.edu.sg/emb/chs_programming"
    m.channel.description = "Extracted messages from chs_programming EMB"
    m.items.do_sort = true

    doc = Hpricot(res.body)
     
    for i in 2...(doc/"form").length
        item = m.items.new_item
        x = (doc/"form")[i]
        item.title = (x/"a").text
        item.link = "http://smb.chs.edu.sg/emb/chs_programming/"
        item.date = Time.parse(((x/"font")[0]).innerHTML.strip) 
        emb_body = Hpricot(emb_client.get_msg( x.at("a")['href'] ).body)
        item.description = emb_body.at("pre")
        item.author = (x/"td")[2].innerHTML.strip    
    end
}

$stdout.write(content)

Permalink: http://codedumper.com/imupo