OSDN Git Service

Print decoded form data.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / submit_window.py
index e46a4c5..b06ea91 100644 (file)
@@ -25,7 +25,6 @@ import urllib
 import urllib2
 import cookielib
 import os.path
-import time
 
 from BbsType import bbs_type_judge_uri
 from BbsType import bbs_type_exception
@@ -109,14 +108,6 @@ class WinWrap:
         self.entry_mail = self.widget_tree.get_widget("entry_mail")
         self.textbuffer = self.widget_tree.get_widget("textview").get_buffer()
 
-        self.post_dict = {}
-        self.post_dict["bbs"] = self.bbs_type.board
-        self.post_dict["key"] = self.bbs_type.thread
-        self.post_dict["time"] = str(int(time.time()))
-        self.post_dict["hana"] = "mogera"
-        self.post_dict["submit"] = u"\u66f8\u304d\u8fbc\u3080".encode(
-            "cp932", "replace")
-
         sigdic = {"on_submit_activate": self.on_submit_activate,
                   "on_close_activate": self.on_close_activate}
 
@@ -131,26 +122,22 @@ class WinWrap:
 
     def on_submit_activate(self, widget):
         name = self.entry_name.get_text()
-        if self.bbs_type.bbs_type == "2ch" \
-               and self.bbs_type.board == "morningcoffee" \
-               and not name:
-            name = u'\u540d\u7121\u3057\u52df\u96c6\u4e2d\u3002\u3002\u3002'
         mail = self.entry_mail.get_text()
         msg = self.textbuffer.get_text(
             self.textbuffer.get_start_iter(), self.textbuffer.get_end_iter())
 
-        self.post_dict["FROM"] = name.encode("cp932", "replace")
-        self.post_dict["mail"] = mail.encode("cp932", "replace")
-        self.post_dict["MESSAGE"] = msg.encode("cp932", "replace")
+        self.post_dict = self.bbs_type.build_post_dict(name, mail, msg)
+        self.post_dict = self.bbs_type.set_extra_post(self.post_dict)
 
         self.do_submit()
 
     def do_submit(self):
+        for name, value in self.post_dict.iteritems():
+            print "%s: %s" \
+                  % (name, value.decode(self.bbs_type.encoding, "replace"))
         post_encoded = urllib.urlencode(self.post_dict)
-        print post_encoded
 
-        req = urllib2.Request("http://" + self.bbs_type.host + "/test/bbs.cgi",
-                              post_encoded)
+        req = urllib2.Request(self.bbs_type.get_post_uri(), post_encoded)
         req.add_header("Referer", self.uri)
 
         res = opener.open(req)
@@ -158,12 +145,27 @@ class WinWrap:
 
     def on_response(self, response):
         data = response.read()
-
+        info = response.info()
+        if "Content-Type" in info:
+            import re
+            match = re.search(
+                "charset=(?P<charset>[a-zA-Z0-9_\-]+)", info["Content-Type"])
+            if match:
+                charset = match.group("charset").lower()
+
+        if charset in ("x-sjis", "x_sjis", "sjis", "shiftjis", "shift-jis",
+                       "shift_jis", "s-jis", "s_jis"):
+            encoding = "cp932"
+        elif charset in ("euc-jp", "euc_jp", "eucjp"):
+            encoding = "euc-jp"
+
+        if encoding:
+            data = data.decode(encoding, "replace")
         p = ConfirmationHTMLParser()
-        p.feed(data.decode("cp932", "replace"))
+        p.feed(data)
         p.close()
 
-        print data.decode("cp932")
+        print data
 
         window = gtk.Window()
         if not p.complete:
@@ -178,9 +180,11 @@ class WinWrap:
             if "type" in input and input["type"] == "submit":
                 button = gtk.Button(input["value"])
                 button.connect("clicked",
-                               self.on_button_submit_clicked, p.inputs)
+                               lambda widget: self.on_button_submit_clicked(
+                    widget, p.inputs, encoding))
                 anchor = buf.create_child_anchor(buf.get_end_iter())
                 textview.add_child_at_anchor(button, anchor)
+                button.grab_focus()
                 break
 
         window.add(textview)
@@ -194,13 +198,15 @@ class WinWrap:
 
             gobject.timeout_add(2 * 1000, on_timeout, window)
 
-    def on_button_submit_clicked(self, widget, inputs=None):
+    def on_button_submit_clicked(self, widget, inputs, encoding):
         widget.get_toplevel().destroy()
         self.post_dict = {}
         for input in inputs:
             if "name" in input and "value" in input:
                 name = input["name"]
                 value = input["value"]
-                self.post_dict[name] = value.encode("cp932", "replace")
+                if encoding:
+                    value = value.encode(encoding, "replace")
+                self.post_dict[name] = value
 
         self.do_submit()