-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
305 lines (251 loc) · 8.96 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# -*- coding: utf-8 -*-
import os, config
from functools import wraps
from flask import (
Flask, request, json, jsonify,
render_template, send_from_directory,
session, url_for, redirect
)
from flask_oauthlib.client import OAuth
import mido
import melete.lyrics as Lyrics
import melete.rhythm as Rhythm
import melete.chord as Chord
import melete.melody as Melody
import melete.util as Util
app = Flask(__name__)
app.debug = True
app.config['SQLALCHEMY_DATABASE_URI'] = config.db_uri
app.config['TEMPLATE_FOLDER'] = config.dirs['template']
app.config['STATIC_FOLDER'] = config.dirs['static']
app.config['MEDIA_FOLDER'] = config.dirs['media']
app.config['ICONS_FOLDER'] = config.dirs['icons']
app.config['SECRET_KEY'] = config.secret_key
# init dirs
if not os.path.exists(app.config['MEDIA_FOLDER']):
os.makedirs(app.config['MEDIA_FOLDER'])
if not os.path.exists(app.config['ICONS_FOLDER']):
os.makedirs(app.config['ICONS_FOLDER'])
# router utils
from melete.models import *
from melete.login_util import *
def get_user_name():
return session['user_name'] if 'user_name' in session else None
def get_icon(user_id):
login_icon_path = None
if user_id:
login_user = Users.query.filter_by(id=user_id).first()
if login_user and login_user.icon_path:
login_icon_path = login_user.icon_path
return login_icon_path
def get_login_icon():
return get_icon(session['user_id']) if 'user_id' in session else None
# resource
@app.route('/media/<path>')
def media(path):
return send_from_directory(app.config['MEDIA_FOLDER'], path)
@app.route('/icons/<path>')
def icons(path):
return send_from_directory(app.config['ICONS_FOLDER'], path)
# router
@app.route('/')
def index():
if 'user_id' not in session:
return redirect(url_for('login'))
rhythms = [Rhythms.query.get(1)]
stared_rhythms = StaredRhythms.query.filter_by(user_id=session['user_id']).all()
for sr in stared_rhythms:
rhythms += [sr.rhythms]
chords = [Chords.query.get(1)]
stared_chords = StaredChords.query.filter_by(user_id=session['user_id']).all()
for sc in stared_chords:
chords += [sc.chords]
return render_template(
'index.html',
login_icon_path=get_login_icon(),
user_name=get_user_name(),
rhythms=rhythms,
chords=chords
)
@app.route('/watch/<int:id>')
def watch(id):
music = Musics.query.filter_by(id=id).first()
data = json.loads(music.data)
lyrics = map(lambda t: t['lyric'], data)
media_path = music.media_path + '.mp3' if music.media_path else None
icon_path = get_icon(music.user_id)
return render_template(
'watch.html',
login_icon_path=get_login_icon(),
user_name=get_user_name(),
title=music.name,
lyrics=lyrics,
media_path=media_path,
icon_path=icon_path
)
@app.route('/users/<name>')
def users(name):
user = Users.query.filter_by(name=name).first()
musics = Musics.query.order_by(Musics.id.desc()).filter_by(user_id=user.id).limit(20).all()
return render_template(
'user.html',
login_icon_path=get_login_icon(),
user_name=get_user_name(),
user=user,
musics=musics
)
@app.route('/rhythm_creator')
def rhythm_creator():
return render_template('rhythm_creator.html')
@app.route('/rhythms/<int:id>')
def rhythms(id):
rhythm = Rhythms.query.get(id)
if not rhythm:
return ('', 500)
return render_template(
'rhythm.html',
login_icon_path=get_login_icon(),
user_name=get_user_name(),
rhythm=rhythm
)
@app.route('/chords/<int:id>')
def chords(id):
pass
@app.route('/accoms/<int:id>')
def accoms(id):
pass
@app.route('/ranking')
def ranking():
pass
@app.route('/new_entry')
def new_entry():
musics = Musics.query.order_by(Musics.id.desc()).limit(20).all()
return render_template(
'new_entry.html',
login_icon_path=get_login_icon(),
user_name=get_user_name(),
musics=musics
)
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/sign_up', methods=['GET', 'POST'])
def sign_up():
if request.method == 'POST':
if 'twitter_oauth' in session:
res = session['twitter_oauth']
user = Users.query.filter_by(twitter_id=res['user_id']).first()
if user is not None:
return redirect(url_for('index'))
name = request.form['name']
user = Users.query.filter_by(name=name).first()
if user is not None:
return redirect(url_for('sign_up'))
user = Users(name, res['screen_name'])
user.twitter_id = res['user_id']
icon_path = Util.save_twitter_icon(
twitter,
app.config['ICONS_FOLDER'],
res['screen_name'], res['user_id']
)
if icon_path:
user.icon_path = icon_path
db.session.add(user)
db.session.commit()
session['user_id'] = user.id
return redirect(url_for('index'))
else:
return render_template('sign_up.html')
@app.route('/logout')
@login_required
def logout():
session.pop('twitter_oauth', None)
session.pop('user_id', None)
session.pop('user_name', None)
return redirect(url_for('index'))
@app.route('/terms_of_use')
def terms_of_use():
return render_template('terms_of_use.html')
# API
@app.route('/analyze_lyrics', methods=['POST'])
@login_required
def analyze_lyrics():
return jsonify({'tunes': Lyrics.analyze(request.form['text'])})
@app.route('/compose', methods=['POST'])
@login_required
def compose():
try:
user_id = session['user_id']
title = request.form['title']
data = json.loads(request.form['data'])
if not isinstance(data, list):
return 'Error'
with mido.MidiFile(ticks_per_beat=48, charset='utf-8') as midi:
for tune in data:
pass
ts = Rhythm.TimeSignature(int(tune['nn']), int(tune['dd']))
chord_id = tune['chord_id']
chords_db = Chords.query.filter_by(id=chord_id).first()
if not chords_db:
return ('Chords DB Error', 500)
prog = Chord.ChordProg.from_dict(json.loads(chords_db.data))
rhythm_id = tune['rhythm_id']
rhythms_db = Rhythms.query.filter_by(id=rhythm_id).first()
if not rhythms_db:
return ('Rhythms DB Error', 500)
rhythm_tree = Rhythm.RhythmTree.from_dict(json.loads(rhythms_db.data))
note_range = range(int(tune['min_note']), int(tune['max_note']))
skip_prob = float(tune['skip_prob'])
bpm = int(tune['bpm'])
bars = Lyrics.divide(tune['phoneme'], rhythm_tree)
beats = Lyrics.pair(bars, rhythm_tree)
composer = Melody.Composer(ts, beats, prog, note_range, skip_prob, bpm)
midi = Melody.concat_midi(midi, composer.compose())
savepath = Util.random_string(16)
midi.save(app.config['MEDIA_FOLDER'] + '/' + savepath + '.mid')
os.system('timidity %s.mid -Ow -o - | lame - -b 64 %s.mp3' % (app.config['MEDIA_FOLDER'] + '/' + savepath, app.config['MEDIA_FOLDER'] + '/' + savepath))
music = Musics(title, savepath, request.form['data'], user_id)
db.session.add(music)
db.session.commit()
except ValueError as e:
return ('Error: %s' % e, 500)
except KeyError as e:
return ('Error: %s' % e, 500)
return (jsonify({'music_id': music.id}), 200)
@app.route('/rhythm', methods=['POST'])
@login_required
def rhythm():
try:
user_id = session['user_id']
data = json.loads(request.form['data'])
ts = Rhythm.TimeSignature(int(data['nn']), int(data['dd']))
tree = Rhythm.RhythmTree(int(data['division']), int(data['time']), ts, data['patterns'])
rhythm = Rhythms(request.form['title'], json.dumps(tree.to_dict()), user_id)
db.session.add(rhythm)
db.session.commit()
# とりあえず作ったリズムをStarしていく運用
sr = StaredRhythms(rhythm.id, user_id)
db.session.add(sr)
db.session.commit()
except ValueError as e:
return ('Error: %s' % e, 500)
return (jsonify({'rhythm_id': rhythm.id}), 200)
@app.route('/star_rhythm', methods=['POST'])
@login_required
def star_rhythm():
user_id = session['user_id']
rhythm_id = int(request.form['rhythm_id'])
rhythm = Rhythms.query.get(rhythm_id)
if not rhythm:
return ('', 500)
ex_sr = StaredRhythms.query.filter_by(rhythms_id=rhythm_id).filter_by(user_id=user_id).first()
if ex_sr:
db.session.delete(ex_sr)
db.session.commit()
return ('', 200)
sr = StaredRhythms(rhythm_id, user_id)
db.session.add(sr)
db.session.commit()
return ('', 200)
if __name__ == '__main__':
manager.run()