-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
77 lines (60 loc) · 3.05 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import Flask, jsonify, request
import requests
import datetime
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route('/current', methods=['POST'])
def get_current_data():
try:
# Get the league id, swid, and espn_s2 values from the POST body
data = request.get_json()
league_id = data.get('leagueId')
swid = data.get('swid')
espn_s2 = data.get('espn_s2')
# Check if all parameters are provided
if league_id is None or swid is None or espn_s2 is None:
return jsonify({'error': 'Missing one or more parameters'}), 400
# Define variables for ESPN fantasy api request
current_year = str(datetime.datetime.now().year)
base_url = 'https://lm-api-reads.fantasy.espn.com/apis/v3/'
current_season_endpoint = 'games/ffl/seasons/'+current_year+'/segments/0/leagues/'+league_id+'?view=mMatchupScore&view=mTeam&view=mSettings'
# Make the request
response = requests.get(url=base_url+current_season_endpoint, verify=False, cookies={'swid': swid, 'espn_s2': espn_s2})
espn_data = response.json()
# Check if data is valid (this is just a placeholder condition)
if not espn_data:
return jsonify({'error': 'No espn data found'}), 404
return jsonify(espn_data), 200
except requests.RequestException as e:
return jsonify({'error': str(e)}), 500
except Exception as e:
return jsonify({'error': 'An unexpected error occurred: ' + str(e)}), 500
@app.route('/historic', methods=['POST'])
def get_historic_data():
try:
# Get the league id, swid, and espn_s2 values from the POST body
data = request.get_json()
league_id = data.get('leagueId')
swid = data.get('swid')
espn_s2 = data.get('espn_s2')
# Check if all parameters are provided
if league_id is None or swid is None or espn_s2 is None:
return jsonify({'error': 'Missing one or more parameters'}), 400
# Define variables for ESPN fantasy api request
current_year = str(datetime.datetime.now().year)
base_url = 'https://lm-api-reads.fantasy.espn.com/apis/v3/'
historic_season_endpoint = 'games/ffl/leagueHistory/'+league_id+'?view=mLiveScoring&view=mMatchupScore&view=mRoster&view=mSettings&view=mStandings&view=mStatus&view=mTeam&view=modular&view=mNav&seasonId='
# Make the request
response = requests.get(url=base_url+historic_season_endpoint, verify=False, cookies={'swid': swid, 'espn_s2': espn_s2})
espn_data = response.json()
# Check if data is valid (this is just a placeholder condition)
if not espn_data:
return jsonify({'error': 'No espn data found'}), 404
return jsonify(espn_data), 200
except requests.RequestException as e:
return jsonify({'error': str(e)}), 500
except Exception as e:
return jsonify({'error': 'An unexpected error occurred: ' + str(e)}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)