Skip to content

Instantly share code, notes, and snippets.

@noexpect
Last active January 1, 2016 23:29
Show Gist options
  • Save noexpect/8216567 to your computer and use it in GitHub Desktop.
Save noexpect/8216567 to your computer and use it in GitHub Desktop.
自宅電子書籍済リストをブラウザから検索するもの。 年越しプログラミング2013-2014課題。ブラウザのブックマークレット(bookseek.js)かブラウザからのクエリ入力(選択テキストorプロンプト入力)。 ローカルのbottle使ったpython製のwebサーバ(book_seek.py)が自宅電子書籍済リストからクエリでカスタムgrep(タイトルでマッチしたらその下のネストも表示)。 読み込みファイル(aList.htmlはサンプル)はsjisです。tabインデント1段目がタイトルグループ、2段目が各本のタイトルな感じ。
# -*- coding:utf-8 -*-
from bottle import route, run
import codecs
import sys
import re
reload(sys)
sys.setdefaultencoding('shift_jis')
def grep(itr, query):
result = u""
child_flag = False
for line in itr.xreadlines():
if re.search(query.decode("utf"), unicode(line), re.I):
result = result + " %s" % line
if line[1] != "\t":
child_flag = True
elif child_flag:
if line[1] == "\t":
result = result + " %s" % line
else:
child_flag = False
return result
def grep_file(file_name, query):
results = u""
file = codecs.open(file_name, "r", "shift_jis")
results = grep(file, query)
file.close()
return results
@route('/bookseek/<query>')
def bookseek(query = "クエリは未指定です"):
result = ""
file_list = ["C:\\hoge\\aList.html",
"C:\\hoge\\bList.html",
"C:\\hoge\\cList.html",
"C:\\hoge\\dList.html"]
for file in file_list:
result = result + file + "\n" + grep_file(file, query)
return "query:"+ query.decode("utf_8") + "<pre>" + result + "</pre>"
run(host='localhost', port=8888, debug=True, reloader=True)
javascript:(function(){
var q = "";
q = window.getSelection().toString();
if(q == "") {
q = prompt('Input your search words.', q);
}
if(q) {
q=q.split(" ")[0].split("\]")[0].split("\[")[0].split("\(")[0].split("\)")[0].split("\.")[0];
var iframe = document.createElement('iframe');
iframe.style.zIndex = 1000;
iframe.style.position = 'fixed';
iframe.style.backgroundColor = 'white';
iframe.style.borderColor = 'black';
iframe.style.left = '800px';
iframe.style.top = '50px';
iframe.width = '350';
iframe.height = '800';
iframe.src = 'http://localhost:8888/bookseek/' + q;
document.body.appendChild(iframe);
}
})();
<html><head><meta http-equiv='Content-Type' content='text/html; charset=Shift-JIS'><title>Book List</title></head><body><pre>
Update:Thu Jan 02 17:31:00 JST 2014
manga
[続巻]デスレス
Deathless_Volume01
Deathless_Volume02
Deathless_Volume03
Deathless_Volume04
[続巻]デンキ街の本屋さん
[水あさと] デンキ街の本屋さん 第01巻
[水あさと] デンキ街の本屋さん 第02巻
[水あさと] デンキ街の本屋さん 第03巻
[水あさと] デンキ街の本屋さん 第04巻
[水あさと] デンキ街の本屋さん 第05巻
End of List</pre></body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment