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.7"
@@boards =

Module variables

{}

Class Method Summary collapse

Class Method Details

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

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/simple2ch.rb', line 39

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/bbsmenu.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))).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
    end
  end
  @@boards[type_of_2ch]
end

.fetch(url) ⇒ String

HTTPでGETする



26
27
28
29
30
31
32
33
# File 'lib/simple2ch.rb', line 26

def self.fetch(url)
  encode = if url.to_s.index('subject.txt') || url.to_s.index('.dat') || url.to_s.index('bbsmenu')
                  'SHIFT_JIS'
                else
                  'UTF-8'
                end
  OpenURI.open_uri(url, 'r:binary').read.force_encoding(encode).encode('utf-8', undef: :replace, invalid: :replace, replace: '〓')
end

.parse_url(url) ⇒ Array<String>

URLを分解する

Raises:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/simple2ch.rb', line 89

def self.parse_url(url)
  # http://www.rubular.com/r/h63xdfmQIH
  case url.to_s
    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>.*)\.)?(?:(?<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のタイプを返す

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/simple2ch.rb', line 70

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
    raise NotA2chUrlException, "Given URL: #{url}"
  end
end