Last active
December 25, 2015 09:19
-
-
Save macolyte/6953016 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import os | |
import sys | |
import webbrowser | |
from dropboxlogin import get_client | |
client = get_client() | |
f = client.get_file('/path/to/Library.txt') | |
books = f.read().split('\n') | |
template = '.*(%s).*' | |
query = sys.argv[1] | |
# I ganked this from http://www.cademuir.eu/blog/2011/10/20/python-searching-for-a-string-within-a-list-list-comprehension/ | |
regex = re.compile((template % query), re.IGNORECASE) | |
results = [m.group(0) for l in books for m in [regex.search(l)] if m] | |
if results: | |
content = """ | |
<table border=1> | |
<tr> | |
<th>Author</th> | |
<th>Title</th> | |
</tr> | |
{rows} | |
</table> | |
""".format(rows = "\n".join(['<tr>\n\t<td>{0}</td>\n\t<td>{1}</td>\n</tr>'.format(*i.split('\t')) for i in results])) | |
else: | |
content = """ | |
<p>No results found</p> | |
""" | |
html = """ | |
<html> | |
<head> | |
<title>Search Results</title> | |
</head> | |
<body> | |
{content} | |
</body> | |
</html> | |
""".format(content=content) | |
with open("results.html", 'w') as f: | |
f.write(html) | |
pth = "file:///" + os.path.join(os.path.dirname(os.path.abspath(__file__)), "results.html") | |
webbrowser.open(pth) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment