Class: Ruby2ch::Dat

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby2ch/datparse.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, title) ⇒ Dat



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
# File 'lib/ruby2ch/datparse.rb', line 6

def initialize(url,title)
  #http://uni.2ch.net/test/read.cgi/news/1322032301/l50
  #http://bg20.2ch.net/test/r.so/uni.2ch.net/news/1322032301/
  @title = title.scan(/\d*\s(.*)\s\(\d*\)/).flatten[0]
  url_elements = url.split("/")
  @dat_url = "http://bg20.2ch.net/test/r.so/" + url_elements[2] + "/" + url_elements[5] + "/" + url_elements[6] + "/"
  @agent = Mechanize.new
  @page = @agent.get(@dat_url).body.toutf8

  @page = @page.split("\n").map{|res| dat_parse(res).flatten}.delete_if{|res| res == []}
  @page.map{|res| 
    res[6] = res[6].gsub("<>","").gsub("<br>","\n")
    unless res[5] == nil 
      res[5] = res[5].scan(/.*&gt;&gt;(\d+).*/).flatten[0]
      res[5] = ">>" + res[5] + res[6]
    else
      res[5] = res[6]
    end
    res.pop

  }
  @thre = @page.map{|res|
    {name: res[0],place: res[1], date: res[2], id: res[3], point: res[4],text: res[5]}
  }
end

Instance Attribute Details

#dat_urlObject

Returns the value of attribute dat_url.



50
51
52
# File 'lib/ruby2ch/datparse.rb', line 50

def dat_url
  @dat_url
end

#pageObject

Returns the value of attribute page.



50
51
52
# File 'lib/ruby2ch/datparse.rb', line 50

def page
  @page
end

#threObject

Returns the value of attribute thre.



50
51
52
# File 'lib/ruby2ch/datparse.rb', line 50

def thre
  @thre
end

#titleObject

Returns the value of attribute title.



50
51
52
# File 'lib/ruby2ch/datparse.rb', line 50

def title
  @title
end

Instance Method Details

#create_threadObject



40
41
42
# File 'lib/ruby2ch/datparse.rb', line 40

def create_thread
  return @thre[0][:date]
end

#dat_parse(res) ⇒ Object



36
37
38
# File 'lib/ruby2ch/datparse.rb', line 36

def dat_parse(res)
  res_array = res.scan(/(^\S+)\<\/b\>(\(.*\))\<b\>\<\>\S*\<\>(.+)\sID:(\S+)\s*(BE.*)*\<\>\s(\<a.*\<\/a\>)*(.*)/)
end

#impetusObject



44
45
46
# File 'lib/ruby2ch/datparse.rb', line 44

def impetus
  return @thre.size/(Time.now - Time.parse(create_thread )) * 60 * 60 * 24
end

#to_aObject



32
33
34
# File 'lib/ruby2ch/datparse.rb', line 32

def to_a
  @array = @page.split("\n")
end