Skip to content

Commit

Permalink
add gist post options
Browse files Browse the repository at this point in the history
  • Loading branch information
fcremer committed Nov 18, 2023
1 parent 4ba31ef commit 4032939
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
6 changes: 6 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@
from models import PinballMachine, Player, Score
from data_manager import load_data, save_data
import datetime
import os



app = Flask(__name__)
data = load_data()

print( os.environ['GIST_TOKEN'] )
print("Debug")

@app.route('/')

def index():
Expand Down
44 changes: 38 additions & 6 deletions data_manager.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
import yaml
import os
import requests
import json

DATA_PATH = "data.yaml"
GIST_ID = "8542e223cdd0ff01905aa79b439927bb" # Replace with your Gist ID
TOKEN = "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" # Replace with your GitHub token
GIST_FILENAME = "score.yaml"

headers = {"Authorization": f"token {TOKEN}"}

def fetch_gist_content():
url = f"https://api.github.com/gists/{GIST_ID}"
response = requests.get(url, headers=headers)
response.raise_for_status()
gist_data = response.json()
return gist_data['files'][GIST_FILENAME]['content']

def update_gist(content):
url = f"https://api.github.com/gists/{GIST_ID}"
data = {
"files": {
GIST_FILENAME: {
"content": content
}
}
}
response = requests.patch(url, headers=headers, data=json.dumps(data))
response.raise_for_status()
return response.json()

def load_data():
if not os.path.exists(DATA_PATH):
try:
content = fetch_gist_content()
# print("LOAD DEBUG"+content)
return yaml.safe_load(content)
except Exception as e:
return {"pinball_machines": [], "players": [], "scores": []}
with open(DATA_PATH, "r") as file:
return yaml.safe_load(file)

def save_data(data):
with open(DATA_PATH, "w") as file:
yaml.dump(data, file)
try:
content = yaml.dump(data)
update_gist(content)
#print("SAVECONTENT: " + data)
except Exception as e:
print(f"Error saving data: {e}")
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Flask
PyYAML
PyYAML
requests
6 changes: 4 additions & 2 deletions static/mobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ function submitScore() {
const pinball = document.getElementById('pinball-select').value;
const score = document.getElementById('score-input').value;

console.log(parseInt(score.replaceAll(",","")));

// Ermitteln des heutigen Datums im Format YYYY-MM-DD
const today = new Date();
const formattedDate = today.toISOString().split('T')[0];
Expand All @@ -66,7 +68,7 @@ function submitScore() {
body: JSON.stringify({
player_abbreviation: player,
pinball_abbreviation: pinball,
points: parseInt(score, 10),
points: parseInt(score.replaceAll(",","")),
date: formattedDate
})
})
Expand All @@ -84,7 +86,7 @@ function formatScoreInput(inputElement) {
let value = inputElement.value.replace(/[^\d]/g, '').replace(/^0+/, '');

// Teile die Zahl in Gruppen von drei Ziffern
value = value.replace(/\B(?=(\d{3})+(?!\d))/g, '.');
value = value.replace(/\B(?=(\d{3})+(?!\d))/g, ',');

// Setze den formatierten Wert zurück ins Eingabefeld
inputElement.value = value;
Expand Down
2 changes: 0 additions & 2 deletions static/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ document.addEventListener('DOMContentLoaded', function() {
});
}
});

let playerNamesMap = {};


function loadPlayers() {
fetch('/players')
.then(response => response.json())
Expand Down

0 comments on commit 4032939

Please sign in to comment.