Module: Simple2ch

Defined in:
lib/simple2ch.rb,
lib/simple2ch/dat.rb,
lib/simple2ch/res.rb,
lib/simple2ch/thre.rb,
lib/simple2ch/board.rb,
lib/simple2ch/version.rb,
lib/simple2ch/simple2ch_exception.rb

Defined Under Namespace

Classes: Board, Dat, DatParseException, KakoLogException, NoThreGivenException, NotA2chUrlException, Res, Simple2chException, Thre

Constant Summary collapse

DEBUG =
false
VERSION =
"0.1.6"
@@boards =

Module variables

{}

Class Method Summary collapse

Class Method Details

.boards(bbsmenu_url = nil, force_refresh: nil) ⇒ Array<Simple2ch::Board>

bbsmenuのURLが渡されればセットして,板リストを返す

Parameters:

  • bbsmenu_url (String) (defaults to: nil)

    bbs_menuのURL

  • [Boolean] (Hash)

    a customizable set of options

Returns:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/simple2ch.rb', line 43

def self.boards(bbsmenu_url=nil, force_refresh:nil)
  if bbsmenu_url
    bbsmenu_urls = {
      net: 'http://menu.2ch.net/bbsmenu.html', sc: 'http://2ch.sc/bbsmenu.html', open: 'http://open2ch.net/menu/pc_menu.html'
    }
    # http://www.rubular.com/r/u1TJbQAULD
    board_extract_regex = /<A HREF=http:\/\/(?<subdomain>\w+).(?<openflag>open|)2ch.(?<tld>sc|net)\/(?<board_name>\w+)\/>(?<board_name_ja>.+)<\/A>/
    type_of_2ch = self.type_of_2ch(bbsmenu_url)

    if force_refresh || (boards=@@boards.fetch(type_of_2ch, [])).size == 0
      prepared_bbsmenu_url = bbsmenu_urls[type_of_2ch]

      data = nil
      boards_array = []

      raise RuntimeError, "Failed to fetch #{url}" if (data = fetch(URI.parse(prepared_bbsmenu_url), type_of_2ch)).empty?
      raise RuntimeError, "Failed to parse #{url}" if (boards_array=data.scan(board_extract_regex).uniq).empty?

      boards_array.each do |b|
        boards << Simple2ch::Board.new(b[4],"http://#{b[0]}.#{b[1]}2ch.#{b[2]}/#{b[3]}/")
      end
      @@boards[type_of_2ch] = boards
    else
      @@boards[type_of_2ch]
    end
  end
end

.fetch(url, site = :sc) ⇒ String

HTTPでGETする

Parameters:

  • url (URI)

    URL

  • site (Symbol) (defaults to: :sc)

    :net, :sc, :openのいずれか.(2ch.net or 2ch.sc or open2ch.net)

Returns:

  • (String)

    取得本文



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/simple2ch.rb', line 27

def self.fetch(url, site=:sc)
  res = OpenURI.open_uri(url){|text| text.read }
  case site
    when :net, :sc
      res.force_encoding("cp932").encode!('utf-8', :undef => :replace)
    when :open
      res.force_encoding("utf-8")
    else
      raise RuntimeError, "Invalid type of 2ch was given: #{site}"
  end
end

.parse_url(url) ⇒ Array<String>

URLを分解する

Parameters:

  • url (String)

    URL

Returns:

  • (Array<String>)

    結果(thread_key等が該当無しの場合,nilを返す)

Raises:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/simple2ch.rb', line 93

def self.parse_url(url)
  # http://www.rubular.com/r/h63xdfmQIH
  case url
    when /http:\/\/(?<server_name>.+)\.(?<openflag>open)?2ch.(?<tld>net|sc)\/test\/read.cgi\/(?<board_name>.+)\/(?<thread_key>[0-9]+)/,
        /http:\/\/(?<server_name>.+)\.(?<openflag>open)?2ch.(?<tld>net|sc)\/(?<board_name>.+)\/subject\.txt/,
        /http:\/\/(?<server_name>.+)\.(?<openflag>open)?2ch\.(?<tld>net|sc)\/(?<board_name>.+)\//,
        /http:\/\/(?<server_name>.+)\.(?<openflag>open)?2ch\.(?<tld>net|sc)\/(?<board_name>\w+)/,
        /http:\/\/(?<server_name>.+)\.(?<openflag>open)?2ch.(?<tld>net|sc)\/(.+)\/dat\/(?<thread_key>[0-9]+)\.dat/,
        /http:\/\/(?:(?<server_name>\w*)\.)?(?<openflag>open)?2ch\.(?<tld>sc|net)/
      { server_name: ($~[:server_name] rescue nil),
        board_name: ($~[:board_name] rescue nil),
        openflag: ($~[:openflag] rescue nil),
        tld: $~[:tld],
        thread_key: ($~[:thread_key] rescue nil)
      }
    else
      raise NotA2chUrlException, "Given URL: #{url}"
  end
end

.rootObject



16
17
18
# File 'lib/simple2ch.rb', line 16

def self.root
  File.dirname __dir__
end

.type_of_2ch(url) ⇒ Symbol

2chのタイプを返す

Parameters:

  • url (String)

    URL

Returns:

  • (Symbol)

    :open or :net or :sc



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

def self.type_of_2ch(url)
  parsed_url = self.parse_url(url)
  openflag = parsed_url[:openflag]
  tld = parsed_url[:tld]
  if openflag && tld=='net'
    :open
  elsif !openflag && tld=='net'
    :net
  elsif !openflag && tld=='sc'
    :sc
  else
    nil
  end
end