From e767eb15b9cf477a3c01bf8ffe8bee35691bd955 Mon Sep 17 00:00:00 2001 From: Aiwota Programmer Date: Thu, 24 Aug 2006 00:04:46 +0900 Subject: [PATCH] Not redirect when http 302 Found. --- src/Hage1/http_sub.py | 26 ++++++++++++++++++++++++++ src/Hage1/subjecttxtfile.py | 15 +++++++++++---- src/Hage1/thread_window.py | 4 +++- 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 src/Hage1/http_sub.py diff --git a/src/Hage1/http_sub.py b/src/Hage1/http_sub.py new file mode 100644 index 0000000..25f8130 --- /dev/null +++ b/src/Hage1/http_sub.py @@ -0,0 +1,26 @@ +# Copyright (C) 2006 by Aiwota Programmer +# aiwotaprog@tetteke.tk +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + +from urllib2 import HTTPRedirectHandler +from urllib2 import HTTPError + + +class HTTPRedirectHandler302(HTTPRedirectHandler): + """Not process 302""" + + def http_error_302(self, req, fp, code, msg, hdrs): + raise HTTPError(req.get_full_url(), code, msg, hdrs, fp) diff --git a/src/Hage1/subjecttxtfile.py b/src/Hage1/subjecttxtfile.py index 8f20faf..49b92f3 100644 --- a/src/Hage1/subjecttxtfile.py +++ b/src/Hage1/subjecttxtfile.py @@ -18,9 +18,10 @@ import re import os.path import codecs -import urllib +import urllib2 from misc import get_board_subjecttxt_path from misc import get_board_idx_path +from http_sub import HTTPRedirectHandler302 REG_EXPR = re.compile("(?P.*).dat<>(?P.*)\((?P<res>\d*)\)") @@ -52,10 +53,16 @@ def load_subjecttxt(bbs, board, func): analyze_subjecttxt(subjecttxt_encoded.decode("cp932", "replace"), func) def get_subjecttxt(bbs, board, uri): - u = urllib.urlopen(uri) - subjecttxt_encoded = u.read() - info = u.info() + # get subject.txt + + opener = urllib2.build_opener(HTTPRedirectHandler302) + response = opener.open(uri) + info = response.info() + print info + subjecttxt_encoded = response.read() + + # save subject.txt subjecttxt_path = get_board_subjecttxt_path(bbs, board) basedir = os.path.dirname(subjecttxt_path) diff --git a/src/Hage1/thread_window.py b/src/Hage1/thread_window.py index 9ba6261..025f339 100644 --- a/src/Hage1/thread_window.py +++ b/src/Hage1/thread_window.py @@ -31,6 +31,7 @@ import barehtmlparser import idxfile import session import board_window +from http_sub import HTTPRedirectHandler302 GLADE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "..", "data") @@ -116,7 +117,8 @@ class WinWrap: req.add_header("If-None-Match", etag) print req.headers - res = urllib2.urlopen(req) + opener = urllib2.build_opener(HTTPRedirectHandler302) + res = opener.open(req) headers = res.info() print headers -- 2.11.0