• R/O
  • HTTP
  • SSH
  • HTTPS

提交

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

sfjplib for python


Commit MetaInfo

修訂b5561a04bfec320b5c99de8c76e25c0e7d05be85 (tree)
時間2011-08-25 20:33:18
作者Hiromichi MATSUSHIMA <hirom@offi...>
CommiterHiromichi MATSUSHIMA

Log Message

add Wiki support

Change Summary

差異

--- a/crawlerlib.py
+++ b/crawlerlib.py
@@ -63,7 +63,7 @@ class Crawler(object):
6363 p = urllib2.HTTPCookieProcessor(c)
6464 opener = urllib2.build_opener(p)
6565
66- res = opener.open(url, params)
66+ res = opener.open(url, urllib.urlencode(params))
6767 self._set_cookie(c)
6868 return res
6969
--- a/sfjplib.py
+++ b/sfjplib.py
@@ -4,6 +4,8 @@
44
55 import crawlerlib
66 import urllib
7+import htmltree
8+import re
79
810 def login(uname, passwd):
911 """
@@ -51,4 +53,47 @@ class docman2(object):
5153 return res.read()
5254
5355
54-
56+class Wiki(object):
57+ """Wiki manipulation library"""
58+ def __init__(self, sfjp_user = None):
59+ self._user = sfjp_user
60+
61+ def retrive_wikitext(self, project_uid, name):
62+ c = crawlerlib.Crawler(self._user)
63+ url = "http://sourceforge.jp/projects/%s/wiki/%s?action=edit" % (project_uid, name)
64+ res = c.get(url)
65+ html = res.read()
66+
67+ tree = htmltree.parse(html).root()
68+ title = tree.get_element_by_id("title").attr("value")
69+ wikitext = tree.get_element_by_id("text").inner_text()
70+ comment = tree.get_element_by_id("comment").attr("value")
71+
72+ # get postkey
73+ #document.write('<inp'+'ut type="hidden" name="postkey" value="Alqh7yg">');
74+
75+ m = re.search(r'''type="hidden" name="postkey" value="(.*?)"''', html)
76+ if m:
77+ postkey = m.group(1)
78+ else:
79+ postkey = ""
80+ return (title, wikitext, comment, postkey)
81+
82+ def post_wikitext(self, project_uid, name, title, wikitext, comment, postkey):
83+ c = crawlerlib.Crawler(self._user)
84+ url = "http://sourceforge.jp/projects/%s/wiki/%s?action=update" % (project_uid, name)
85+
86+ title = title.encode("utf_8")
87+ wikitext = wikitext.encode("utf_8")
88+ comment = comment.encode("utf_8")
89+
90+ params = {
91+ "title": title,
92+ "textarea_height": 24,
93+ "text": wikitext,
94+ "comment": comment,
95+ "postkey": postkey
96+ }
97+ res = c.post_form(url, params)
98+ return res
99+