Hexo部落格同步工具

smileorigin發表於2019-03-01

Blog

點選跳轉到我的部落格檢視原文

Why write this script?

正常我們要同步部落格內容時都需要先開啟命令視窗(Windows),然後調轉到部落格目錄,然後輸入hexo命令清除之前生成的快取,然後生成靜態檔案,最後在部署到repository,很繁瑣,這時候如果有個圖示點選幾下就部署到respository多舒服。。。所以就心血來潮。。。嗯。。。
PS:指令碼只有在windows 10下1080p解析度螢幕的電腦測試過,Python版本為3.6.3

Screen capture

指令碼執行後會開啟一個介面和一個命令列視窗

Hexo部落格同步工具

Hexo部落格同步工具

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:指令碼要放在部落格目錄裡才可使用,建立一個指令碼的快捷方式到桌面就不用每次都到部落格目錄執行指令碼了

    Hexo部落格同步工具
  • 注意
    本指令碼沒有使用子執行緒去執行耗時操作,所以在執行一項操作時不要再點選其他按鈕,不然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
Blog: smileorigin.site
Email: 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()

複製程式碼

相關文章