maintaince tools for sfjp magazine
修订版 | 81e075a1d916532ec67ee60b93bd2f0dac8c3e2d (tree) |
---|---|
时间 | 2013-05-01 20:28:17 |
作者 | hylom <hylom@hylo...> |
Commiter | hylom |
add: upload_media method
@@ -7,14 +7,17 @@ import sys | ||
7 | 7 | HOST_PATH = 'magazine-admin.sourceforge.jp/magazine/xmlrpc.php' |
8 | 8 | |
9 | 9 | 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): | |
11 | 11 | self.username = username |
12 | 12 | self.password = password |
13 | + if endpoint == None: | |
14 | + endpoint = HOST_PATH | |
15 | + | |
13 | 16 | 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) | |
16 | 19 | else: |
17 | - self.uri = "http://" + HOST_PATH | |
20 | + self.uri = "http://" + endpoint | |
18 | 21 | |
19 | 22 | def _get_blog_id(self): |
20 | 23 | try: |
@@ -42,6 +45,23 @@ class MagClient(object): | ||
42 | 45 | self.password, |
43 | 46 | content) |
44 | 47 | |
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 | + | |
45 | 65 | def get_users_blogs(self): |
46 | 66 | proxy = self._get_proxy() |
47 | 67 | return proxy.wp.getUsersBlogs(self.username, self.password) |
@@ -9,10 +9,11 @@ from sfjpmagclient import MagClient | ||
9 | 9 | |
10 | 10 | username = config["username"] |
11 | 11 | 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"] | |
14 | 15 | |
15 | -c = MagClient(username, password, auth_user, auth_pass) | |
16 | +c = MagClient(username, password, auth_user, auth_pass, endpoint) | |
16 | 17 | ret = c.get_users_blogs() |
17 | 18 | print ret |
18 | 19 |
@@ -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 | + |