您的位置:首页 > 编程语言 > Python开发

Hexo-blogs-to-add-search-and-the-python-scripts-record-automatically

2016-11-09 17:50 429 查看
title: hexo博客添加搜索以及各自动python脚本记录

date: 2016/10/18 20:46:25

description: hexo博客添加搜索以及各自动python脚本记录,使用的是hexo-generator-search插件.

tags:

- python

- hexo

categories: python

hexo博客添加搜索以及各自动python脚本记录

小五游侠 2016-10-18 16:21:16

hexo搜索

使用的是hexo-generator-search插件,安装方法见

https://github.com/PaicHyperionDev/hexo-generator-search

安装好之后,如果你的笔记文件是中文名的话,应该不会搜出结果,使用浏览器查看search.xml文件会报错,导致不能搜索

解决方法:将中文文件名全部转成英文命名的文件,文件太多,写了一个py脚本自动讲中文文件名转换成英文文件名,执行前请做好备份,确认电脑py2.7环境正常,其中注意修改文件路径

# -*- coding: cp936 -*-
import os
import json
import urllib
import urllib2
import sys
from urllib import quote

path = 'E:\\Desktop\\files\\nodejs\\hexoBlog\\source\\_posts'

class Youdao:
def get_translation(self,words):
url = "http://fanyi.youdao.com/openapi.do?keyfrom=testappccw01&key=1820729705&type=data&doctype=json&version=1.1&q="
url = url+urllib.quote(words.decode(sys.stdin.encoding).encode('utf8'))
result = urllib2.urlopen(url).read()
json_result = json.loads(result)
json_result = json_result["translation"]
return json_result[0]

youdao = Youdao()

for file in os.listdir(path):
if os.path.isfile(os.path.join(path,file)) and len(file.split('.md'))>0:
newname=(youdao.get_translation(file.split('.md')[0])+'.md').replace(' ','-')
os.rename(os.path.join(path,file),os.path.join(path,newname))
print newname,'ok'
print file,'ok'


修改好之后,还要注意文件名也要不包含特殊字符

创建Hexo笔记脚本

每次新建笔记都需要修改文件格式,很麻烦,下面这个脚本可自动创建文件并且重命名英文文件名,生成hexo格式的笔记文件,使用markdown pad2打开,注意markdown软件的路径

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# encoding=utf8
import sys
import codecs
import os
import json
import urllib
import urllib2
import time
from urllib import quote

path = os.path.abspath('.')

class Youdao:
def get_translation(self,words):
url = "http://fanyi.youdao.com/openapi.do?keyfrom=testappccw01&key=1820729705&type=data&doctype=json&version=1.1&q="
#url = url+urllib.quote(words.decode(sys.stdin.encoding).encode('utf8'))
url = url+urllib.quote(words.encode('utf8'))
result = urllib2.urlopen(url).read()
json_result = json.loads(result)
json_result = json_result["translation"]
return json_result[0]

youdao = Youdao()

title = raw_input("Please input a title: ").decode(sys.stdin.encoding)
tags = raw_input("Please input a tags(split by -): ").decode(sys.stdin.encoding)
category = raw_input("Please input a category: ").decode(sys.stdin.encoding)
newname=(youdao.get_translation(title)).replace(' ','-')+'.md'
file=codecs.open(newname,"w","utf-8")
print newname,'ok'
file.write("---")
file.write("\r\n")
file.write("\r\n")
file.write("title: "+title)
file.write("\r\n")
file.write("tags: ")
file.write("\r\n")
tags2 = tags.split("-")
for item in tags2:
file.write("    - "+item)
file.write("\r\n")
print item
file.write("categories: "+category)
file.write("\r\n")
file.write("\r\n")
file.write("---")
file.write("\r\n")
file.write("\r\n")
file.write("\r\n")
file.write("\r\n")
file.write("<!--more-->")
file.write("\r\n")
file.write("\r\n")
file.write("## "+title+" ##")
file.write("\r\n")
file.write("\r\n")
file.write("-----------")
file.write("\r\n")
file.write("\r\n")
file.write(u"*[小五游侠](http://895474150.qzone.qq.com)* ")
file.write(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
file.write("\r\n")
file.write("\r\n")
file.close()
os.system("G:\dev-software\mardown2.4.2\MarkdownPad2.exe "+path+"\\"+newname)
print "D:\my file\dev-software\mardown2.4.2\MarkdownPad2.exe "+path+"\\"+newname
raw_input("any key quit.")


上传更新coding page

自动上传更新py脚本,注意博客文件所在路径

import os
import sys
p = os.popen('hexo clean',"r")
while 1:
line = p.readline()
if not line: break
print line

p = os.popen('hexo g',"r")
while 1:
line = p.readline()
if not line: break1
print line
ss = raw_input("local server or deploy(1 or 2): ").decode(sys.stdin.encoding)
if ss == '1':
p = os.popen('hexo s',"r")
while 1:
line = p.readline()
if not line: break
print line
if ss == '2':
p = os.popen('hexo d',"r")
while 1:
line = p.readline()
if not line: break
print line
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐