Module: Twoch::Channel

Defined in:
lib/twoch/channel.rb

Class Method Summary collapse

Class Method Details

.get_meaningsObject

:nodoc:



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/twoch/channel.rb', line 119

def get_meanings #:nodoc:
  meanings = Array.new
  @agent.page.search('li div').each do |div|
    lis = div.search('li')
    if lis.size != 0
      meanings << lis.inject('') { |text, li| text + "  " + li.inner_text + "\n" }
    else
      meanings << "  " + div.inner_text
    end
  end
  return meanings
end

.get_search_assistancesObject

:nodoc:



105
106
107
108
109
110
111
# File 'lib/twoch/channel.rb', line 105

def get_search_assistances #:nodoc:
  assistances = []
  @agent.page.search('div.sas strong').map do |h|
    assistances << h.inner_text.chomp("\t\t\t\t\t\t")
  end
  assistances
end

.get_titlesObject

:nodoc:



113
114
115
116
117
# File 'lib/twoch/channel.rb', line 113

def get_titles #:nodoc:
  return @agent.page.search('li span.midashi').map do |midashi|
    inner_text = midashi.inner_text
  end
end

.make_query_from_words(words) ⇒ Object

:nodoc:



101
102
103
# File 'lib/twoch/channel.rb', line 101

def make_query_from_words(words) #:nodoc:
  return words.map { |param| URI.encode(param) }.join('+')
end

.make_thread_url(board_url, thread_id) ⇒ Object



70
71
72
73
# File 'lib/twoch/channel.rb', line 70

def make_thread_url(board_url, thread_id)
  m = /http:\/\/.*?\//.match board_url
  board_url.dup.insert(m[0].length, "bbs/read.cgi/") + thread_id
end

.make_uri_from_words(words) ⇒ Object

:nodoc:



97
98
99
# File 'lib/twoch/channel.rb', line 97

def make_uri_from_words(words) #:nodoc:
  return BASE_URI + make_query_from_words(words) + CHAR_CODE
end

.match_search_condition?(text, search_words) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
# File 'lib/twoch/channel.rb', line 90

def match_search_condition?(text, search_words)
  search_words.each do |word|
    return true if text =~ /#{word}/
  end
  false
end

.search(words) ⇒ Object

:nodoc:



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/twoch/channel.rb', line 7

def search(words) #:nodoc:
  @agent.get(make_uri_from_words(words))
  @words = words
  result = ""
  get_search_assistances.each do |assistance|
    result << set_color(assistance, :green) + "\n"
  end
  
  meanings = get_meanings
  get_titles.each_with_index do |title, index|
    result << set_color(title, :yellow) + "\n"
    result << set_color(meanings[index]) + "\n"
  end
  result
end

.search_board(words) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/twoch/channel.rb', line 28

def search_board(words)
  url = "http://www2.2ch.net/bbsmenu.html"
  a = Mechanize.new
  a.get(url)
  boards = []
  a.page.search('a').map do |e|
    if words == [] || match_search_condition?(e.inner_text, words)
      boards << {:title => e.inner_text, :url => e[:href]}
    end
  end

  boards.each_with_index do |board, i|
    puts "#{i} : #{board[:title]}(#{board[:url]})"
  end

  print "input board no >"
  no = STDIN.gets.to_i
  show_thread_list(boards[no][:url])
end

.set_color(str, color) ⇒ Object



23
24
25
# File 'lib/twoch/channel.rb', line 23

def set_color(str, color)
  Thor::Shell::Color.new.set_color(str, color)
end

.show_thread(thread_url) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/twoch/channel.rb', line 75

def show_thread(thread_url)
  puts thread_url
  a = Mechanize.new
  a.get(thread_url)
  comments = []
  a.page.search('dt').map do |dt|
    dttext = dt.inner_text.gsub(/\n/, "")
    puts set_color(dttext, :yellow) 
    dd = dt.next
    puts dd.inner_text
    puts
  end

end

.show_thread_list(board_url) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/twoch/channel.rb', line 48

def show_thread_list(board_url)
  thread_list_url = board_url + "subback.html"
  a = Mechanize.new
  a.get(thread_list_url)
  threads = []
  a.page.search('a').map do |e|
    /\d+:\s+(.*)$/ =~ e.inner_text
    title = $1
    pp e
    threads << {:title => title, :url => e[:href]}
  end

  threads.each_with_index do |thread, i|
    puts "#{i} : #{thread[:title]}(#{thread[:url]})"
  end

  print "input thread no >"
  no = STDIN.gets.to_i
  thread_url = make_thread_url(board_url, threads[no][:url])
  show_thread(thread_url)
end