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

来源:互联网 发布:echo linux 编辑:程序博客网 时间:2024/05/05 23:19

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 osimport jsonimport urllibimport urllib2import sysfrom urllib import quotepath = '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=utf8import sysimport codecsimport osimport jsonimport urllibimport urllib2import timefrom urllib import quotepath = 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 itemfile.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+"\\"+newnameraw_input("any key quit.")

上传更新coding page

  • 自动上传更新py脚本,注意博客文件所在路径
import osimport sysp = os.popen('hexo clean',"r")while 1:    line = p.readline()    if not line: break    print linep = os.popen('hexo g',"r")while 1:    line = p.readline()    if not line: break1    print liness = 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 lineif ss == '2':    p = os.popen('hexo d',"r")while 1:line = p.readline()if not line: breakprint line
0 0
原创粉丝点击