1 # Copyright (C) 2006 by Aiwota Programmer
2 # aiwotaprog@tetteke.tk
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.
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.
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
22 from misc import get_board_subjecttxt_path
23 from misc import get_board_idx_path
24 from http_sub import HTTPRedirectHandler302
26 REG_EXPR = re.compile("(?P<id>.*).dat<>(?P<title>.*)\((?P<res>\d*)\)")
29 m = REG_EXPR.match(line)
32 title = m.group("title")
34 res = int(m.group("res"))
39 def analyze_subjecttxt(subjecttxt, func):
40 lines = subjecttxt.splitlines()
44 def load_subjecttxt(bbs, board, func):
45 subjecttxt_path = get_board_subjecttxt_path(bbs, board)
46 if not os.path.exists(subjecttxt_path):
49 f = open(subjecttxt_path)
50 subjecttxt_encoded = f.read()
53 analyze_subjecttxt(subjecttxt_encoded.decode("cp932", "replace"), func)
55 def get_subjecttxt(bbs, board, uri):
59 opener = urllib2.build_opener(HTTPRedirectHandler302)
60 response = opener.open(uri)
61 info = response.info()
63 subjecttxt_encoded = response.read()
66 subjecttxt_path = get_board_subjecttxt_path(bbs, board)
68 basedir = os.path.dirname(subjecttxt_path)
69 if not os.path.isdir(basedir):
72 f = open(subjecttxt_path, "w")
73 f.write(subjecttxt_encoded)
77 if "Last-Modified" in info:
78 lastmod = info["Last-Modified"]
79 save_board_idx(bbs, board, lastmod)
81 return subjecttxt_encoded.decode("cp932", "replace"), lastmod
83 def load_board_idx(bbs, board):
85 boardidxfile = get_board_idx_path(bbs, board)
86 if os.path.exists(boardidxfile):
87 for line in file(boardidxfile):
88 if line.startswith("lastModified="):
89 lastmod = line[len("lastModified="):].rstrip("\n")
93 def save_board_idx(bbs, board, lastmod):
97 boardidx_path = get_board_idx_path(bbs, board)
98 basedir = os.path.dirname(boardidx_path)
99 if not os.path.isdir(basedir):
102 f = file(boardidx_path, "w")
103 f.write("lastModified=" + lastmod + "\n")