![](https://img.haomeiwen.com/i5798456/f513b26712790602.png)
背景
你是否遇到过如下的问题:
-
在看书或者读文章时,经常需要做笔记。可选的方案有:
- 在本地的记事本中记录。缺点:只能本地访问,不能通过浏览器查看;如果硬盘意外损坏,记录的数据也将丢失。
- vscode/atom + github,vscode 和 atom 都是非常优秀的编辑器,将写成的笔记上传到 github 仓库中,可以实现通过浏览器进行笔记查看以及备份的功能。缺点:就做个笔记要在 github 上建个仓库,未免有点大材小用,也有点麻烦。
- 其他,例如印象笔记(收费,有时候同步超级超级慢)、有道云笔记(不专业,写 markdown 好丑)、简书(做笔记貌似不是很合适)等;
- 我们下面要讲的 gist + gist_backup.py 的方法;
-
你有一些常用的代码片段,希望能够将这些代码片段进行整理、归类与备份,在之后需要使用的时候能够快速找到;
-
你有一些配置,快捷键,问题解决方法等需要记录,并希望以后用的时候能够快速查到;
如果你有上面的这些需求,请继续往下看。
gist
gist 的官网是 https://gist.github.com/,用法也超级简单,我这里就不啰嗦了。
不像 github,gist 文本可以设置为 public 或者 private 两种(github 不缴费只能 public)。在 gist 上写笔记,记录代码都非常简单,界面也非常清爽:
![](https://img.haomeiwen.com/i5798456/7fa1f904ac84404a.png)
gist 归类
gist 目前(timestamp: 2018.12)的不足之处为没有代码归类功能,如果你有上百个代码或者笔记,那找起来将会非常麻烦。
解决方法:在 gist 中写一个 markdown 文件,在该文件中进行归类,你只要记录该文件的网址,以它作为入口访问你的代码或笔记(有点像当年门户网站的功能):![](https://img.haomeiwen.com/i5798456/c9fa0e974da35495.png)
gist 备份
看到上面那张图的右上角有个鲜红色的 Delete
,每次看着它都心惶惶的,一不小心点错,那代码可能就凉凉了(虽然会再弹框进行提示,不过删除一个 gist snippet 比 github repository 要简单得多,总觉得不太安全),而且你可能也会想要找回之前被你删除的代码片段或笔记。
因此,将我们有必要将所有的 gist 再备份到 github ,那即使是误删也可以恢复了。不过如果通过手动复制所有代码,那未免效率太低了。
下面是我写的一个python 脚本,用法为: gist_backup.py account_name
, 其中,account_name 是你想下 gist 的用户账户名,也就是说,通过修改该参数,你不仅可以下自己的 public gist snippet,也能下别人的 public gist snippet。
该脚本功能:
- 如果本地没有对应的 gist 代码/笔记,从 gist.github.com 上下载;
- 如果本地有对应的 gist 代码/笔记,能够根据远端的更新日期选择是否更新本地的代码/笔记;
- 如果远端删除了某个gist 代码/笔记,本地也将同步删除该 gist 代码/笔记;
当前问题:不能下私有的 gist 代码/笔记,后面如果有需要,我将更新一个可以下载私有 gist 代码/笔记 的脚本。另外,如果终端不能访问国外网站,可能会下到一半卡住,如果出现这种情况,解决方法可以是:1、更新 hosts 中的 github 地址(方法自查,该方法有时候不能起效果),2、shadowsocks+privoxy(方法自查,该方法一定有效!)
#!/usr/bin/env python
# Note: if you are in China, you should make sure your shell can't visit
# foreign websites, otherwise you may fail to use git api
import argparse
import urllib
import json
import requests
import os
from datetime import datetime
from urllib.request import urlopen
parser = argparse.ArgumentParser(description='Pull gists from github')
parser.add_argument('git_account', type=str,
help="Specify the github account name you want pull gist")
parser.add_argument('-p', '--perpage', default=30, type=int,
help="Specify the number of entries per page")
parser.add_argument('-i', '--information_file', default='./gist_information.json',
type=str, help="The file of storing gist informations ")
opt = parser.parse_args()
user = opt.git_account
perpage = opt.perpage
information_file = opt.information_file
print('github user:', user)
root_url = 'https://api.github.com/users/'+user
userurl = urlopen(root_url)
public_gists = json.load(userurl)
gistcount = public_gists['public_gists']
print(f'Found gists : {gistcount}')
pages = (gistcount-1) // perpage + 1
print(f"Found pages : {pages}")
# dir '.' is the directory of running this script
# not the directory where script in
if os.path.exists(information_file):
with open(information_file, 'r') as f:
gist_information = json.load(f)
else:
gist_information = dict()
update_information = dict()
files_counter = 0
for page in range(pages):
print(f"Processing page number {page+1} ...")
pageUrl = root_url + '/gists?page=' + str(page+1)
gisturl = urlopen(pageUrl)
gist_entries = json.load(gisturl)
for gist_info in gist_entries:
files_counter += 1
gist_file = gist_info['files']
gist_file_name = list(gist_file.keys())[0]
gist_file_raw_url = gist_file[gist_file_name]['raw_url']
gist_updated_time = gist_info['updated_at']
gist_file_description = gist_info['description']
update_information[gist_file_name] = {
'updated_at': gist_updated_time,
'description': gist_file_description
}
if (gist_file_name in gist_information and
gist_information[gist_file_name]['updated_at'] == gist_updated_time):
print(f'No.{files_counter} file {gist_file_name} is up to date')
del gist_information[gist_file_name]
else:
if gist_file_name in gist_information:
del gist_information[gist_file_name]
print(f"No.{files_counter} file {gist_file_name} is updating...", end= ' ')
gist_content = requests.get(gist_file_raw_url).text
with open(gist_file_name, 'w') as f:
f.write(gist_content)
print('OK')
for gist_file_name in gist_information:
if os.path.exists(os.path.join('.', gist_file_name)):
os.remove(os.path.join('.', gist_file_name))
print(f'File "{gist_file_name}" is deleted')
with open(information_file, 'w') as f:
json.dump(update_information, f)
print('Complete backup')
实验效果:
![](https://img.haomeiwen.com/i5798456/44844e500e7f6a16.png)
我在远端修改了 cv_framework_detail.py,可以看到,就对该文件进行了更新,其他文件保存不变:
![](https://img.haomeiwen.com/i5798456/2828f7151f0b0f3d.png)
gist_infomation.json 用于记录本地的文件信息的,因此会自动修改。之后就可以用 git 将本地文件 push 到 github 上完成备份了,非常简单。
该代码的 github 地址为:https://github.com/foolishflyfox/fscript/blob/master/gist_backup.py
有任何问题大家都可以在上面发 issue,谢谢!
-------------------------
< Just have fun with gist >
-------------------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
网友评论