Browse Source

make IA searches a bit better

master
wes 9 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 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): def searchIA(title, author):
""" """
Do a search on The Internet Archive for a book Do a search on The Internet Archive for a book
""" """
print("running a search") print("running a search")
requrl = searchUrl.format(quote(title + " " + author)) requrl = searchUrl.format(quote(title) + " AND " + quote(author))
try: try:
results = loads(req.get(requrl).text[9:][0:-1]) results = loads(req.get(requrl).text[9:][0:-1])
except ValueError: except ValueError:
@ -24,7 +24,8 @@ def searchIA(title, author):
return [] return []
docs = results["response"]["docs"] docs = results["response"]["docs"]
urls = [] 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"]) urls.append("https://archive.org/details/%s" % result["identifier"])
return urls return urls

4
src/scripts/book.tag

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

6
src/search.py

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

Loading…
Cancel
Save