sfjplib for python
修訂 | b5561a04bfec320b5c99de8c76e25c0e7d05be85 (tree) |
---|---|
時間 | 2011-08-25 20:33:18 |
作者 | Hiromichi MATSUSHIMA <hirom@offi...> |
Commiter | Hiromichi MATSUSHIMA |
add Wiki support
@@ -63,7 +63,7 @@ class Crawler(object): | ||
63 | 63 | p = urllib2.HTTPCookieProcessor(c) |
64 | 64 | opener = urllib2.build_opener(p) |
65 | 65 | |
66 | - res = opener.open(url, params) | |
66 | + res = opener.open(url, urllib.urlencode(params)) | |
67 | 67 | self._set_cookie(c) |
68 | 68 | return res |
69 | 69 |
@@ -4,6 +4,8 @@ | ||
4 | 4 | |
5 | 5 | import crawlerlib |
6 | 6 | import urllib |
7 | +import htmltree | |
8 | +import re | |
7 | 9 | |
8 | 10 | def login(uname, passwd): |
9 | 11 | """ |
@@ -51,4 +53,47 @@ class docman2(object): | ||
51 | 53 | return res.read() |
52 | 54 | |
53 | 55 | |
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 | + |