调用confluence rest api,将str字符串,直接生产page页面,上传到confluence后台执行page页面下,

confluence rest api官方文档:

      https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/

       注意:

  •   confluence 支持page格式是xml格式,不是html格式,所以requests请求的data需要改成指定xml数据格式
  •         rest api 的url有的版本有confluence这级目录,有的版本没有,如:http://XXXXXXXXX/rest/api/content/ 或 http://XXXXXXXXX/confluence/rest/api/content/
  •         update使用rest url后面需要加上更新page的id,如:http://XXXXXXXXX/rest/api/content/123456

confluence.py文件

 1 # -*- coding:utf-8 -*-
 2
 3 import json
 4 import requests
 5 from defaults import *
 6 from requests.auth import HTTPBasicAuth
 7
 8 import sys
 9 reload(sys)
10 sys.setdefaultencoding('utf8')
11
12
13 def g_params(type, title, space):
14     # 生成get请求使用的扩展参数
15     str_space = {'key': space}
16     params = {'type': type,
17               'title': title,
18               "space": str_space,
19               "expand": "space,body.view,version,container"}
20     return params
21
22 def get_page(type, title, space):
23     # 通过title查看page是否已经存在
24     id=None
25     params = g_params(type, title, space)
26     r = requests.get(CONFLUCE_REST, params=params,
27                     auth=(USERNAME, PASSWD),
28                     headers=HEADER)
29     response = json.loads(r.content)
30     try:
31         id = response['results'][0]['id']
32     except Exception as e:
33         return None
34     else:
35         version = response['results'][0]['version']['number']
36         return id,version
37
38 def update_page(data, url):
39     # 更新已经存在的page页面
40     return requests.put(url,
41                      json=data,
42                      auth=(USERNAME, PASSWD),
43                      verify=True,
44                      headers=HEADER)
45
46 def create_page(data):
47     # 创建新的page页面
48     return requests.post(url=CONFLUCE_REST,
49                      json=data,
50                      auth=(USERNAME, PASSWD),
51                      verify=True,
52                      headers=HEADER)
53
54 def get_contnet(c, title, id=None, version_id=None):
55     # 将str字符串生产xml格式
56     confluence_content = ""
57     for sub, con in c:
58         if sub == 'Amazon Web Services' or sub == 'aws@cctvnews.cn':
59             str_sub = "<p style='float:left; width:100px; height:auto; padding:12px; font-size:15px; font-weight:bold;'>"+ 'Amazon Web Services'  + "</p>"
60             str_content = "<p style='float:left; word-break:break-word; border:solid 1px #ADADAD; margin-left:10px; padding:10px; width:600px; height:auto; font-size:14px; background-color:#ECF5FF;'>" + con + "</p>"
61         else:
62             new_sub = sub.replace("<", "[").replace(">", "]")
63             str_sub = "<p style='float:left; width:100px; height:auto; padding:12px; color:#FF8000; font-size:15px; font-weight:bold;'>" + new_sub + "</p>"
64             str_content = "<p style='float:left; word-break:break-word; border:solid 1px #ADADAD; margin-left:10px; padding:10px; width:600px; height:auto; font-size:14px; background-color:#F0F0F0;'>" + con + "</p>"
65         confluence_content = confluence_content + "<div style='clear: both'>" + str_sub + str_content + "</div>"
66     if version_id:
67         # 每更新一次,version id需要加1,update操作时,需要version_id参数
68         data = '{"id":"' + id + '","type":"page","title":"' + title + '","ancestors":[{"id":' + PARENT_PAGE + '}],"space":{"key":"CGTN"},"body":{"storage":{"value":"' + confluence_content + '","representation":"storage"}},"version":{"number":' + version_id + '}}'
69     else:
70         # create操作使用的参数
71         data = '{"type":"page","title":"' + title + '","ancestors":[{"id":' + PARENT_PAGE + '}],"space":{"key":"CGTN"},"body":{"storage":{"value":"' + confluence_content + '","representation":"storage"}}}'
72
73     return data
View Code

defaults.py文件

 1 # -*- coding:utf-8 -*-
 2 #
 3
 4 """
 5 全局变量
 6 """
 7
 8 #: Confluence wiki params
 9 USERNAME = "xxxxx"
10 PASSWD = "xxxxxx"
11 SPACE = "xxxxx"
12 TYPE = "page"
13 CONFLUCE_REST= "http://xxxxxx/rest/api/content/"
14 HEADER = {'content-type': 'application/json'}
15 PARENT_PAGE = "xxxx" #父级目录
View Code
12-17 19:34
查看更多