OSDN Git Service

Print decoded form data.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / submit_window.py
index fa06afa..b06ea91 100644 (file)
@@ -132,8 +132,10 @@ class WinWrap:
         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(self.bbs_type.get_post_uri(), post_encoded)
         req.add_header("Referer", self.uri)
@@ -143,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:
@@ -163,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)
@@ -179,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()