-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs2md.py
132 lines (115 loc) · 4.19 KB
/
fs2md.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import requests
import base64
import json
import re
import yaml
class ImageProcess:
def __init__(self, image_path, file_name):
self.image_path = image_path
self.file_name = file_name
self.__token = ""
self.__repo = ""
self.__msg = "add_" + file_name
self.__name = ""
self.__email = ""
def setGitParam(self, repo, token, name = "nobody", email = "[email protected]"):
self.__token = token
self.__repo = repo
self.__name = name
self.__email = email
def upload2git(self, b64Img):
image_name = os.path.join(self.image_path, self.file_name)
url = "https://api.github.com/repos/" + self.__repo + "/contents/" + image_name
headers = {"Authorization": "token " + self.__token}
data = {
"message": self.__msg,
"committer": {
"name": self.__name,
"email": self.__email
},
"content": b64Img
}
data = json.dumps(data)
req = requests.put(url = url, data = data, headers = headers)
req.encoding = "utf-8"
if req.status_code == 422:
r = requests.get(url=url, headers=headers)
r_data = json.loads(r.text)
return r_data['download_url']
elif req.status_code == 201:
re_data = json.loads(req.text)
return re_data['content']['download_url']
else:
print("Uncaught error in git api, erorr code : ", req.status_code)
return json.loads(req.text)
def download2local(self, b64Img):
if not os.path.exists(self.image_path):
os.makedirs(self.image_path)
image_name = os.path.join(self.image_path, self.image_name)
f = open(image_name, 'wb')
f.write(base64.b64decode(b64Img))
f.close()
return image_name
def fsURL2git(file_name, gitPram):
#read and write file
f = open(file_name, 'r')
Lines = f.readlines()
f.close()
f = open(file_name, 'w')
image_count = 1
No_1_count = 0
No_2_count = 0
No_3_count = 0
# Strips the newline character
img_name = file_name
for line in Lines:
line = line.strip()
if re.match(r'\#\s.+',line):
img_name = re.search('\w+',line).group()
No_1_count += 1
No_2_count = 0
No_3_count = 0
image_count = 1
if re.match(r'\#\#\s.+',line):
img_name = re.search('\w+',line).group()
No_2_count += 1
No_3_count = 0
image_count = 1
if re.match(r'\#\#\#\s.+',line):
img_name = re.search('\w+',line).group()
No_3_count += 1
image_count = 1
# this is a image_url
if re.match(r'\!\[.+\]\(.+\)',line):
# tag = re.search(r'\!\[.+\]',line).group()[2:-1]
url = re.search(r'\(.+\)',line).group()[1:-1]
tag = "Pic_" + str(No_1_count)
tag += "." + str(No_2_count)
tag += "." + str(No_3_count)
tag += "." + str(image_count)
image_name = tag + "_" + img_name + ".png"
try:
req = requests.get(url)
b64Img = base64.b64encode(req.content).decode()
wFlow = ImageProcess(file_name, image_name)
wFlow.setGitParam(repo = gitPram["repo"],token = gitPram["token"], \
name = gitPram["name"], email = gitPram["email"])
#if update image to git, use this
url = wFlow.upload2git(b64Img)
#if download image to local use this
#url = wFlow.download2local(b64Img)
except:
print("unable to download or update from url. ")
print(url)
line = "![" + tag + "]" + "(" + url + ")"
image_count += 1
f.write(line + "\n")
f.close()
# if __name__ == "__main__":
# # load params
# yml = open(yaml_path, 'r', encoding='utf-8')
# myPram = yaml.safe_load(yml.read())
# fsURL2git("VSLAM-Base.md", myPram["git"])