yuque 2 localfile.md

  1. # -*- coding: utf-8 -*-
  2. import json
  3. import requests
  4. import io
  5. import os
  6. import sys
  7. daiyi_tokens = 'x6XxGg7EmUMP7aZ5IvGy2bZDUZcDtYWvu1ZdENOj'
  8. daiyi_uid = 'u10014466'
  9. user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \
  10. (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'
  11. YUQUE_BASIC_V2_API_URL = 'https://www.yuque.com/api/v2/'
  12. headers = {
  13. 'user-agent': user_agent,
  14. 'X-Auth-Token': daiyi_tokens,
  15. "Content-Type": "application/json"
  16. }
  17. def get_repos_slug():
  18. """
  19. get_repos_slug()
  20. name: test_book ## namespace: u10014466/yx95w7
  21. name: IT_programmer ## namespace: u10014466/gtavdh
  22. name: gongde ## namespace: u10014466/itbbmu
  23. name: test_resource ## namespace: u10014466/ocd8g4
  24. """
  25. repos_info_url = YUQUE_BASIC_V2_API_URL + 'users/' + daiyi_uid + '/repos'
  26. r = requests.get(repos_info_url, headers=headers)
  27. result = json.loads(r.text) # len = 4
  28. # repo_namespace_list = []
  29. for repo in result['data']:
  30. print('name: ', repo['name'], ' ## namespace: ', repo['namespace'])
  31. def get_repos_table_of_content(repo_namespace: str) -> list:
  32. a_repos_docs_url = YUQUE_BASIC_V2_API_URL + "repos/" + \
  33. repo_namespace + "/toc" # get_all table of content
  34. r = requests.get(a_repos_docs_url, headers=headers)
  35. return json.loads(r.text)['data']
  36. def get_docs_slug_of_repo(repo_namespace: str) -> list:
  37. # repo_name = "IT_programmer"
  38. a_repos_docs_url = YUQUE_BASIC_V2_API_URL + "repos/" + repo_namespace + "/docs"
  39. r = requests.get(a_repos_docs_url, headers=headers)
  40. docs_info_list = json.loads(r.text)['data']
  41. with open('title_slug.md','w+',encoding='utf-8') as title_doc:
  42. for doc in docs_info_list:
  43. str_doc = "title "+ doc['title']+ " slug: "+ doc['slug']
  44. title_doc.write(str_doc)
  45. title_doc.write("\n\r")
  46. str_doc_list = []
  47. for doc in docs_info_list:
  48. str_doc = {"title": doc['title'], "slug": doc['slug']}
  49. str_doc_list.append(str_doc)
  50. return str_doc_list
  51. def _touch_files(cur_dir, path):
  52. # absolute_path = os.path.join(cur_dir,path)
  53. # if not os.path.exists(absolute_path):
  54. # os.mknod(absolute_path)
  55. pass
  56. def _mkdir(cur_dir, path):
  57. absolute_path = os.path.join(cur_dir,path)
  58. if not os.path.exists(absolute_path):
  59. os.mkdir(absolute_path)
  60. def get_toc_item_by_key(all_info: list, key: str, value: str) -> dict:
  61. for item in all_info:
  62. if item[key] == value:
  63. return item
  64. def _mkdir_and_touch_files(toc_list, cur_dir) -> list:
  65. with open('title_slug_info.json','w+',encoding='utf-8') as title_doc:
  66. for item in toc_list:
  67. title_doc.write(str(item))
  68. title_doc.write("\n\r")
  69. if not item["parent_uuid"]:
  70. item["path"] = item['title']
  71. elif item["parent_uuid"]:
  72. parent_item = get_toc_item_by_key(toc_list, "uuid", item["parent_uuid"])
  73. _mkdir(cur_dir, parent_item['path'])
  74. item["path"] = os.path.join(parent_item["path"], item['title'])
  75. else:
  76. pass
  77. _mkdir(cur_dir, item['path']) if item["type"] == "TITLE" else _touch_files(cur_dir,
  78. item['path'])
  79. return toc_list
  80. def get_doc_as_markdown_format(repo_namespace: str, doc_slug: str) -> str:
  81. # slug ## swd46l ##
  82. # a_doc_url = YUQUE_BASIC_V2_API_URL + "repos/" + repo_namespace + "/docs" + '/1c3f3919b8656a2990b24ccf15ac9ac3'
  83. # a_doc_url = YUQUE_BASIC_V2_API_URL + "repos/" + repo_namespace + "/docs" + '/en0ca3'
  84. # a_doc_url = "https://www.yuque.com/u10014466/gtavdh/1c3f3919b8656a2990b24ccf15ac9ac3/markdown?attachment=true&latexcode=true&anchor=true&linebreak=true"
  85. # a_doc_url = "https://www.yuque.com/" + repo_namespace + "/answer"+ "/markdown?attachment=true&latexcode=true&anchor=true&linebreak=true"
  86. a_doc_url = "https://www.yuque.com/" + repo_namespace + "/" + doc_slug + "/markdown?attachment=true&latexcode=true&anchor=false&linebreak=false"
  87. r = requests.get(a_doc_url, headers=headers)
  88. # print(r.text)
  89. # print(a_doc_url)
  90. return r.text
  91. def wirte_books(cur_dir):
  92. # IT_programmer_namespace = "u10014466/yx95w7"
  93. IT_programmer_namespace = "u10014466/gtavdh"
  94. table_of_contents = get_repos_table_of_content(IT_programmer_namespace)
  95. docs_slugs_list = get_docs_slug_of_repo(IT_programmer_namespace)
  96. print(len(docs_slugs_list))
  97. new_toc_list = _mkdir_and_touch_files(table_of_contents, cur_dir)
  98. print(len(new_toc_list))
  99. for doc_slug in docs_slugs_list:
  100. doc_info = get_doc_as_markdown_format(IT_programmer_namespace, doc_slug['slug'])
  101. # print(new_toc_list)
  102. doc_path = get_toc_item_by_key(new_toc_list, 'slug', doc_slug['slug'])
  103. if doc_path:
  104. # print('### @ ',doc_path,type(doc_path))
  105. # doc_path=doc_path['path']
  106. doc_path = doc_path['path'] + '.md'
  107. with open(os.path.join(cur_dir, doc_path), "w+", encoding='utf-8') as f:
  108. f.write(doc_info)
  109. wirte_books("it_programmer_test")
  110. def get_docs_lake_format(repo_namespace):
  111. # slug ## swd46l ##
  112. # a_doc_url = YUQUE_BASIC_V2_API_URL + "repos/" + repo_namespace + "/docs" + '/1c3f3919b8656a2990b24ccf15ac9ac3'
  113. a_doc_url = YUQUE_BASIC_V2_API_URL + "repos/" + \
  114. repo_namespace + "/docs" + '/en0ca3'
  115. r = requests.get(a_doc_url, headers=headers)
  116. docs_info = json.loads(r.text)['data']
  117. # for key in docs_info:
  118. # print("## ",key," ## ",docs_info[key])
  119. # print("## body: ",docs_info['body'])
  120. print("## slug: ", docs_info['slug'])
  121. print("## title: ", docs_info['title'])
  122. print("## format: ", docs_info['format'])
  123. with open("ldap_in_LENOVO.md", "w", encoding='utf-8') as f:
  124. f.write(docs_info['body'])
  125. """
  126. - docker ldap: [https://github.com/osixia/docker-openldap](https://github.com/osixia/docker-openldap)
  127. <a name="bs2Vv"></a>
  128. # more
  129. 限制,缺点,对内存,cpu 的要求---future<br />
  130. ## slug: en0ca3
  131. ## title: ldap 在联想 lico 的使用
  132. ## format: lake
  133. """
  134. """
  135. repo_name = "IT_programmer"
  136. repo_namesapce = "u10014466/gtavdh"
  137. UserSerializer=(
  138. id=10371552,
  139. type='User','木叶' ,
  140. description=None,
  141. acatar_url='https://cdn.nlark.com/yuque/0/2020/jpeg/anonymous/1608980864764-30953679-b853-40cf-bee9-636bc67359bb.jp
  142. eg',
  143. created_at='2020-12-26T11:08:24.000Z',
  144. upadate_at='2021-01-19T08:29:04.000Z',)
  145. YUQUE_OAUTH_AUTHORIZE_URL = "https://www.yuque.com/oauth2/authorize"
  146. YUQUE_OAUTH_EXCHANGE_TOKEN_URL = 'https://www.yuque.com/oauth2/token'
  147. YUQUE_BASIC_V2_API_URL = 'https://www.yuque.com/api/v2/'
  148. curl -H "X-Auth-Token: x6XxGg7EmUMP7aZ5IvGy2bZDUZcDtYWvu1ZdENOj" https://www.yuque.com/api/v2/users/
  149. """