-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
106 lines (84 loc) · 3.04 KB
/
main.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from flask import Flask, render_template, request, jsonify
from lyricsgenius import Genius
import requests
import json
from init_word_db import *
import random
import lyricModule
app = Flask(__name__)
secretFile = json.load(open("secret.json"))
genius = Genius(secretFile["clientAccessToken"], timeout = 15)
currentWord = ""
guessed = []
class Guess():
def __init__(self, guess_name, song_name, is_correct, song_artist):
self.song_name = song_name
self.is_correct = is_correct
self.guess_name = guess_name
self.song_artist = song_artist
guesses = []
@app.route('/timer-done', methods=['GET'])
def timerDone():
global guesses
print(guessed)
if len(guesses) == 0:
print("No guesses")
return jsonify(error = "No guesses"),
correct_guesses = [guess for guess in guesses if guess.is_correct]
incorrect_guesses = [guess for guess in guesses if not guess.is_correct ]
response = {
"correct_count": len(correct_guesses),
"incorrect_count": len(incorrect_guesses),
"correct_songs": [{"song_name": guess.song_name + " by " + guess.song_artist} for guess in correct_guesses],
"incorrect_songs": [{"song_name": guess.song_name + " by " + guess.song_artist} for guess in incorrect_guesses]
}
guesses = [] # resets guesses for next game
return jsonify(response)
@app.route('/get-word', methods=['GET'])
def get_word():
# put the dictionary api here, this is just dummy code for me
words = word_list(150)
global currentWord
currentWord = random.choice(words)
return jsonify(word=currentWord)
def fetch_song(song_name, retries=2): # 2 retries by default
for i in range(retries):
try:
song = genius.search_song(song_name)
if song != None:
if song.id in guessed:
print("Your can't guess the same thing more then once!")
return "nuh uh"
guessed.append(song.id)
print("Added " + str(song.id) + " to gussed songs.")
return song
except requests.exceptions.ReadTimeout:
print(f"Request timeout. Retry attempt {i+1}...")
print(f"Failed to fetch song after {retries} attempts.")
return None
@app.route('/validate-song', methods=['POST'])
def validate_song():
song_name = request.json.get('song_name')
ourSong = fetch_song(song_name)
is_correct = 0 # 3 means already guessed
if type(ourSong) == str:
is_correct = 3
else:
if ourSong:
if lyricModule.songContains(ourSong.id, currentWord):
is_correct = 1
else:
is_correct = 0
#print(ourSong.lyrics)
else:
print("Song fetch failed")
is_correct = 0
guess = Guess(song_name, ourSong.title, is_correct, ourSong.artist)
global guesses
guesses.append(guess)
return jsonify(is_correct=is_correct)
@app.route('/')
def index():
return render_template('game.html')
if __name__ == '__main__':
app.run(debug=True)