OSDN Git Service

support for jpeg file in markupper.py
authorhylom <hylom@hylom.net>
Thu, 14 Jun 2012 04:15:42 +0000 (13:15 +0900)
committerhylom <hylom@hylom.net>
Thu, 14 Jun 2012 04:15:42 +0000 (13:15 +0900)
count_fig.py [new file with mode: 0755]
deterfile.py
getjpggeom.py [new file with mode: 0644]
imgfind.py [new file with mode: 0755]
markupper.py
otpuploader.py [changed mode: 0644->0755]
poster/__init__.pyc
poster/encode.pyc
poster/streaminghttp.pyc

diff --git a/count_fig.py b/count_fig.py
new file mode 100755 (executable)
index 0000000..f708493
--- /dev/null
@@ -0,0 +1,44 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import re
+import os.path
+import sys
+import codecs
+
+fh_in  = codecs.getreader('utf_8')(sys.stdin)
+fh_out = codecs.getwriter('utf_8')(sys.stdout)
+sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
+
+rex1 = re.compile(ur"^☆図([0-9]+)\s+")
+rex2 = re.compile(ur"\*図([0-9]+)")
+rex3 = re.compile(ur"\*図([0-9]+)[〜~]([0-9]+)")
+rex4 = re.compile(ur"\*図([0-9]+)、([0-9]+)")
+
+counter_ref = 1
+counter_fig = 1
+for l in fh_in:
+    m1 = rex1.match(l)
+    m2 = rex2.search(l)
+    m3 = rex3.search(l)
+    m4 = rex4.search(l)
+
+    if m1:
+        l = rex1.sub(ur"☆図%d " % (counter_ref,), l)
+        counter_ref += 1;
+    elif m3:
+        ref1 = int(m3.group(1))
+        ref2 = int(m3.group(2))
+        l = rex3.sub(ur"*図%d~%d" % (counter_fig, counter_fig+ref2-ref1),  l)
+        counter_fig += ref2 - ref1 + 1
+    elif m4:
+        ref1 = int(m4.group(1))
+        ref2 = int(m4.group(2))
+        l = rex4.sub(ur"*図%d、%d" % (counter_fig, counter_fig+ref2-ref1),  l)
+        counter_fig += ref2 - ref1 + 1
+    elif m2:
+        l = rex2.sub(ur"*図%d" % (counter_fig,), l)
+        counter_fig += 1;
+    fh_out.write(l)
+
+
index c553e10..2d4c0a2 100644 (file)
@@ -1,40 +1,40 @@
-# deterfile.py\r
-"""deterfile.py - determine file type"""\r
-\r
-import os\r
-import os.path\r
-import re\r
-\r
-_file_cmd = "file %s"\r
-\r
-def file(path):\r
-    """\r
-    determine given file's type.\r
-    This function returns strings.\r
-\r
-    @param path: filepath you want to determine\r
-    @type path: string\r
-    """\r
-    \r
-    if not os.path.isfile(path):\r
-        return ("file not exist.",)\r
-\r
-    cmd = _file_cmd % path\r
-    escaped_path = path.replace("\\", "\\\\");\r
-    stdout = os.popen(cmd, "r")\r
-    results = []\r
-    for line in stdout:\r
-        if re.search(r"^%s:" % escaped_path, line):\r
-            line = re.sub(r"^%s:\s*" % escaped_path, "", line)\r
-        tpl = line.strip().split(",")\r
-        results.extend([x.strip() for x in tpl])\r
-    stdout.close()\r
-    return results\r
-\r
-#### test code\r
-# import sys\r
-\r
-# tgt = sys.argv[1]\r
-# r = file(tgt)\r
-# for item in r:\r
-#     print item\r
+# deterfile.py
+"""deterfile.py - determine file type"""
+
+import os
+import os.path
+import re
+import getjpggeom
+
+_file_cmd = "file %s"
+
+def file(path):
+    """
+    determine given file's type.
+    This function returns strings.
+
+    @param path: filepath you want to determine
+    @type path: string
+    """
+    if not os.path.isfile(path):
+        return ("file not exist.",)
+
+    cmd = _file_cmd % path
+    escaped_path = path.replace("\\", "\\\\");
+    stdout = os.popen(cmd, "r")
+    results = []
+    for line in stdout:
+        if re.search(r"^%s:" % escaped_path, line):
+            line = re.sub(r"^%s:\s*" % escaped_path, "", line)
+        tpl = line.strip().split(",")
+        results.extend([x.strip() for x in tpl])
+    stdout.close()
+    return results
+
+#### test code
+# import sys
+
+# tgt = sys.argv[1]
+# r = file(tgt)
+# for item in r:
+#     print item
diff --git a/getjpggeom.py b/getjpggeom.py
new file mode 100644 (file)
index 0000000..425b81f
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/python
+import struct
+
+def get_jpeg_geometory(filename):
+    jpeg = open(filename, 'r')
+    jpeg.read(2)
+    b = jpeg.read(1)
+    try:
+        while (b and ord(b) != 0xDA):
+            while (ord(b) != 0xFF): b = jpeg.read(1)
+            while (ord(b) == 0xFF): b = jpeg.read(1)
+            if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
+                jpeg.read(3)
+                h, w = struct.unpack(">HH", jpeg.read(4))
+                break
+            else:
+                jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)
+            b = jpeg.read(1)
+        width = int(w)
+        height = int(h)
+        return (width, height)
+    except struct.error:
+        return (None, None)
+    except ValueError:
+        return (None, None)
+
+if __name__ == '__main__':
+    pass
+#(w, h) = get_jpeg_geometory('test.jpg')
+#print w, h
+
+
diff --git a/imgfind.py b/imgfind.py
new file mode 100755 (executable)
index 0000000..5597ce0
--- /dev/null
@@ -0,0 +1,30 @@
+#!/usr/bin/python
+
+import re
+import os.path
+
+fh_in = open("txt.txt", "r")
+#fh_out = open("mv.sh", "w")
+dir_from = "img_old"
+dir_dest = "img"
+rex_img1 = re.compile(r"^<(.*),\s*(.*)>\s*")
+rex_img2 = re.compile(r"^<(.*)>\s*")
+
+
+imgs = []
+for l in fh_in:
+    m1 = rex_img1.match(l)
+    m2 = rex_img2.match(l)
+
+    if m1:
+        imgs.append(m1.group(1))
+        imgs.append(m1.group(2))
+    elif m2:
+        imgs.append(m2.group(1))
+
+for f in imgs:
+    if not os.path.exists(os.path.join(dir_from, f)):
+        print "# %s/%s is not exists" % (dir_from, f)
+       print "# mv %s/%s %s/" % (dir_from, f, dir_dest)
+    else:
+        print "mv %s/%s %s/" % (dir_from, f, dir_dest)
index dd3e4e9..f3060b0 100644 (file)
@@ -9,6 +9,7 @@ import pickle
 
 import HTMLTagFilter
 import deterfile
+import getjpggeom
 
 #sys.stdin = codecs.getreader('utf_8')(sys.stdin)
 #sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
@@ -126,6 +127,9 @@ class Markupper(object):
             elif re.search(ur"^○", line):
                 self._head_m(line)
                 continue
+            elif re.search(ur"^◇", line):
+                self._head_s(line)
+                continue
             elif re.search(ur"^☆----", line):
                 self._newpage(line)
                 continue
@@ -193,6 +197,20 @@ class Markupper(object):
             line = re.sub(ur"^○(.*)$", ur"<h4>\1</h4>", line)
         print line
 
+    def _head_s(self, line):
+        line = line.rstrip()
+        if re.search(ur"\*{[a-zA-Z0-9_]*}\s*$", line):
+            self._anchor = re.search(ur"\*\{([a-zA-Z0-9_]*)\}\s*$", line).group(1)
+            line = re.sub(ur"\s*\*\{[a-zA-Z0-9_]*\}\s*$", "", line)
+
+        line = self._default_markup_rule(line)
+        if self._anchor != "":
+            line = re.sub(ur"^◇(.*)$", ur'<div id="%s"><h5>\1</h5></div>' % self._anchor, line)
+            self._anchor = ""
+        else:
+            line = re.sub(ur"^◇(.*)$", ur"<h5>\1</h5>", line)
+        print line
+
     def _paragraph(self, line):
         line = self._default_markup_rule(line)
         line = "<p>" + line + "</p>"
@@ -272,6 +290,7 @@ class Markupper(object):
         # apply filter
         # line = tag_filter.apply(line)
 
+        line = re.sub(ur"[★*](動画[0-9〜~、]+)", ur"<b>\1</b>", line)
         line = re.sub(ur"[★*](表[0-9〜~、]+)", ur"<b>\1</b>", line)
         line = re.sub(ur"[★*](図[0-9〜~、]+)", ur"<b>\1</b>", line)
         line = re.sub(ur"[★*](写真[0-9〜~、]+)", ur"<b>\1</b>", line)
@@ -340,7 +359,7 @@ class Markupper(object):
             str_title = re.search(ur"^☆(リスト.*)$", line).group(1)
         except AttributeError:
             str_title = ""
-        print "<p><b>%s</b></p>" % (str_title)
+        print "<p class='caption'><b>%s</b></p>" % (str_title)
         print self._list_start()
 
         for line in self.input_iter:
@@ -457,11 +476,16 @@ class Markupper(object):
     """ % (cap)
 
     def _get_png_geom(self, filepath):
-        desc = deterfile.file(filepath)
+        s = filepath.split('.')
+        ext = s[-1]
+        if (ext == 'JPG') or (ext == 'jpg'):
+            (w, h) = getjpggeom.get_jpeg_geometory(filepath)
+            return (w, h)
+        
         try:
             m = re.match(r"([0-9]+)\s*x\s*([0-9]+)", desc[1])
         except IndexError:
-            err = ",".join(desc)
+            err = ", ".join(desc)
             raise Exception("deterfile error: %s, file: %s . " % (err,filepath))
         if m:
             w = m.group(1)
old mode 100644 (file)
new mode 100755 (executable)
index 7f2937f..92cce50
@@ -1,80 +1,81 @@
-# otpuploader.py\r
-# -*- coding: utf-8 -*-\r
-"""otpuploader.py - OpenTechPress Attachment Uploader"""\r
-\r
-from poster.encode import multipart_encode\r
-from poster.streaminghttp import register_openers\r
-import urllib\r
-import urllib2\r
-import cookielib\r
-\r
-class OtpUploader(object):\r
-    "OpenTechPress Attachment Uploader"\r
-    def __init__(self):\r
-        self.set_attach_url()\r
-\r
-    def set_attach_url(self, url=""):\r
-        if url == "":\r
-            url = "http://magazine.sourceforge.jp/fileadmin.pl"\r
-        self._attach_url = url\r
-\r
-    def login(self, username, passwd):\r
-        c = cookielib.CookieJar()\r
-        p = urllib2.HTTPCookieProcessor(c)\r
-        opener = urllib2.build_opener(p)\r
-\r
-        login_url = "http://magazine.sourceforge.jp/login.pl"\r
-        params = urllib.urlencode({\r
-            "op": "userlogin",\r
-            "unickname": username,\r
-            "upasswd": passwd,\r
-            "userlogin": u"ログイン".encode("utf-8")\r
-            })\r
-        req = opener.open(login_url, params)\r
-        self._cookie = c\r
-\r
-    def post_attachment(self, sid, filename):\r
-        params = {\r
-            "file_content": open(filename, "rb"),\r
-            "description": "",\r
-            "op": "addFileForStory",\r
-            "sid": sid,\r
-            "Submit": "Submit"\r
-            }\r
-        opener = register_openers()\r
-        opener.add_handler(urllib2.HTTPCookieProcessor(self._cookie))\r
-\r
-        (datagen, headers) = multipart_encode(params)\r
-        request = urllib2.Request(self._attach_url, datagen, headers)\r
-        res = opener.open(request)\r
-        #res = urllib2.urlopen(request)\r
-        #print res.read()\r
-\r
-if __name__ == "__main__":\r
-    import getpass\r
-    import getopt\r
-    import sys\r
-\r
-    (opts, args) = getopt.getopt(sys.argv[1:], "u:")\r
-    opt_dict = dict(opts)\r
-    print opt_dict\r
-    print args\r
-\r
-    if not "-u" in opt_dict:\r
-        uname = raw_input("Username: ")\r
-    else:\r
-        uname = opt_dict["-u"]\r
-\r
-    if len(args) < 2:\r
-        sys.exit("usage: cmd sid <filename>")\r
-    sid = args.pop(0)\r
-\r
-    passwd = getpass.getpass()\r
-    u = OtpUploader()\r
-    u.login(uname, passwd)\r
-    print "cookie: ", u._cookie\r
-\r
-    for f in args:\r
-        print "post %s to sid %s" % (f, sid)\r
-        u.post_attachment(sid, f)\r
-\r
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+# otpuploader.py
+"""otpuploader.py - OpenTechPress Attachment Uploader"""
+
+from poster.encode import multipart_encode
+from poster.streaminghttp import register_openers
+import urllib
+import urllib2
+import cookielib
+
+class OtpUploader(object):
+    "OpenTechPress Attachment Uploader"
+    def __init__(self):
+        self.set_attach_url()
+
+    def set_attach_url(self, url=""):
+        if url == "":
+            url = "http://magazine.sourceforge.jp/fileadmin.pl"
+        self._attach_url = url
+
+    def login(self, username, passwd):
+        c = cookielib.CookieJar()
+        p = urllib2.HTTPCookieProcessor(c)
+        opener = urllib2.build_opener(p)
+
+        login_url = "http://magazine.sourceforge.jp/login.pl"
+        params = urllib.urlencode({
+            "op": "userlogin",
+            "unickname": username,
+            "upasswd": passwd,
+            "userlogin": u"ログイン".encode("utf-8")
+            })
+        req = opener.open(login_url, params)
+        self._cookie = c
+
+    def post_attachment(self, sid, filename):
+        params = {
+            "file_content": open(filename, "rb"),
+            "description": "",
+            "op": "addFileForStory",
+            "sid": sid,
+            "Submit": "Submit"
+            }
+        opener = register_openers()
+        opener.add_handler(urllib2.HTTPCookieProcessor(self._cookie))
+
+        (datagen, headers) = multipart_encode(params)
+        request = urllib2.Request(self._attach_url, datagen, headers)
+        res = opener.open(request)
+        #res = urllib2.urlopen(request)
+        #print res.read()
+
+if __name__ == "__main__":
+    import getpass
+    import getopt
+    import sys
+
+    (opts, args) = getopt.getopt(sys.argv[1:], "u:")
+    opt_dict = dict(opts)
+    print opt_dict
+    print args
+
+    if not "-u" in opt_dict:
+        uname = raw_input("Username: ")
+    else:
+        uname = opt_dict["-u"]
+
+    if len(args) < 2:
+        sys.exit("usage: cmd sid <filename>")
+    sid = args.pop(0)
+
+    passwd = getpass.getpass()
+    u = OtpUploader()
+    u.login(uname, passwd)
+    print "cookie: ", u._cookie
+
+    for f in args:
+        print "post %s to sid %s" % (f, sid)
+        u.post_attachment(sid, f)
+
index ce48cf7..e532c3c 100644 (file)
Binary files a/poster/__init__.pyc and b/poster/__init__.pyc differ
index 727a923..3d4e54d 100644 (file)
Binary files a/poster/encode.pyc and b/poster/encode.pyc differ
index f4590e7..2e185ae 100644 (file)
Binary files a/poster/streaminghttp.pyc and b/poster/streaminghttp.pyc differ