-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsongservice.py
More file actions
51 lines (43 loc) · 1.52 KB
/
songservice.py
File metadata and controls
51 lines (43 loc) · 1.52 KB
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
import datetime
import flask
import os
import songmaker
app = flask.Flask(__name__)
song_writer = None
WORD_POOL_SIZE = 50000
MIN_LETTERS = 4
MAX_LETTERS = 9
def main():
global song_writer
text_path = os.path.join(os.path.dirname(__file__), 'metamorphosis.txt')
reader = songmaker.Reader(open(text_path, 'r'))
reader.parse_text()
generator = songmaker.WordGenerator(
reader,
min_letters=MIN_LETTERS,
max_letters=MAX_LETTERS,
allow_doubles=False)
loads_of_words = [generator.generate_word() for i in range(WORD_POOL_SIZE)]
song_writer = songmaker.SongWriter(loads_of_words)
@app.route("/")
def get_song():
scheme = flask.request.args.get('scheme')
min_syllables = int(flask.request.args.get('minSyllables', 1))
max_syllables = int(flask.request.args.get('maxSyllables', 4))
while True:
try:
song = song_writer.get_song(scheme, min_syllables, max_syllables)
resp = flask.make_response(flask.jsonify({'songlines': song}))
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
except IndexError:
continue
except songmaker.NoValidRhymeGroupsFound:
resp = flask.make_response(flask.jsonify(
{'songlines': ['could not create a song with those parameters']}))
resp.headers['Access-Control-Allow-Origin'] = '*'
return resp
if __name__ == "__main__":
main()
print 'ready'
app.run(host='0.0.0.0', debug=True, use_reloader=False)