Skip to content

Instantly share code, notes, and snippets.

@macolyte
Last active December 25, 2015 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macolyte/6953016 to your computer and use it in GitHub Desktop.
Save macolyte/6953016 to your computer and use it in GitHub Desktop.
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