0. 简介

confluence 是我们日常文档的整理工具,有时候我们经常需要拉取一些好文章或是查看最近有什么更新,而可视化界面对于细节查找不太友好,这里我将尝试着使用atlassian-api对confluence实现二次开发,并将特定时间段的源码快速导出

1. Atlassian-api

atlassian-api 作为一款基于python的API lib,非常适合快速开发,confulence提供了非常清晰的RESTful API,直接使用API比confluence_python_cli这个库更方便。同时该api提供了充足的文档来方便我们对jira、confluence进行调用,并快速过滤到我们想要的信息。
在这里插入图片描述

2. CQL语言

在atlassian-api的高级开发中,我们避免不了使用CQL语言中的字段是一个词,该语言类似于MYSQL语言,通过一个单词表示内容的索引属性。一个运算符表示前后两者的关系;运算符后面跟一个或多个值(或函数)用来表示具体特征。运算符将字段的值与右侧的一个或多个值或函数进行比较,以便子句只检索真正的结果。

  • Ancestor:搜索给定父页面的所有子页面
  • Container:搜索包含在具有给定ID的内容中的Content
  • Content:搜索具有给定ID的内容
  • Created:搜索在特定日期(或日期范围)当天、之前或之后创建的内容
  • Creator:搜索由特定用户创建的内容
  • Contributor:搜索由特定用户创建或编辑的内容
  • Favourite, favorite:搜索特定用户喜欢的内容
  • ID:搜索具有给定内容ID的内容
  • Label:搜索具有特定标签的内容
  • Last modified:搜索上次在特定日期(或日期范围)当天、之前或之后修改的内容
  • Macro:搜索内容体中具有给定名称的宏实例的内容
  • Mention:搜索提及特定用户的内容
  • Parent:搜索特定父页面的子内容
  • Space
  • Text
  • Title:按标题或包含特定文本的标题搜索内容
  • Type:搜索特定类型的内容
  • Watcher:搜索特定用户正在观看的内容

请添加图片描述

3. Confluence文档导出代码

# coding=utf-8
import os
import re
from unittest import result
from atlassian import Confluence

"""This example shows how to export pages """

confluence = Confluence(url="http://localhost:8090", username="admin", password="admin")


def save_file(content, title, label):
    path= os.path.join(os.getcwd(),label)
    if(os.path.exists(path)==False):
        os.mkdir(path)
    print(path)
    file_pdf = open(path + "/"+ title + ".pdf", "wb")
    file_pdf.write(content)
    file_pdf.close()
    print("Completed")


if __name__ == "__main__":
    spaces = ["ADMIN","QA","DEV"]
    for space in spaces:
      pages = confluence.get_all_pages_from_space(space=space, start=0, limit=10000)
      # print(pages[0]["id"])
      start_data ="2022/02/15"
      end_data ="2022/02/22"
      cqls='space="{}" and created <="{}" and created >="{}"'.format(space, end_data,start_data)

      answers = confluence.cql(cqls, expand=True)["results"]


       for i in range(len(answers)):
         try:
           pattern_all = answers[i]['content']['_links']['webui']
           pattern = re.compile(r'(?<=2F)\d+\.?\d*')
           print(pattern.findall(pattern_all)[0])
           page_title = answers[i]['content']['title']
           print(page_title)
           if(page_title.find("jpg")==-1 and page_title.find("png")==-1 and page_title.find("pdf")==-1 and page_title.find("JPG")==-1 and page_title.find("PNG")==-1 and page_title.find("PDF")==-1):
             response = confluence.get_page_as_pdf(pattern.findall(pattern_all)[0])
             save_file(response, page_title, space)
         except Exception as e:
           pass

参考文献

https://atlassian-python-api.readthedocs.io/confluence.html

https://github.com/atlassian-api/atlassian-python-api

https://github.com/RaymiiOrg/confluence-python-cli

https://www.cnblogs.com/superhin/p/11728411.html

https://developer.atlassian.com/server/confluence/cql-field-reference/#created