OSDN Git Service

Use bbs type globally. support jbbs.
[fukui-no-namari/fukui-no-namari.git] / src / FukuiNoNamari / submit_window.py
index fa06afa..3b2985b 100644 (file)
@@ -143,12 +143,27 @@ class WinWrap:
 
     def on_response(self, response):
         data = response.read()
 
     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 = ConfirmationHTMLParser()
-        p.feed(data.decode("cp932", "replace"))
+        p.feed(data)
         p.close()
 
         p.close()
 
-        print data.decode("cp932")
+        print data
 
         window = gtk.Window()
         if not p.complete:
 
         window = gtk.Window()
         if not p.complete:
@@ -163,9 +178,11 @@ class WinWrap:
             if "type" in input and input["type"] == "submit":
                 button = gtk.Button(input["value"])
                 button.connect("clicked",
             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)
                 anchor = buf.create_child_anchor(buf.get_end_iter())
                 textview.add_child_at_anchor(button, anchor)
+                button.grab_focus()
                 break
 
         window.add(textview)
                 break
 
         window.add(textview)
@@ -179,13 +196,15 @@ class WinWrap:
 
             gobject.timeout_add(2 * 1000, on_timeout, window)
 
 
             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"]
         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()
 
         self.do_submit()