• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javaandroidc++linuxc#objective-ccocoa誰得qtrubypythongamewindowsbathyscaphephpguic翻訳omegattwitterframeworktestbtronarduinovb.net計画中(planning stage)directxpreviewerゲームエンジンdom

maintaince tools for sfjp magazine


Commit MetaInfo

修订版81e075a1d916532ec67ee60b93bd2f0dac8c3e2d (tree)
时间2013-05-01 20:28:17
作者hylom <hylom@hylo...>
Commiterhylom

Log Message

add: upload_media method

更改概述

差异

--- a/sfjpmagclient/sfjpmagclient.py
+++ b/sfjpmagclient/sfjpmagclient.py
@@ -7,14 +7,17 @@ import sys
77 HOST_PATH = 'magazine-admin.sourceforge.jp/magazine/xmlrpc.php'
88
99 class MagClient(object):
10- def __init__(self, username, password, auth_user="", auth_pass=""):
10+ def __init__(self, username, password, auth_user="", auth_pass="", endpoint=None):
1111 self.username = username
1212 self.password = password
13+ if endpoint == None:
14+ endpoint = HOST_PATH
15+
1316 if auth_user and auth_pass:
14-# self.uri = "http://{username}:{password}@{url}".format(username=auth_user, password=auth_pass, url=HOST_PATH)
15- self.uri = "http://%s:%s@%s" % (auth_user, auth_pass, HOST_PATH)
17+# self.uri = "http://{username}:{password}@{url}".format(username=auth_user, password=auth_pass, url=endpoint)
18+ self.uri = "http://%s:%s@%s" % (auth_user, auth_pass, endpoint)
1619 else:
17- self.uri = "http://" + HOST_PATH
20+ self.uri = "http://" + endpoint
1821
1922 def _get_blog_id(self):
2023 try:
@@ -42,6 +45,23 @@ class MagClient(object):
4245 self.password,
4346 content)
4447
48+ def upload_media(self, filename, data, mimetype, overwrite=True, parent=None):
49+ proxy = self._get_proxy()
50+ post_data = {
51+ "name": filename,
52+ "type": mimetype,
53+ "bits": xmlrpclib.Binary(data),
54+ "overwrite": overwrite
55+ }
56+
57+ if parent != None:
58+ post_data["post_id"] = parent
59+
60+ return proxy.wp.uploadFile(self._get_blog_id(),
61+ self.username,
62+ self.password,
63+ post_data)
64+
4565 def get_users_blogs(self):
4666 proxy = self._get_proxy()
4767 return proxy.wp.getUsersBlogs(self.username, self.password)
Binary files /dev/null and b/test/sample.png differ
--- a/test/test_sfjpmagclient.py
+++ b/test/test_sfjpmagclient.py
@@ -9,10 +9,11 @@ from sfjpmagclient import MagClient
99
1010 username = config["username"]
1111 password = config["password"]
12-auth_user = config["auth_user"]
13-auth_pass = config["auth_pass"]
12+auth_user = config.get("auth_user", "")
13+auth_pass = config.get("auth_pass", "")
14+endpoint = config["endpoint"]
1415
15-c = MagClient(username, password, auth_user, auth_pass)
16+c = MagClient(username, password, auth_user, auth_pass, endpoint)
1617 ret = c.get_users_blogs()
1718 print ret
1819
--- /dev/null
+++ b/test/test_upload_media.py
@@ -0,0 +1,36 @@
1+#!/usr/bin/python
2+# -*- coding: utf-8 -*-
3+
4+import json
5+import sys
6+import xmlrpclib
7+sys.path.append('.')
8+
9+from testconfig import config
10+from sfjpmagclient import MagClient
11+
12+username = config["username"]
13+password = config["password"]
14+auth_user = config.get("auth_user", "")
15+auth_pass = config.get("auth_pass", "")
16+endpoint = config["endpoint"]
17+
18+POST_ID = 1
19+TEST_FILE = "test/sample.png"
20+
21+c = MagClient(username, password, auth_user, auth_pass, endpoint)
22+
23+f = open(TEST_FILE, "r")
24+test_data = f.read()
25+f.close()
26+
27+try:
28+ ret = c.upload_media("This_is_test.png", test_data, "image/png",
29+ True, 1)
30+except xmlrpclib.Fault, e:
31+ for k in e.__dict__:
32+ print k, e.__getattribute__(k)
33+ sys.exit(-1)
34+print ret
35+
36+