from flask import Flask, render_template, redirect
from flask_pymongo import PyMongo
import scrape_mars
import pymongo

app = Flask(__name__)

# Use flask_pymongo to set up mongo connection
# app.config["MONGO_URI"] = "mongodb://localhost:27017/mars_app_db"
# mongo = PyMongo(app)

# Create connection variable
# conn = 'mongodb://localhost:27017'
# Pass connection to the pymongo instance.
# client = pymongo.MongoClient(conn)


app.config["MONGO_URI"] = "mongodb://localhost:27017/mars_app_db"
mongo = PyMongo(app)


@app.route("/")
def index():
    listings = mongo.db.listings.find_one()
    return render_template("index.html", listings=listings)


@app.route("/scrape")
def scraper():
    listings = mongo.db.listings
    # listings_data = scrape_mars.data_scrape()
    listings_data = scrape_mars.scrape()
    mongo.db.listings.update({}, listings_data, upsert=True)
    return redirect("/", code=302)




if __name__ == "__main__":
    app.run(debug=True)