怎么个事儿呢
就是说我这个网页部署在GitHub的一个文件夹里,根目录是我的主页,但是这就导致了一个问题:
我没法直接通过hexo d上传文件
这样倒也好解决,直接手动上传好啦
但是我懒~~~~~
于是乎用Python写了个能自动上传的程序
这样下次写东西直接上传即可
导入模块
1 2 3 4 5 6
| from time import ctime,time import time as t import shutil import os.path from shutil import copyfile from os import system
|
这边说一下具体思路
就是通过system命令行执行git指令
git上传
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| def get_time(): """ 获取当前时间 """ return ctime()
def create_git_order(time): """ 生成git指令并执行 """ order_arr = ["git add E:/Github/adeptmicors279.github.io","git commit -m " + '"' + time + '"',"git push origin main"] for order in order_arr: system(order)
def put_file(time): """ 在桌面放置文件 """ file = open(r"C:\Users\1\Desktop\git_push_time.txt","w") file.write(time + "完成最后一次提交")
|
这边我也是直接复制的,大致意思是创建命令列表并遍历执行
配套的执行代码是
1 2 3 4
| if __name__ == "__main__": date = get_time() create_git_order(date) put_file(date)
|
创建文件并写入开头
我用的是NexT主题,开头格式是
所以直接上代码
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
| struct = t.localtime() year = struct[0] month = struct[1] date = struct[2] hour = struct[3] minute = struct[4] second = struct[5] title = str(input('请输入标题:')) tags = str(input('请输入标签,标签用英文逗号隔开:')) tags = str(tags.split(',')) tags = tags.replace('[', '').replace(']', '').replace("'", "") text = f'''--- title: {title} date: {year}-{month}-{date} {hour}:{minute}:{second} tags: {tags} ---''' print(text) with open(title+'.md', 'w', encoding='utf-8') as f: f.write(text)
answer = input('是否完成编辑?1/0') if answer == '1': flag = True else: flag = False
|
获取时间并获取标题和tag,写入文件头
然后?然后就自己编辑去吧!
是否完成编辑用True和False方便易读
移动文件至hexo目录
1 2 3 4 5 6 7 8 9 10
| if flag == True: path = os.path.abspath(title+'.md') pathto = 'E:\\hexo\\blog\\source\\_posts\\'+title+'.md' copyfile('./'+title+'.md', pathto) answer1 = str(input('是否上传文件?1/0')) if answer1 == '1': sign = True else: print('取消上传,缓存至本地')
|
传入_post目录,准备上传
开始上传
打包
1 2 3
| if sign==True: os.chdir(r'E:\hexo\blog') system('hexo g')
|
一并写入命令集提交给os模块去
替换本地仓库
1 2 3 4
| t.sleep(10) shutil.rmtree(r'E:\Github\adeptmicors279.github.io\homepage') shutil.copytree(r'E:\hexo\blog\public', r'E:\Github\adeptmicors279.github.io\homepage') os.chdir(r'E:\Github\adeptmicors279.github.io')
|
替换掉之后,便可上传,记得设置工作目录
最后接上
1 2 3 4
| if __name__ == "__main__": date = get_time() create_git_order(date) put_file(date)
|
免密登录GitHub
对用户名和密码进行设置
咱就是说,你总不能推送上去一次,就输入一次密码吧
1 2
| git config --global user.name '用户名' git config --global user.email '用户邮箱'
|
在git里输入,配置一下
生成公钥和私钥
生成公钥和私钥的命令
生成的一般在你的user目录里的.ssh
没有?没有别问我,我润了
然后去GitHub上配置
这里不做赘述
代码总结
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
| from time import ctime, time import time as t import shutil import os.path from shutil import copyfile from os import system
def get_time(): """ 获取当前时间 """ return ctime()
def create_git_order(time): """ 生成git指令并执行 """ order_arr = ["git add E:/Github/adeptmicors279.github.io", "git commit -m " + '"' + time + '"', "git push origin main"] for order in order_arr: system(order)
def put_file(time): """ 在桌面放置文件 """ file = open(r"C:\Users\1\Desktop\git_push_time.txt", "w") file.write(time + "完成最后一次提交")
struct = t.localtime() year = struct[0] month = struct[1] date = struct[2] hour = struct[3] minute = struct[4] second = struct[5] title = str(input('请输入标题:')) tags = str(input('请输入标签,标签用英文逗号隔开:')) tags = str(tags.split(',')) tags = tags.replace('[', '').replace(']', '').replace("'", "") text = f'''--- title: {title} date: {year}-{month}-{date} {hour}:{minute}:{second} tags: {tags} ---''' print(text) with open(title+'.md', 'w', encoding='utf-8') as f: f.write(text)
answer = input('是否完成编辑?1/0') if answer == '1': flag = True else: flag = False
if flag == True: path = os.path.abspath(title+'.md') pathto = 'E:\\hexo\\blog\\source\\_posts\\'+title+'.md' copyfile('./'+title+'.md', pathto) answer1 = str(input('是否上传文件?1/0')) if answer1 == '1': sign = True else: print('取消上传,缓存至本地')
if sign == True: os.chdir(r'E:\hexo\blog') system('hexo g') shutil.rmtree(r'E:\Github\adeptmicors279.github.io\homepage') shutil.copytree(r'E:\hexo\blog\public', r'E:\Github\adeptmicors279.github.io\homepage') os.chdir(r'E:\Github\adeptmicors279.github.io') if __name__ == "__main__": date = get_time() create_git_order(date) put_file(date)
|
最后…..没有最后了
再不睡觉明天就要上课睡了
see you tomorrow~