Hexo博客同步工具

1,785 阅读4分钟

Blog

点击跳转到我的博客查看原文

Why write this script?

正常我们要同步博客内容时都需要先打开命令窗口(Windows),然后调转到博客目录,然后输入hexo命令清除之前生成的缓存,然后生成静态文件,最后在部署到repository,很繁琐,这时候如果有个图标点击几下就部署到respository多舒服。。。所以就心血来潮。。。嗯。。。 PS:脚本只有在windows 10下1080p分辨率屏幕的电脑测试过,Python版本为3.6.3

Screen capture

脚本运行后会打开一个界面和一个命令行窗口


Function

  • Generator 执行'hexo clean'和'hexo g',清理之前生成的静态文件再重新生成静态文件

  • Deploy 执行'hexo d',将生成的静态文件部署到云端

  • GeneratorAndDeploy 直接自动执行前面两个命令,清理静态文件-->重新生成静态文件-->部署到云端

  • Server 执行'hexo s',启动本地模拟(本地预览博客),默认端口为博客配置设置的,没有设置则为:localhost:4000 PS:使用该按钮之前记得先使用Gnerator按钮

  • ServerWithCustomPort 如果默认端口被占用,则可在下面的输入另外的端口,端口位4位数字,然后点击‘ServerWithCustomPort’即可开始自定义端口的本地模拟 PS:使用该按钮之前记得先使用Gnerator按钮

  • 最后一个表情按钮 你猜

Using(Windows10)

  • 安装Python3.6.3 点击跳转到官网下载

  • 安装 下载完,一路点击安装即可,最后的完成界面有个选项将python的目录添加到PATH环境变量记得勾选

  • 获取脚本 下载脚本(密码:s3r2)或者将下面的脚本内容复制,然后新建一个.py结尾的文件保存进去即可

  • 执行脚本 右键脚本-->选择打开方式-->选择其他应用-->找到Python的安装目录,选择python.exe双击即可,选定打开方式后记勾选设置为默认打开方式,下次直接双击打开即可。PS:脚本要放在博客目录里才可使用,创建一个脚本的快捷方式到桌面就不用每次都到博客目录运行脚本了

  • 注意 本脚本没有使用子线程去执行耗时操作,所以在执行一项操作时不要再点击其他按钮,不然windows会提示程序无响应可能导致脚本退出,请等待DOS窗口提示操作完成,或者是按下的按钮浮起来界面显示正常在进行其他操作 使用Server或者ServerWithCustomPort功能时,要退出本地模拟只要点击DOS窗口按CTRL+c快捷键组合就会提示是否退出,输入y即可退出,这时候会发现程序界面按钮恢复默认状态,可以继续进行其他操作,重新开启本地模拟或者部署等

Script

#!/usr/bin/env python

# encoding: utf-8

'''

@author: smileorigin

@license: (C) Copyright 2017

@contact: smileorigin@foxmail.com

@file: hexo_utils.py

@time: 12/3/2017 7:02 PM

@desc:

'''

# import
import os
from tkinter import *
from tkinter import messagebox


class MainView(Frame):
    bg = 'white'
    bt_bg = '#E91E63'
    text_color = '#fff'
    about_msg = 'Author: smileorigin\nBlog: smileorigin.site\nEmail: smileorigin@foxmail.com'

    def __init__(self, generatorCallback, deployCallback, serverCallback,
                 serverCustomPortCallback, generatorAndDeployCallback, master=None):
        Frame.__init__(self, master, bg=self.bg)
        # expand扩展frame背景充满整窗口
        self.pack(expand=YES, fill='both')
        self.createWidgets(generatorCallback, deployCallback, serverCallback,
                           serverCustomPortCallback, generatorAndDeployCallback)

    def createWidgets(self, generatorCallback, deployCallback, serverCallback,
                      serverCustomPortCallback, generatorAndDeployCallback):
        # four function button
        self.generator_bt = Button(self, command=generatorCallback, text='Generator', width=20,
                                   bg=self.bt_bg, fg=self.text_color)
        self.generator_bt.pack(pady=10, padx=20)

        self.deploy_bt = Button(self, command=deployCallback, text='Deploy', width=20,
                                bg=self.bt_bg, fg=self.text_color)
        self.deploy_bt.pack()

        self.generator_and_deploy_bt = Button(self, command=generatorAndDeployCallback,
                                              text='GeneratorAndDeploy', width=20,
                                              bg=self.bt_bg, fg=self.text_color)
        self.generator_and_deploy_bt.pack(pady=10)

        self.server_bt = Button(self, command=serverCallback, text='Server', width=20,
                                bg=self.bt_bg, fg=self.text_color)
        self.server_bt.pack()

        self.server_custom_port = Button(self, command=serverCustomPortCallback,
                                         text='ServerWithCustomPort', width=20, bg=self.bt_bg,
                                         fg=self.text_color)
        self.server_custom_port.pack(pady=8)

        self.custom_port_label = Label(self, text="Port:", bg=self.bg)
        self.custom_port_label.pack(fill='x')

        hint = StringVar()
        hint.set('5000')
        self.custom_port_entry = Entry(self, textvariable=hint, bg=self.bg)
        self.custom_port_entry.pack()

        self.about_bt = Button(self, text='(⓿_⓿)', command=self.showMessage, bg=self.bt_bg,
                               fg=self.text_color)
        self.about_bt.pack(pady=10)

    def showMessage(self):
        messagebox.showinfo('About', self.about_msg)


# --------------------------------------------------------------------------------------------------
# constant values
# --------------------------------------------------------------------------------------------------
# clean
cmd_clean = 'hexo clean'

# generator
cmd_generator = 'hexo g'

# deploy
cmd_deploy = 'hexo d'

# server
cmd_server = 'hexo s'

# windows width height
width = 230
height = 280

# star
star = '*'

# star num
star_num = 84

# icon path
# icon_path = '\\favicon.ico'

# out
generator_start_text = ' generator start '
generator_done_text = ' generator done '
deploy_start_text = ' deploy start '
deploy_done_text = ' deploy done '
server_start_text = ' server start '
server_done_text = ' server done'
welcome_text = ' welcome '


# --------------------------------------------------------------------------------------------------
# method
# --------------------------------------------------------------------------------------------------

# server with another port
# port  string  what's your port do you want to server
def serverWithAnotherPort(port):
    return cmd_server + ' -p ' + port


def executeCommand(cmd):
    os.system(command=cmd)


def regexFourNum(str):
    p = re.compile('^[0-9]{4}')
    return p.match(str)


def printStar(num):
    print(star * num)


def printStarNotEnter(num):
    print(star * num, end='')


def printStringWithStar(num, string):
    printStar(star_num)
    string_len = len(string)
    half_star_num = (int)((num - string_len) / 2)
    remainder = num - half_star_num * 2 - string_len
    printStarNotEnter(half_star_num + remainder)
    print(string, end='')
    printStar(half_star_num)
    printStar(star_num)


def generatorCallback():
    printStringWithStar(star_num, generator_start_text)
    executeCommand(cmd_clean)
    executeCommand(cmd_generator)
    printStringWithStar(star_num, generator_done_text)


def deployCallback():
    printStringWithStar(star_num, deploy_start_text)
    executeCommand(cmd_deploy)
    printStringWithStar(star_num, deploy_done_text)


def serverCallback():
    printStringWithStar(star_num, server_start_text)
    try:
        executeCommand(cmd_server)
    except KeyboardInterrupt:
        printStringWithStar(star_num, server_done_text)


def serverCustomPortCallback():
    custom_port = str(main_view.custom_port_entry.get())
    if custom_port:
        # port: just 4 number
        if regexFourNum and custom_port.__len__() == 4:
            printStringWithStar(star_num, server_start_text)

            try:
                executeCommand(serverWithAnotherPort(custom_port))
            except KeyboardInterrupt:
                printStringWithStar(star_num, server_done_text)
        else:
            messagebox.showinfo('Input error', 'Port needs 4 digits.Example:5000')
    else:
        # error hint input port
        messagebox.showinfo('Error', 'Please input port.Example:5000')


def generatorAndDeployCallback():
    generatorCallback()
    deployCallback()


# --------------------------------------------------------------------------------------------------
# run code
# --------------------------------------------------------------------------------------------------
printStringWithStar(star_num, welcome_text)
root = Tk()
main_view = MainView(generatorCallback, deployCallback, serverCallback, serverCustomPortCallback,
                     generatorAndDeployCallback, master=root)
root.title("")
root.resizable(0, 0)
size = '{}x{}+{}+{}'.format(
    width, height, (int)((root.winfo_screenwidth() - width) / 2),
    (int)((root.winfo_screenheight() - height) / 2))
root.geometry(size)
# root.iconbitmap(sys.path[0] + icon_path)
root.mainloop()