Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Wrote function to request and process data. Started implementing schedule module for automating the process.
  • Loading branch information
maryjng authored Jan 27, 2022
1 parent c7d85f8 commit 99fd955
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 22 deletions.
19 changes: 10 additions & 9 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from key import API_KEY, SECRET_KEY

from forms import UserAddForm, LoginForm, TrackItemForm
from models import db, connect_db, User, Item, Prices, Current_Shops
from models import db, connect_db, User, Item, Shops, Shops_Item, PriceHistory

BASE_URL = "https://api.originsro.org/api/v1/market/list"

Expand Down Expand Up @@ -40,7 +40,7 @@ def add_user_to_g():

@app.route("/home", methods=["GET"])
def index():
return render_template("index.html")
return render_template("home.html")


@app.route("/login", methods=["GET", "POST"])
Expand All @@ -58,15 +58,15 @@ def login():
return render_template("login.html", form=form)


@app.route("register", methods=["GET", "POST"])
@app.route("/register", methods=["GET", "POST"])
def register():
form = UserAddForm()

if form.validate_on_submit:
try:
user.signup(
username = form.username.data
email = form.username.data
User.signup(
username = form.username.data,
email = form.username.data,
password = form.password.data)

db.session.commit()
Expand All @@ -75,20 +75,21 @@ def register():

except:
flash("Invalid ")
return redirect("register.html", form=form)
return redirect("register.html")

return redirect("home.html")

return render_template("register.html", form=form)


@app.route("logout", methods=["POST"])
@app.route("/logout", methods=["POST"])
def logout():
if CURR_USER_KEY in session:
del session[CURR_USER_KEY]
redirect("/index.html")


@app.route("add", methods=["GET", "POST"])
@app.route("/add", methods=["GET", "POST"])
def add_item():
form = TrackItemForm()

Expand Down
90 changes: 90 additions & 0 deletions func.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import requests

from key import API_KEY
from models import db, connect_db, Item, Shops, Shops_Item, PriceHistory

###############################################################################
# def ping_API():
# return requests.get("https://api.originsro.org/api/v1/ping")
#
# def whoami():
# return requests.get("https://api.originsro.org/api/v1/whoami", params={"key": API_KEY})

# def get_all_items():
# items = requests.get("https://api.originsro.org/api/v1/items/list", params={"api_key": API_KEY})
# items_res = items.json()
# items_data = []
#
# for item in items_res["items"]:
# dict = {}
# dict["item_id"] = item["item_id"]
# dict["unique_name"] = item["unique_name"]
# dict["name"] = item["name"]
#
# items_data.append(dict)
#
# return items_data

#create table for static item data
def populate_items_db():
items = get_all_items()
for i in items:
row = Item(i)
db.session.add(row)
db.session.commit()

#automate this request to occur at set intervals
def get_current_data():
shops = requests.get("https://api.originsro.org/api/v1/market/list", params={"api_key": API_KEY})
shops_res = shops.json()

return shops_res

def organize_results(res):
shops_data = []
prices_data = []

#get Shops data
for shop in res["shops"]:
shops = {}
location = shop["location"]

shops["map_location"] = location["map"]
shops["map_x"] = location["x"]
shops["map_y"] = location["y"]
shops["title"] = shop["title"]
shops["owner"] = shop["owner"]

shops_data.append(shops)

#get PriceHistory data
for item in res["items"]:
prices = {}

prices["item_id"] = item["item_id"]
prices["cost"] = item["price"]
prices["owner"] = shop["owner"]
prices["timestamp"] = res["generation_timestamp"]

prices_data.append(prices)

return (shops_data, prices_data)

def store_res_data((shops, prices)):
all_shops = [Shops(shop) for shop in shops]
all_prices = [PriceHistory(price) for price in prices]

try:
db.session.add(all_shops)
db.session.add(all_prices)
db.session.commit()
except:
print("Did not work")

def request_and_store_data():
res = get_current_data()
(shops_data, prices_data) = organize_results(res)
store_res_data((shops_data, prices_data))
print("Done.")

schedule.every(15).minutes.do(request_and_store_data)
16 changes: 9 additions & 7 deletions models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,6 @@ class Item(db.Model):

current_shops = db.relationship("Shops_Item", backref="Item")

class Shops_Item(db.Model):
"""Items currently in shops and their prices"""

owner = db.Column(db.ForeignKey(Shops.owner), primary_key=True)
item_id = db.Column(db.ForeignKey(Item.id), primary_key=True)
price = db.Column(db.Integer, nullable=False)

class Shops(db.Model):
"""Shops currently open."""

Expand All @@ -72,12 +65,21 @@ class Shops(db.Model):
items = db.relationship("Shops_Item", backref="Shops")
#need to somehow include cost for each item

class Shops_Item(db.Model):
"""Items currently in shops and their prices"""

owner = db.Column(db.ForeignKey(Shops.owner), primary_key=True)
item_id = db.Column(db.ForeignKey(Item.id), primary_key=True)
price = db.Column(db.Integer, nullable=False)

class PriceHistory(db.Model):
"""Historical prices for all items"""

item_id = db.Column(db.Text, nullable=False)
timestamp = db.Column(db.DateTime, nullable=False)
#timestamp is WHEN the data was REQUESTED
cost = db.Column(db.Integer, nullable=False)
owner = db.Column(db.ForeignKey(Shops.owner), primary_key=True)

#only care about shop data if shops are from most recent request

Expand Down
9 changes: 5 additions & 4 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@
<body>
<nav>
{% if not g.user %}
<a href="">Login</a>
<a href="">Register</a>
<a href="/login">Login</a>
<a href="/register">Register</a>
{% else %}
<a href="">Home</a>
<a href="/home">Home</a>
<a href="">My Trackings</a>
<a href=""> | Logout</a>
<a href="/logout"> | Logout</a>
{% endif %}
</nav>

{% block content %}

{% endblock %}

</body>

</html>
2 changes: 1 addition & 1 deletion templates/home.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "base.html"}
{% extends "base.html" %}

{% block content %}

Expand Down
21 changes: 21 additions & 0 deletions templates/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends "base.html" %}
{% block content %}
<div>
<div>
<form method="POST" id="LoginForm">
{{ form.hidden_tag() }}

{% for field in form if field.widget.input_type != 'hidden' %}
{% for error in field.errors %}
<span class="text-danger">{{ error }}</span>
{% endfor %}
{{ field(placeholder=field.label.text, class="form-control") }}
{% endfor %}

<button class="btn btn-primary btn-block btn-lg">Login</button>
</form>
</div>
</div>


{% endblock %}
2 changes: 1 addition & 1 deletion templates/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
{% endfor %}
{% endif %}

{{ form.text(placeholder=" ") }}
{{ form.text }}

</div>
<button class>Add Item</button>
Expand Down

0 comments on commit 99fd955

Please sign in to comment.