OSDN Git Service

b4acb5191351dcaec8f03ee10733991814795a17
[fukui-no-namari/fukui-no-namari.git] / src / Hage1 / BbsType / bbs_type_2ch.py
1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 import re
19
20 from bbs_type_exception import BbsTypeError
21
22
23 _base_reg_expr = re.compile("http://(?P<host>[^./]+\.2ch\.net)/(?P<board>[^/]+)/$")
24 _cgi_reg_expr = re.compile("http://(?P<host>[^./]+\.2ch\.net)/test/read.cgi/(?P<board>[^/]+)/(?P<thread>[^/]+)/.*")
25
26
27 class Type2ch:
28
29     def __init__(self, host=None, board=None, thread=None, uri=None):
30         if uri:
31             self._parse_uri(uri)
32         else:
33             if not host or not board:
34                 raise BbsTypeError, "host and board, or uri must be specified"
35             self.uri = uri
36             self.host = host
37             self.board = board
38             self.thread = thread
39
40     def _parse_uri(self, uri):
41         self.uri = uri
42         self.bbs_type = None
43         self.host = None
44         self.board = None
45         self.thread = None
46
47         m = _base_reg_expr.match(self.uri)
48         if m:
49             self.bbs_type = "2ch"
50             self.host = m.group("host")
51             self.board = m.group("board")
52         else:
53             m = _cgi_reg_expr.match(self.uri)
54             if m:
55                 self.bbs_type = "2ch"
56                 self.host = m.group("host")
57                 self.board = m.group("board")
58                 self.thread = m.group("thread")
59
60     def is_board(self):
61         return not self.thread
62
63     def is_thread(self):
64         return self.thread
65
66     def clone_with_thread(self, thread):
67         if not thread:
68             raise ValueError, "parameter must not be empty"
69         return Type2ch(self.host, self.board, thread)
70
71     def get_uri_base(self):
72         return "http://" + self.host + "/" + self.board + "/"
73
74     def get_subject_txt_uri(self):
75         return self.get_uri_base() + "subject.txt"
76
77     def get_dat_uri(self):
78         if not self.thread:
79             raise BbsTypeError, "not specified thread"
80         return self.get_uri_base() + "dat/" + self.thread + ".dat"
81
82     def get_thread_uri(self):
83         if not self.thread:
84             raise BbsTypeError, "not specified thread"
85         return "http://" + self.host + "/test/read.cgi/" + \
86                   self.board + "/" + self.thread + "/"