yuque 2 localfile.md
# -*- coding: utf-8 -*-
import json
import requests
import io
import os
import sys
daiyi_tokens = 'x6XxGg7EmUMP7aZ5IvGy2bZDUZcDtYWvu1ZdENOj'
daiyi_uid = 'u10014466'
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'
YUQUE_BASIC_V2_API_URL = 'https://www.yuque.com/api/v2/'
headers = {
'user-agent': user_agent,
'X-Auth-Token': daiyi_tokens,
"Content-Type": "application/json"
}
def get_repos_slug():
"""
get_repos_slug()
name: test_book ## namespace: u10014466/yx95w7
name: IT_programmer ## namespace: u10014466/gtavdh
name: gongde ## namespace: u10014466/itbbmu
name: test_resource ## namespace: u10014466/ocd8g4
"""
repos_info_url = YUQUE_BASIC_V2_API_URL + 'users/' + daiyi_uid + '/repos'
r = requests.get(repos_info_url, headers=headers)
result = json.loads(r.text) # len = 4
# repo_namespace_list = []
for repo in result['data']:
print('name: ', repo['name'], ' ## namespace: ', repo['namespace'])
def get_repos_table_of_content(repo_namespace: str) -> list:
a_repos_docs_url = YUQUE_BASIC_V2_API_URL + "repos/" + \
repo_namespace + "/toc" # get_all table of content
r = requests.get(a_repos_docs_url, headers=headers)
return json.loads(r.text)['data']
def get_docs_slug_of_repo(repo_namespace: str) -> list:
# repo_name = "IT_programmer"
a_repos_docs_url = YUQUE_BASIC_V2_API_URL + "repos/" + repo_namespace + "/docs"
r = requests.get(a_repos_docs_url, headers=headers)
docs_info_list = json.loads(r.text)['data']
with open('title_slug.md','w+',encoding='utf-8') as title_doc:
for doc in docs_info_list:
str_doc = "title "+ doc['title']+ " slug: "+ doc['slug']
title_doc.write(str_doc)
title_doc.write("\n\r")
str_doc_list = []
for doc in docs_info_list:
str_doc = {"title": doc['title'], "slug": doc['slug']}
str_doc_list.append(str_doc)
return str_doc_list
def _touch_files(cur_dir, path):
# absolute_path = os.path.join(cur_dir,path)
# if not os.path.exists(absolute_path):
# os.mknod(absolute_path)
pass
def _mkdir(cur_dir, path):
absolute_path = os.path.join(cur_dir,path)
if not os.path.exists(absolute_path):
os.mkdir(absolute_path)
def get_toc_item_by_key(all_info: list, key: str, value: str) -> dict:
for item in all_info:
if item[key] == value:
return item
def _mkdir_and_touch_files(toc_list, cur_dir) -> list:
with open('title_slug_info.json','w+',encoding='utf-8') as title_doc:
for item in toc_list:
title_doc.write(str(item))
title_doc.write("\n\r")
if not item["parent_uuid"]:
item["path"] = item['title']
elif item["parent_uuid"]:
parent_item = get_toc_item_by_key(toc_list, "uuid", item["parent_uuid"])
_mkdir(cur_dir, parent_item['path'])
item["path"] = os.path.join(parent_item["path"], item['title'])
else:
pass
_mkdir(cur_dir, item['path']) if item["type"] == "TITLE" else _touch_files(cur_dir,
item['path'])
return toc_list
def get_doc_as_markdown_format(repo_namespace: str, doc_slug: str) -> str:
# slug ## swd46l ##
# a_doc_url = YUQUE_BASIC_V2_API_URL + "repos/" + repo_namespace + "/docs" + '/1c3f3919b8656a2990b24ccf15ac9ac3'
# a_doc_url = YUQUE_BASIC_V2_API_URL + "repos/" + repo_namespace + "/docs" + '/en0ca3'
# a_doc_url = "https://www.yuque.com/u10014466/gtavdh/1c3f3919b8656a2990b24ccf15ac9ac3/markdown?attachment=true&latexcode=true&anchor=true&linebreak=true"
# a_doc_url = "https://www.yuque.com/" + repo_namespace + "/answer"+ "/markdown?attachment=true&latexcode=true&anchor=true&linebreak=true"
a_doc_url = "https://www.yuque.com/" + repo_namespace + "/" + doc_slug + "/markdown?attachment=true&latexcode=true&anchor=false&linebreak=false"
r = requests.get(a_doc_url, headers=headers)
# print(r.text)
# print(a_doc_url)
return r.text
def wirte_books(cur_dir):
# IT_programmer_namespace = "u10014466/yx95w7"
IT_programmer_namespace = "u10014466/gtavdh"
table_of_contents = get_repos_table_of_content(IT_programmer_namespace)
docs_slugs_list = get_docs_slug_of_repo(IT_programmer_namespace)
print(len(docs_slugs_list))
new_toc_list = _mkdir_and_touch_files(table_of_contents, cur_dir)
print(len(new_toc_list))
for doc_slug in docs_slugs_list:
doc_info = get_doc_as_markdown_format(IT_programmer_namespace, doc_slug['slug'])
# print(new_toc_list)
doc_path = get_toc_item_by_key(new_toc_list, 'slug', doc_slug['slug'])
if doc_path:
# print('### @ ',doc_path,type(doc_path))
# doc_path=doc_path['path']
doc_path = doc_path['path'] + '.md'
with open(os.path.join(cur_dir, doc_path), "w+", encoding='utf-8') as f:
f.write(doc_info)
wirte_books("it_programmer_test")
def get_docs_lake_format(repo_namespace):
# slug ## swd46l ##
# a_doc_url = YUQUE_BASIC_V2_API_URL + "repos/" + repo_namespace + "/docs" + '/1c3f3919b8656a2990b24ccf15ac9ac3'
a_doc_url = YUQUE_BASIC_V2_API_URL + "repos/" + \
repo_namespace + "/docs" + '/en0ca3'
r = requests.get(a_doc_url, headers=headers)
docs_info = json.loads(r.text)['data']
# for key in docs_info:
# print("## ",key," ## ",docs_info[key])
# print("## body: ",docs_info['body'])
print("## slug: ", docs_info['slug'])
print("## title: ", docs_info['title'])
print("## format: ", docs_info['format'])
with open("ldap_in_LENOVO.md", "w", encoding='utf-8') as f:
f.write(docs_info['body'])
"""
- docker ldap: [https://github.com/osixia/docker-openldap](https://github.com/osixia/docker-openldap)
<a name="bs2Vv"></a>
# more
限制,缺点,对内存,cpu 的要求---future<br />
## slug: en0ca3
## title: ldap 在联想 lico 的使用
## format: lake
"""
"""
repo_name = "IT_programmer"
repo_namesapce = "u10014466/gtavdh"
UserSerializer=(
id=10371552,
type='User','木叶' ,
description=None,
acatar_url='https://cdn.nlark.com/yuque/0/2020/jpeg/anonymous/1608980864764-30953679-b853-40cf-bee9-636bc67359bb.jp
eg',
created_at='2020-12-26T11:08:24.000Z',
upadate_at='2021-01-19T08:29:04.000Z',)
YUQUE_OAUTH_AUTHORIZE_URL = "https://www.yuque.com/oauth2/authorize"
YUQUE_OAUTH_EXCHANGE_TOKEN_URL = 'https://www.yuque.com/oauth2/token'
YUQUE_BASIC_V2_API_URL = 'https://www.yuque.com/api/v2/'
curl -H "X-Auth-Token: x6XxGg7EmUMP7aZ5IvGy2bZDUZcDtYWvu1ZdENOj" https://www.yuque.com/api/v2/users/
"""