From 584e964f65e1c46c2bf662573fcd482c94d962be Mon Sep 17 00:00:00 2001 From: macs1207 Date: Sat, 6 Jun 2020 01:59:29 +0800 Subject: [PATCH] Add mp3 generator --- server.py | 11 ++--- templates/index.html | 104 ++++++++++++++++++------------------------- 2 files changed, 49 insertions(+), 66 deletions(-) diff --git a/server.py b/server.py index 0886869..e05e404 100644 --- a/server.py +++ b/server.py @@ -70,10 +70,10 @@ def get_resource(): else: return make_response("need resource id", 400) -@app.route('/api/video', methods=['POST']) +@app.route('/api/media', methods=['POST']) def make_video(): text = request.values.get('text') - file_format = "mp3" + file_format = request.values.get('format') if text is not None: text = text.strip() if len(text) == 0: @@ -85,10 +85,11 @@ def make_video(): "error": "text is too long", }), 400) try: - dir_path, video_id = VideoProcessor().get_media(text, file_format) - print(video_id) + dir_path, media_id = VideoProcessor().get_media(text, file_format) + print(media_id) return jsonify({ - "video_id": video_id + "media_id": media_id, + "path": dir_path }) except VideoNotFoundError as e: logging.error(e) diff --git a/templates/index.html b/templates/index.html index 05ab2e7..07e2204 100644 --- a/templates/index.html +++ b/templates/index.html @@ -69,17 +69,17 @@

可以輸入注音 aria-describedby="inputGroup-sizing-sm" placeholder="上限暫時50字,守護How哥的嘴巴" v-model="text">
- -
影片沒有跳轉的話按這邊下載 - 音檔沒有跳轉的話按這邊下載 + 音檔沒有跳轉的話按這邊下載 @@ -90,25 +90,41 @@

可以輸入注音 data: { text: '', videoWaiting: false, - voiceWaiting: false, + audioWaiting: false, video: null, - voice: null, + audio: null, }, methods: { - getVideo: function () { + getMedia: function (format) { if (this.text) { - $("#getVideo-btn").attr('disabled', true); - this.videoWaiting = true; - this.$http.post("/api/video", { + switch (format) { + case 'mp4': + console.log(format); + $("#getVideo-btn").attr('disabled', true); + this.videoWaiting = true; + break; + case 'mp3': + $("#getAudio-btn").attr('disabled', true); + this.audioWaiting = true; + } + this.$http.post("/api/media", { text: this.text, - }, { emulateJSON: true }).then( + format: format + }, { + emulateJSON: true + }).then( function (response) { // console.log(`${response.status} ${response.bodyText}`); - this.video = `/video?v=${JSON.parse(response.bodyText)["video_id"]}`; - // console.log(this.video); - this.videoWaiting = false; - $("#getVideo-btn").attr('disabled', false); - window.open(this.video, '_blank') + switch (format) { + case 'mp4': + this.video = `/video?v=${JSON.parse(response.bodyText)["media_id"]}`; + window.open(this.video, '_blank') + break; + case 'mp3': + this.audio = `/audio?a=${JSON.parse(response.bodyText)["media_id"]}`; + window.open(this.audio, '_blank') + } + this.releaseBtn(format); }, function (response) { console.log(`${response.status} ${response.bodyText}`); response = JSON.parse(response.bodyText); @@ -130,8 +146,7 @@

可以輸入注音 '', 'error' ) - this.videoWaiting = false; - $("#getVideo-btn").attr('disabled', false); + this.releaseBtn(format); } ); } else { @@ -141,49 +156,16 @@

可以輸入注音 'error' ) } - }, getVoice: function() { - if (this.text) { - $("#getVoice-btn").attr('disabled', true); - this.waiting = true; - this.$http.post("/api/voice", { - text: this.text, - }, { emulateJSON: true }).then( - function (response) { - // console.log(`${response.status} ${response.bodyText}`); - this.voice = `/voice?v=${JSON.parse(response.bodyText)["voice_id"]}`; - // console.log(this.video); - this.waiting = false; - $("#getVoice-btn").attr('disabled', false); - window.open(this.voice, '_blank') - }, function (response) { - console.log(`${response.status} ${response.bodyText}`); - let errormsg = '未定義錯誤'; - switch (response.bodyText) { - case "too long": - errormsg = "你要讓How哥念到嘴酸ㄇ"; - break; - case "empty": - errormsg = "沒有文字不能送ㄛ"; - break; - case "not found": - errormsg = "How哥沒念這個字"; - break; - } - Swal.fire( - errormsg, - '', - 'error' - ) - this.waiting = false; - $("#getVoice-btn").attr('disabled', false); - } - ); - } else { - Swal.fire( - '沒有文字不能送ㄛ', - '', - 'error' - ) + }, + releaseBtn: function (format) { + switch (format) { + case 'mp4': + this.videoWaiting = false; + $("#getVideo-btn").attr('disabled', false); + break; + case 'mp3': + this.audioWaiting = false; + $("#getAudio-btn").attr('disabled', false); } } }