Skip to content

Commit

Permalink
Add firestore functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-hsieh committed Dec 5, 2023
1 parent 00d48fc commit 945457d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
21 changes: 10 additions & 11 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
from fantasy_app.functions import get_current_matchups, current_week, get_most_position_points, order_positions_by_points
from fantasy_app.contest_list import contests
import firebase_admin
from firebase_admin import credentials, db
from firebase_admin import credentials, firestore

load_dotenv()
DATABASE_URL = os.getenv('DATABASE_URL')
CREDS = os.getenv('CREDS')
# DATABASE_URL = os.getenv('DATABASE_URL')
CREDS = os.getenv('CREEDS')

cred = credentials.Certificate(CREDS)
if not firebase_admin._apps:
firebase_admin.initialize_app(cred, {
'databaseURL': DATABASE_URL
})
firebase_admin.initialize_app(cred)


# app.config['SQLALCHEMY_DATABASE_URI'] = config('DATABASE_URL')
Expand Down Expand Up @@ -72,23 +70,24 @@ def calculate_most_points_get():
contests[stat]['position_list'], current_week()-1)
contest_dict['contest_results'] = order_positions_by_points(points)
contest_dict['contest'] = stat

return jsonify(contest_dict)


@app.route('/most_points_post', methods=['POST'])
def calculate_most_points_post():
output_dict = {}
contest_dict = {}
point_request = request.get_json()
stat = point_request['contest']
week = point_request['week']
week = int(point_request['week'])
year = point_request['year']
points = get_most_position_points(
contests[stat]['position_list'], week)
contest_dict['contest_results'] = order_positions_by_points(points)
contest_dict['contest'] = stat
contest_dict['week'] = week
output_week = "week_" + str(week)
output_dict[output_week] = contest_dict
ref = db.reference("/" + str(year))
ref.push(output_dict)
db = firestore.client()
doc_ref = db.collection(year).document(output_week)
doc_ref.set(contest_dict)
return '{"status":"200", "data": "OK"}'
15 changes: 7 additions & 8 deletions fantasy_app/functions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import json
# import json
import operator
from datetime import datetime
from dotenv import load_dotenv
Expand All @@ -8,12 +8,14 @@

# load environment variables
load_dotenv()
LEAGUE_ID = os.getenv('LEAGU2')
SWID = os.getenv('SWID')
LEAGUE_ID = os.getenv('LEAGUE_ID')
ESPN_S2 = os.getenv('ESPN_S2')
SWID = os.getenv('SWID')


# retrieve football year


def get_year():
currentMonth = datetime.now().month
if currentMonth > 7:
Expand Down Expand Up @@ -145,11 +147,8 @@ def new_dict(position, current_dict, lineup):


def get_most_position_points(position, currentweek=0):
if currentweek > 0:
matchups = league.box_scores(currentweek)
else:
matchups = get_current_matchups()
currentweek = current_week()
matchups = league.box_scores(currentweek)

matchups_list = []

for matchup in matchups:
Expand Down

0 comments on commit 945457d

Please sign in to comment.