使用Python封装hexo博客框架的shell脚本,实现运行Python程序就能自动化创建hexo博客、同时实现随机给出博客使用的缩略图的URL。
最终效果展示
- Python创建脚本效果
 
- 创建后生成的文件效果
 
- 
创建后生成的缩略图效果 此处需要注意,你的hexo主题文件要支持博客的缩略的才可以,我当前使用的主题是pure,缩略图需要存放在主题的source文件夹下,如下图。 
 
| md文件头 | 效果 | 
|---|---|
| — title: 使用Python自动创建Hexo博客并生成博客缩略图 thumbnail_url: /images/default_img/blog-default-4.jpg date: 2021-08-03 10:35:29 — |  | 
Python脚本自动创建Hexo博客并生成博客缩略图过程
- 
首先在项目的根目录下创建main.py文件,当然名字也可以自己定。 
- 
创建项目目录检查函数,防止main.py不是在根目录下执行。 1 
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16import os 
 import random
 # 此处的dev就是项目的根目录名称,需要根据自己的情况进行命名
 def _mkdir(path=None, root_name='dev'):
 """获取项目根目录路径以及根目录下指定path的路径"""
 root = os.path.dirname(__file__)
 if root.split('/')[-1] != root_name:
 print('项目根目录不正确,请检查')
 else:
 if path is None:
 return root
 else:
 root_nex = os.path.join(root, path)
 return root_nex
- 
修改博客默认的缩略图。前边提到的创建博客后会生成默认的缩略图,但所有的博客都使用同一个缩略图会不太美观,于是就有了针对缩略图自动修改的Python脚本。 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
 28def _random_blog_images(default_path='themes/pure/source/images/default_img', prd_path='/images/default_img/'): 
 """获取blog缩略图默认的图片列表并随机选择一个,拼接生成线上使用的URL"""
 mkdir = _mkdir(default_path)
 default_image_list = get_file_path(mkdir)
 default_images = random.choice(default_image_list).split('/')[-1]
 image_path = prd_path + default_images
 return image_path
 # 'thumbnail_url: '是你在post.md模板文件头中创建的缩略图变量(字段)名称
 def _revise_blog_thumbnail_url(blog_path, blog_thumbnail='thumbnail_url: '):
 """创建新博客后,输入新博客的路径信息修改默认的缩略图URL"""
 print(' 修改博客缩略图URL')
 with open(blog_path) as f:
 blog = f.readlines()
 # 获取博客中默认缩略图的路径,下标
 thumbnail_url = [img for img in blog if blog_thumbnail in img]
 print(' 创建的博客中使用的缩略图:', thumbnail_url[0])
 thumbnail_url_index = blog.index(thumbnail_url[0])
 # 获取随机缩略图,替换博客中默认的thumbnail_url
 random_blog_images = _random_blog_images() + '\n'
 random_blog_images = blog_thumbnail + random_blog_images
 blog[thumbnail_url_index] = random_blog_images
 print(' 修改后博客中使用的缩略图:', random_blog_images)
 with open(blog_path, 'w+') as w:
 w.writelines(blog)
 print(' 博客缩略图URL修改成功')
- 
定义一个交互函数,让运行main.py文件时能够进行输入操作。 下边的"2 编译博客内容并上传到Github仓库"暂且不表,以后有时间再展开讲,计划是实现博客内容中的图片URL替换、搜索引擎的自动提交等等吧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
 37def run_code(): 
 types = """
 该程序提供如下服务内容:
 1 创建新博客
 2 编译博客内容并上传到Github仓库
 
 请输入需要操作的序号:
 """
 print(types)
 inputs = input()
 if 1 <= int(inputs) <= 2:
 # 1 创建新博客
 if int(inputs) == 1:
 os.system('cd {0}'.format(_mkdir()))
 create_blog_name, create_blog_id, create_blog_path = input('请输入文章标题:'), input('请输入文章ID(url使用):'), input('请输入文章所属文件夹名称:')
 create_blog = create_blog_path + '/' + create_blog_id + ' "' + create_blog_name + '"'
 revise_url = create_blog_path + '/' + create_blog_id
 print('将创建博客内容:', create_blog)
 create_type = os.system('hexo new post --path {0}'.format(create_blog))
 if int(create_type) == 0:
 print('创建成功')
 _revise_blog_thumbnail_url('source/_posts/' + revise_url + '.md')
 else:
 print('创建异常,请检查')
 # 2 编译博客内容并上传到Github仓库
 if int(inputs) == 2:
 _revise_blog_img_url()
 os.system('cd {0}'.format(_mkdir()))
 # os.system('hexo clean && hexo deploy')
 os.system('hexo clean')
 os.system('hexo generate')
 os.system('cp -rf ../README.md public/')
 os.system('hexo deploy')
 else:
 print('输入错误,请输入(1-2)序号中需要操作的数字')
OK,使用Python自动创建Hexo博客并生成博客缩略图的完整内容就这些,非常简单且容易上手。
完整的代码如下:
| 1 | # @Author : 曲书贤 | 
如有问题,欢迎留言~
 
         
              