def music_listen(id): music = Music.query.get_or_404(id) music.audio_url = musicAudiosSet.url(filename=music.audiofile) music.cover_url = musicCoversSet.url(filename=music.coverfile) music.lrc_url = musicLrcsSet.url(filename=music.lrcfile) lrc_path = musicLrcsSet.path(filename=music.lrcfile) lrcf = open(file=lrc_path, mode='r', encoding='utf8') music.lrc = lrcf.readlines() return render_template("admin/music_listen.html", music=music)
def music_change(id): form = MusicForm() set_music_choices(form) form.music_cover.validators = [form.music_cover.validators[1]] form.music_audio.validators = [form.music_audio.validators[1]] form.music_lrc.validators = [form.music_lrc.validators[1]] music = Music.query.get_or_404(id) if request.method == "GET": form.music_title.data = music.title form.music_singer.data = music.singer form.music_singer.data = music.singer form.music_privacy.data = music.privacy form.music_recmmed.data = music.recommed form.music_lang.data = music.lang_id form.music_style.data = music.style_id form.music_theme.data = music.theme_id form.music_emotion.data = music.emotion_id form.music_scene.data = music.scene_id if form.validate_on_submit(): set_music_attribs(form, music, False) subfolder = 'musics' + '/' + music.uuid cover_fs = form.music_cover.data if cover_fs: cover_path = musicCoversSet.path(filename=music.coverfile) if os.path.isfile(cover_path): os.remove(cover_path) music.coverfile = musicCoversSet.save(cover_fs, folder=subfolder, name=cover_fs.filename) audio_fs = form.music_audio.data if audio_fs: audio_path = musicAudiosSet.path(filename=music.audiofile) if os.path.isfile(audio_path): os.remove(audio_path) music.audiofile = musicAudiosSet.save(audio_fs, folder=subfolder, name=audio_fs.filename) lrc_fs = form.music_lrc.data if lrc_fs: lrc_path = musicLrcsSet.path(filename=music.lrcfile) if os.path.isfile(lrc_path): os.remove(lrc_path) music.lrcfile = musicLrcsSet.save(lrc_fs, folder=subfolder, name=lrc_fs.filename) db.session.add(music) db.session.commit() flash(message='音乐修改成功!', category='ok') return redirect(url_for('admin.music_list', page=1)) return render_template('admin/music_change.html', music=music, form=form)
def music_listen(id): # 获取当前音乐对象 music = Music.query.get_or_404(id) # 更新播放量 music.clicknum += 1 db.session.add(music) db.session.commit() # 获取当前音乐文件的url music.audio_url = musicAudiosSet.url(music.audiofile) music.cover_url = musicAudiosSet.url(music.coverfile) # 读取歌词文件 lrc_path = musicLrcsSet.path(filename=music.lrcfile) lrcf = open(file=lrc_path, mode='r', encoding='utf8') music.lrc = [] for lrcline in lrcf.readlines(): lrcline = lrcline.strip('\n') # print(lrcline) idx = lrcline.find(']') + 1 # 分割时间戳和歌词内容 timestamp = lrcline[0:idx] lrccontent = lrcline[idx:] tidx = timestamp.find(':') # 分割时间戳内的分钟数和秒数 midum = timestamp[tidx - 2:tidx] second = timestamp[tidx + 1:tidx + 3] if midum == 'ti' or midum == 'ar' or midum == 'al' or midum == 'by': continue else: timestamp = int(midum) * 60 + int(second) # 把时间戳转换成秒数 # 把处理好的一行歌词添加到列表 music.lrc.append((timestamp, lrccontent)) # 获取推荐音乐和url recmm_lang_musics = Music.query.filter(Music.lang_id == music.lang_id, Music.id != music.id).all()[0:6] for recmm_lang in recmm_lang_musics: recmm_lang.cover_url = musicCoversSet.url(filename=recmm_lang.coverfile) # 按音乐流派推荐 recmm_style_musics = Music.query.filter(Music.style_id == music.style_id, Music.id != music.id).all()[0:6] for recmm_style in recmm_style_musics: recmm_style.cover_url = musicCoversSet.url(filename=recmm_style.coverfile) # 按音乐主题推荐 recmm_theme_musics = Music.query.filter(Music.theme_id == music.theme_id, Music.id != music.id).all()[0:6] for recmm_theme in recmm_theme_musics: recmm_theme.cover_url = musicCoversSet.url(filename=recmm_theme.coverfile) # 按音乐心情推荐 recmm_emotion_musics = Music.query.filter(Music.emotion_id == music.emotion_id, Music.id != music.id).all()[0:6] for recmm_emotion in recmm_emotion_musics: recmm_emotion.cover_url = musicCoversSet.url(filename=recmm_emotion.coverfile) # 按音乐场景推荐 recmm_scene_musics = Music.query.filter(Music.scene_id == music.scene_id, Music.id != music.id).all()[0:6] for recmm_scene in recmm_scene_musics: recmm_scene.cover_url = musicCoversSet.url(filename=recmm_scene.coverfile) # 按音乐歌手推荐 recmm_singer_musics = Music.query.filter(Music.singer == music.singer, Music.id != music.id).all()[0:6] for recmm_singer in recmm_singer_musics: recmm_singer.cover_url = musicCoversSet.url(filename=recmm_singer.coverfile) # 获取评论列表,以及用户头像 music_comments = MusicComment.query.filter_by(music_id=music.id). \ order_by(MusicComment.addtime.desc()).all() for cmmt in music_comments: cmmt.userface_url = photosSet.url(filename=cmmt.user.name + '/' + cmmt.user.face) return render_template('home/music_listen.html', form=MusicCommentForm(), music=music, recmm_lang_musics=recmm_lang_musics, recmm_style_musics=recmm_style_musics, recmm_theme_musics=recmm_theme_musics, recmm_emotion_musics=recmm_emotion_musics, recmm_scene_musics=recmm_scene_musics, recmm_singer_musics=recmm_singer_musics, music_comments=music_comments)