-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgist_sync.py
More file actions
executable file
·66 lines (48 loc) · 1.88 KB
/
gist_sync.py
File metadata and controls
executable file
·66 lines (48 loc) · 1.88 KB
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
#!/usr/bin/env /Library/Developer/CommandLineTools/usr/bin/python3
import os
import time
import gistyc
AUTH_TOKEN = "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"
BASE_DIR = "/Users/yejiaxin/Documents/文档/学习笔记/"
def get_local_files():
local_file = os.listdir(BASE_DIR)
return list(filter(lambda filename: filename[-2:] == "md", local_file))
def get_gists_files(api: gistyc.GISTyc):
gist_list = api.get_gists()
gist_files = []
for gist in gist_list:
for gist_filename in gist.get("files").keys():
gist_files.append(gist_filename)
return gist_files
def update_gist(api: gistyc.GISTyc, filename):
file_path = os.path.join(BASE_DIR, filename)
api.update_gist(file_name=file_path)
def create_gist(api: gistyc.GISTyc, filename):
file_path = os.path.join(BASE_DIR, filename)
api.create_gist(file_name=file_path)
def get_gists_url_list(api: gistyc.GISTyc):
gist_list = api.get_gists()
gist_url_list = {}
for gist in gist_list:
html_url = gist.get("html_url")
for file_name in gist.get("files").keys():
if file_name[-2:] == "md":
gist_url_list[file_name] = html_url
return gist_url_list
if __name__ == '__main__':
start = time.time()
gist_api = gistyc.GISTyc(auth_token=AUTH_TOKEN)
local_files = get_local_files()
gists_files = get_gists_files(gist_api)
need_create_files = set(local_files) - set(gists_files)
need_update_files = set(local_files) - need_create_files
for file in need_create_files:
print("create gist " + file)
create_gist(gist_api, file)
for file in need_update_files:
print("update gist " + file)
update_gist(gist_api, file)
gist_urls = get_gists_url_list(gist_api)
for filename, url in gist_urls.items():
print(filename + ": " + url)
print("sync gists cost {}s".format(time.time()-start))