Browse Source

make IA searches a bit better

master
wes 8 years ago
parent
commit
37cd50f5fb
  1. 7
      src/archive.py
  2. 4
      src/scripts/book.tag
  3. 6
      src/search.py

7
src/archive.py

@ -5,14 +5,14 @@ from json import loads, dumps
import requests as req
searchUrl = "https://archive.org/advancedsearch.php?q={0}&fl%5B%5D=avg_rating&fl%5B%5D=description&fl%5B%5D=identifier&fl%5B%5D=type&sort%5B%5D=&sort%5B%5D=&sort%5B%5D=&rows=50&page=1&output=json&callback=callback&save=yes#raw"
searchUrl = "https://archive.org/advancedsearch.php?q={0}&fl%5B%5D=avg_rating&fl%5B%5D=description&fl%5B%5D=identifier&fl%5B%5D=mediatype&fl%5B%5D=type&fl%5B%5D=type&sort%5B%5D=&sort%5B%5D=&sort%5B%5D=&rows=50&page=1&output=json&callback=callback&save=yes#raw"
def searchIA(title, author):
"""
Do a search on The Internet Archive for a book
"""
print("running a search")
requrl = searchUrl.format(quote(title + " " + author))
requrl = searchUrl.format(quote(title) + " AND " + quote(author))
try:
results = loads(req.get(requrl).text[9:][0:-1])
except ValueError:
@ -24,7 +24,8 @@ def searchIA(title, author):
return []
docs = results["response"]["docs"]
urls = []
for result in results["response"]["docs"][0:3]:
for result in [r for r in results["response"]["docs"][0:10] if r["mediatype"] == "texts"]:
print(result)
urls.append("https://archive.org/details/%s" % result["identifier"])
return urls

4
src/scripts/book.tag

@ -9,14 +9,14 @@
</div>
<p if={ iarchive }>
<a target="_blank" href="{ iarchive }">
<button class="centered btn btn-link">
<button class="centered btn">
Internet Archive Result
</button>
</a>
</p>
<p if={ openlib }>
<a target="_blank" href="{ openlib }">
<button class="centered btn btn-link">
<button class="centered btn">
Open Library Result
</button>
</a>

6
src/search.py

@ -20,8 +20,8 @@ es = elasticsearch.Elasticsearch()
def summarize(text):
splitted = text.split(" ")
if len(splitted) > 4:
return " ".join(splitted[0:4]) + ".."
if len(splitted) > 6:
return " ".join(splitted[0:6]) + ".."
return text
def sectionToJSON(section):
@ -204,7 +204,7 @@ def searchTerms(terms):
if obj.books:
secs["books"] = [
{
"booktitle" : summarize(book[0]),
"booktitle" : book[0],
"bookauthor" : book[1],
"bookprice" : book[2]
}

Loading…
Cancel
Save