工作中经常用到ProcessOn,在线制作思维导图,E-R图,类图,框图,流程图,泳道图,原型图,?架构图,组件部署图,UML图,网络拓扑图,组织结构图,BPMN图,这个图,那个图,比起viso来,优点是他是一个网页版工具,打开浏览器即可画图;缺点是他是一个网页版工具,你画的图都存在别人的机器上,虽然可以下载到本地,但如果你的文件比较多,一个一个下载也比较繁琐。好的长话短说,本文提供一个基于Python的Processon文件一键备份脚本,以解决上述问题。
技术分析:
1.登录processon,使用cookie-editor导出cookie文件
2.request时使用cookie参数加载cookie字典,完成用户验证
3.递归调用接口获取文件夹和文件列表
4.获取文件的definition
5.根据文件的definition导出文件
实现如下:
import requests
import re
import os
from threading import Thread
import time
import requests
from io import BytesIO
import http.cookiejar as cookielib
from PIL import Image
import sys
import psutil
import json
import random
import datetime
import math
base_url = 'https://www.processon.com'
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.55 Safari/537.36'
}
def get_cookies():
cookies_dict = {}
try:
with open('cooki.txt', 'r') as f:
cookies = json.loads(f.read())
for cookie in cookies:
cookies_dict[cookie['name']] = cookie['value']
except Exception as e:
print(e)
return cookies_dict
def get_defs(chart_id):
defs = ''
url = '%s/diagraming/%s' % (base_url, chart_id)
try:
time.sleep(random.random()*5)
cookies = get_cookies()
response = requests.get(url=url, headers=headers, cookies=cookies)
response.raise_for_status()
ts = round(time.time() * 1000)
url_def = '%s/diagraming/getdef?id=%s&_=%s' % (base_url, chart_id, ts)
print(url_def)
response = requests.get(url=url_def, headers=headers, cookies=cookies)
response.raise_for_status()
res = response.text
res_json = json.loads(res)
def_json = res_json['def']
defs = def_json # .replace('\\', '')
print(defs)
except Exception as e:
print(e)
return defs
def export_file(file_type, file_path, chart_id):
url = 'https://assets.processon.com/diagram_export/mindmap'
file_name = file_path.split('\\')[-1]
try:
defs = get_defs(chart_id)
# with open('defff', "w") as f:
# f.write(defs)
if defs:
data = {
'type': file_type,
'title':file_name,
'chartId':chart_id,
'ignore':'definition',
# 'width':'',
# 'height':'',
'definition': defs
}
cookies = get_cookies()
res = requests.post(url=url, headers=headers, cookies=cookies, data=data)
res.raise_for_status()
content = res.content
print(file_path)
with open(file_path, "wb") as f:
f.write(content)
# exit()
except Exception as e:
print(e)
def load_files(folder_title, folder_id):
file_type = 'pos'
url = '%s/folder/loadfiles' % base_url
cookies = get_cookies()
data = {
'resource': 'diagrams',
'folderId': folder_id,
'searchTitle': '',
'sort': 'title',
'view': 'list'
}
time.sleep(random.random()*3)
response = requests.post(url=url, headers=headers, cookies=cookies, data=data)
response.raise_for_status()
# print(response)
# print(response.text)
if not os.path.exists(folder_title):
os.makedirs(folder_title)
try:
files = json.loads(response.text)
if files['result'] == 'success':
charts = files['charts']
folders = files['folders']
for chart in charts:
print('\t'+chart['title'])
file_path = os.path.join(folder_title, chart['title'])
file_path += '.'+file_type
export_file(file_type, file_path, chart['chartId'])
# print('')
if bool(folders):
for folder in folders:
# print('\t'+folder['title'])
print(os.path.join(folder_title, folder['title']))
load_files(os.path.join(folder_title, folder['title']), folder['folderId'])
print('')
except Exception as e:
print(e)
if __name__ == '__main__':
backup_date = 'back'+(datetime.datetime.now().strftime('%Y%m%d'))
load_files(backup_date, 'root')
免责声明:1、本文资料均来源于网络,如有侵权,请联系删除。2、本文仅作本人学习、记录、分析之用途,无推荐任何人购买之目的,任何人据此操作,本人不负任何责任。
|