Find Cheaper University Textbooks
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

148 lines
4.3 KiB

#! /usr/bin/python2
from functools import partial
from couchdb import ResourceConflict
from flask import Flask, render_template, flash, request, send_from_directory
from flask_bootstrap import Bootstrap
from flask_appconfig import AppConfig
from urllib import unquote
from search import searchTerms
from openlibrary import bookUrls
from archive import searchIA
from urllib import quote, unquote
from json import dumps, loads
from werkzeug.contrib.cache import MemcachedCache
cache = MemcachedCache(['127.0.0.1:11211'])
import os
def predict(fieldtype, term):
print fieldtype
print term
if not term:
return "[]"
else:
try:
cs = completers[fieldtype](term.lower())
except KeyError:
return "[]"
if cs:
return cs
return "[]"
def predictor(fieldtype):
def inner(request):
params = dict(request.args.items())
return predict(fieldtype, params["term"])
return inner
def cacheit(key, thunk):
"""
Tries to find a cached version of ``key''
If there is no cached version then it will
evaluate thunk (which must be a generator)
and cache that, then return the result
"""
cached = cache.get(quote(key))
if cached is None:
result = list(thunk())
cache.set(quote(key), result)
return result
return cached
def ClassSearch(configfile=None):
defaults = {"Day", "Building", "Exact Location", "Department"}
app = Flask(__name__)
AppConfig(app, configfile) # Flask-Appconfig is not necessary, but
# highly recommend =)
# https://github.com/mbr/flask-appconfig
Bootstrap(app)
app.config["scripts"] = "/home/wes/MGOAL/scripts"
app.config["styles"] = "/home/wes/MGOAL/styles"
@app.route('/favicon.ico')
def favicon():
return send_from_directory("/srv/http/goal/favicon.ico",
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route("/buildpred", methods=("GET", "POST"))
def buildpred():
return predictbuild(request)
@app.route("/locpred", methods=("GET", "POST"))
def locpred():
return predictloc(request)
@app.route("/daypred", methods=("GET", "POST"))
def daypred():
return predictday(request)
@app.route("/deptpred", methods=("GET", "POST"))
def deptpred():
return predictdept(request)
@app.route("/titlepred", methods=("GET", "POST"))
def titlepred():
return predicttitle(request)
@app.route("/", methods=("GET", "POST"))
def index():
return render_template("search.html")
@app.route("/fc", methods=("GET", "POST"))
def fc():
""" Filter Courses """
print "trying to get courses"
params = dict(request.args.items())
for key, val in params.iteritems():
if val in defaults:
del params[key]
results = searchTerms(params)
return results
@app.route("/resources", methods=("GET", "POST"))
def resources():
""" Get Resources """
notRequired = False
params = loads(dict(request.args.items())["data"])
print params
author = params["author"]
title = params["title"]
if ("No Textbooks" in title or
"No Adoption" in title):
return dumps("false")
# Cache the result of the open library search
openlib = cacheit("openlib"+title+author, lambda : bookUrls(title, author))
print openlib
# cache the result of an internet archive search
iarchive = cacheit("iarchive"+title+author, lambda : searchIA(title, author))
print iarchive
if not (any(openlib) or any(iarchive)):
# We literally could not find ANYTHING
return dumps("false")
return dumps({
"iarchive" : iarchive,
"openlib" : openlib
})
@app.route("/scripts/<filename>")
def send_script(filename):
return send_from_directory(app.config["scripts"], filename)
@app.route("/styles/<filename>")
def send_style(filename):
return send_from_directory(app.config["styles"], filename)
return app
if __name__ == "__main__":
ClassSearch().run(port=8001, debug=True)