Hexo博客同步工具

Blog

點擊跳轉到個人博客查看原文python

Why write this script?

正常咱們要同步博客內容時都須要先打開命令窗口(Windows),而後調轉到博客目錄,而後輸入hexo命令清除以前生成的緩存,而後生成靜態文件,最後在部署到repository,很繁瑣,這時候若是有個圖標點擊幾下就部署到respository多舒服。。。因此就心血來潮。。。嗯。。。 PS:腳本只有在windows 10下1080p分辨率屏幕的電腦測試過,Python版本爲3.6.3git

Screen capture

腳本運行後會打開一個界面和一個命令行窗口 windows


Function

  • Generator 執行'hexo clean'和'hexo g',清理以前生成的靜態文件再從新生成靜態文件緩存

  • Deploy 執行'hexo d',將生成的靜態文件部署到雲端hexo

  • GeneratorAndDeploy 直接自動執行前面兩個命令,清理靜態文件-->從新生成靜態文件-->部署到雲端oop

  • Server 執行'hexo s',啓動本地模擬(本地預覽博客),默認端口爲博客配置設置的,沒有設置則爲:localhost:4000 PS:使用該按鈕以前記得先使用Gnerator按鈕測試

  • ServerWithCustomPort 若是默認端口被佔用,則可在下面的輸入另外的端口,端口位4位數字,而後點擊‘ServerWithCustomPort’便可開始自定義端口的本地模擬 PS:使用該按鈕以前記得先使用Gnerator按鈕this

  • 最後一個表情按鈕 你猜spa

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()

複製代碼
相關文章
相關標籤/搜索