diff --git a/src/main/java/net/Broken/Tools/EmbedMessageUtils.java b/src/main/java/net/Broken/Tools/EmbedMessageUtils.java index 5890837..9139eee 100644 --- a/src/main/java/net/Broken/Tools/EmbedMessageUtils.java +++ b/src/main/java/net/Broken/Tools/EmbedMessageUtils.java @@ -3,6 +3,7 @@ package net.Broken.Tools; import net.Broken.DB.Entity.GuildPreferenceEntity; import net.Broken.MainBot; import net.Broken.audio.Youtube.SearchResult; +import net.Broken.audio.Youtube.YoutubeTools; import net.dv8tion.jda.core.EmbedBuilder; import net.dv8tion.jda.core.entities.Member; import net.dv8tion.jda.core.entities.MessageEmbed; @@ -129,6 +130,7 @@ public class EmbedMessageUtils { .setColor(Color.CYAN) .setTitle(result.title) .setImage(result.imageUrl) + .addField("Duration: ", YoutubeTools.getInstance().ytTimeToString(result.duration), false) .addField("URL:","https://www.youtube.com/watch?v="+result.id,false) .addField("Chanel:", result.channelTittle, false); return buildStandar(builder); diff --git a/src/main/java/net/Broken/audio/Youtube/SearchResult.java b/src/main/java/net/Broken/audio/Youtube/SearchResult.java index c62b241..1a36538 100644 --- a/src/main/java/net/Broken/audio/Youtube/SearchResult.java +++ b/src/main/java/net/Broken/audio/Youtube/SearchResult.java @@ -8,8 +8,9 @@ public class SearchResult { public String channelId; public String channelTittle; public String imageUrl; + public String duration; - public SearchResult(com.google.api.services.youtube.model.SearchResult result){ + public SearchResult(com.google.api.services.youtube.model.SearchResult result, String duration){ id = result.getId().getVideoId(); title = result.getSnippet().getTitle(); description = result.getSnippet().getDescription(); @@ -17,5 +18,6 @@ public class SearchResult { channelId = result.getSnippet().getChannelId(); channelTittle = result.getSnippet().getChannelTitle(); imageUrl = result.getSnippet().getThumbnails().getDefault().getUrl(); + this.duration = duration; } } diff --git a/src/main/java/net/Broken/audio/Youtube/YoutubeTools.java b/src/main/java/net/Broken/audio/Youtube/YoutubeTools.java index 64d8254..12c3e78 100644 --- a/src/main/java/net/Broken/audio/Youtube/YoutubeTools.java +++ b/src/main/java/net/Broken/audio/Youtube/YoutubeTools.java @@ -5,6 +5,8 @@ import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.youtube.YouTube; import com.google.api.services.youtube.model.SearchListResponse; import com.google.api.services.youtube.model.SearchResult; +import com.google.api.services.youtube.model.Video; +import com.google.api.services.youtube.model.VideoListResponse; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -12,6 +14,8 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; +import static org.hibernate.engine.jdbc.Size.LobMultiplier.M; + public class YoutubeTools { private Logger logger = LogManager.getLogger(); @@ -88,13 +92,80 @@ public class YoutubeTools { searchList.setOrder("relevance"); SearchListResponse response = searchList.execute(); + + StringBuilder idString = new StringBuilder(); + for(SearchResult item : response.getItems()){ + idString.append(item.getId().getVideoId()).append(","); + } + + + HashMap videoHashMap = new HashMap<>(); + YouTube.Videos.List video = youTube.videos().list("contentDetails"); + video.setId(idString.toString()); + video.setKey(apiKey); + VideoListResponse videoResponse = video.execute(); + for(Video item : videoResponse.getItems()){ + videoHashMap.put(item.getId(), item); + } ArrayList finalResult = new ArrayList<>(); for(SearchResult item : response.getItems()){ logger.debug(item.getSnippet().getTitle()); - finalResult.add(new net.Broken.audio.Youtube.SearchResult(item)); + finalResult.add(new net.Broken.audio.Youtube.SearchResult(item, videoHashMap.get(item.getId().getVideoId()).getContentDetails().getDuration())); } + + + + return finalResult; } + + + public String ytTimeToString(String time){ + int hours; + int minutes; + int seconds; + if(time.equals("PT0S")) + return ":red_circle: LIVE"; + + time = time.replace("PT",""); + if(time.contains("H")) { + + String matched = time.substring(0, time.indexOf("H")+1); + time = time.replace(matched,""); + hours = Integer.parseInt(matched.replace("H", "")); + } + else + hours = 0; + logger.debug(time); + + if(time.contains("M")) { + + String matched = time.substring(0, time.indexOf("M")+1); + time = time.replace(matched,""); + minutes = Integer.parseInt(matched.replace("M", "")); + } + else + minutes = 0; + logger.debug(time); + if(time.contains("S")) { + + String matched = time.substring(0, time.indexOf("S")+1); + time = time.replace(matched,""); + seconds = Integer.parseInt(matched.replace("S", "")); + } + else + seconds = 0; + logger.debug(time); + + String hoursStr = (hours < 10) ? "0" + hours : String.valueOf(hours); + String minutesStr = (minutes < 10) ? "0" + minutes : String.valueOf(minutes); + String secondsStr = (seconds < 10) ? "0" + seconds : String.valueOf(seconds); + if (hours > 0) + return hoursStr + ":" + minutesStr + ":" + secondsStr; + else + return minutesStr + ":" + secondsStr; + + } } diff --git a/src/main/resources/static/css/style.css b/src/main/resources/static/css/style.css index f6d371e..c421fc8 100644 --- a/src/main/resources/static/css/style.css +++ b/src/main/resources/static/css/style.css @@ -7,4 +7,5 @@ .collapsible-body{ padding: 0px; -} \ No newline at end of file +} + diff --git a/src/main/resources/static/js/music.js b/src/main/resources/static/js/music.js index 031abc3..452b343 100644 --- a/src/main/resources/static/js/music.js +++ b/src/main/resources/static/js/music.js @@ -15,7 +15,7 @@ var loadingFlag = false; var guild; $(document).ready(function () { - if (Cookies.get('guild') != undefined) { + if (Cookies.get('guild') !== undefined) { guild = Cookies.get('guild'); btn_play = $('#btn_play'); @@ -28,7 +28,8 @@ $(document).ready(function () { switchAutoFlow = $("#autoflow"); setInterval("getCurentMusic()", 1000); - $('#modalAdd').modal(); + + M.Modal.init($('#modalAdd').get(0)); $('#modal_current_info').modal(); @@ -75,7 +76,7 @@ function getCurentMusic() { $('#btn_info').addClass("determinate").removeClass("indeterminate"); } $('#music_progress').width("0%"); - if (Cookies.get('token') != undefined) { + if (Cookies.get('token') !== undefined) { disableBtn(btn_stop); disableBtn(btn_info); enableBtn(btn_add); @@ -298,7 +299,7 @@ function updateControl(data) { } $('#music_progress').width(percent + "%"); - if (Cookies.get('token') != undefined) { + if (Cookies.get('token') !== undefined) { enableBtn(btn_play); enableBtn(btn_stop); enableBtn(btn_info); @@ -327,7 +328,7 @@ function updateControl(data) { function sendCommand(command) { modal_loading.modal('open'); - console.log(command); + // console.log(command); $.ajax({ type: "POST", dataType: 'json', @@ -343,13 +344,19 @@ function sendCommand(command) { }).fail(function (data) { console.log(data); - alert(data.responseJSON.Message); modal_loading.modal('close'); if (data.responseJSON.error === "token") { Cookies.remove('token'); Cookies.remove('name'); location.reload(); } + else{ + M.toast({ + html: " warning Command fail!", + classes: 'red', + displayLength: 99999999 + }); + } }); } @@ -358,18 +365,116 @@ function comparePlaylist(list1, list2) { return false; } - if (list1.length != list2.length) { + if (list1.length !== list2.length) { return false; } for (var i = 0; i++; i < list1.length) { - if (list1[i].uri != list2[i].uri) + if (list1[i].uri !== list2[i].uri) return false } return true; } + + +function search() { + let query = $('#input_search').val(); + let list = $("#search_result"); + let load = $("#search_load"); + disableBtn($('#btn_search')); + // list.addClass("hide"); + list.removeClass("scale-in"); + load.removeClass("hide"); + load.addClass("scale-in"); + + $.get("/api/music/search?query=" + query, (data) => { + // console.log(data); + list.empty(); + data.forEach((item)=>{ + + let html = + "
  • " + + " \"\"" + + " "+item["title"]+"" + + "

    "+item["channelTittle"]+ " ║ "+ item["publishedAt"].substr(0, item["publishedAt"].indexOf('T'))+"
    " + ytTimeToTime(item["duration"]) + + "

    " + + " add_circle_outline" + + " " + + "
  • "; + + + + list.append(html) + }); + + $(".add-btn-list").click(addListClick); + // list.removeClass("hide"); + + load.removeClass("scale-in"); + load.addClass("hide"); + list.addClass("scale-in"); + enableBtn($('#btn_search')); + + + }); + + +} + + +function addListClick(event){ + let button; + if(event.target.nodeName === "I"){ + button = event.target.parentNode; + } + else + button = event.target; + button.classList.add("scale-out"); + + let command = { + command: "ADD", + url: button.id, + playlistLimit: $('#limit_range').val(), + onHead: !$('#bottom').is(':checked') + }; + sendCommand(command); +} + +function ytTimeToTime(duration) { + let hours; + let minutes; + let seconds; + if(duration === "PT0S") + return "🔴 LIVE"; + if(duration.includes("H")) + hours = parseInt(duration.match(/\d*H/)[0].replace("H",""), 10); + else + hours = 0; + + if(duration.includes("M")) + minutes = parseInt(duration.match(/\d*M/)[0].replace("M",""), 10); + else + minutes = 0; + + if(duration.includes("S")) + seconds = parseInt(duration.match(/\d*S/)[0].replace("S",""), 10); + else + seconds = 0; + + hours = (hours < 10) ? "0" + hours : hours; + minutes = (minutes < 10) ? "0" + minutes : minutes; + seconds = (seconds < 10) ? "0" + seconds : seconds; + if (hours > 0) + return hours + ":" + minutes + ":" + seconds; + else + return minutes + ":" + seconds; +} + + + + function msToTime(duration) { var milliseconds = parseInt((duration % 1000) / 100) , seconds = parseInt((duration / 1000) % 60) @@ -403,6 +508,8 @@ function listeners() { }); + $('#btn_search').click(search); + $('#btn_next').click(function () { sendCommand({command: "NEXT"}); }); @@ -419,6 +526,26 @@ function listeners() { } }); + $('#input_search').on("input", function () { + if ($('#input_search').val() == "") { + disableBtn($('#btn_search')); + } + else { + enableBtn($('#btn_search')); + } + }); + + $('#add_btn').click(function () { + if ($('#input_search').val() == "") { + disableBtn($('#btn_search')); + } + else { + enableBtn($('#btn_search')); + } + }); + + + $('#modalChanels').change(function () { if ($('#btn_ok_channel').hasClass("disabled")) { $('#btn_ok_channel').removeClass("disabled"); @@ -432,18 +559,6 @@ function listeners() { sendCommand(command); }); - $('#btn_add').click(function () { - console.log(!$('#bottom').is(':checked')); - var command = { - command: "ADD", - url: $('#input_link').val(), - playlistLimit: $('#limit_range').val(), - onHead: !$('#bottom').is(':checked') - }; - $('#input_link').val(''); - sendCommand(command); - }); - $('#btn_ok_channel').click(function () { var command = { @@ -458,7 +573,7 @@ function listeners() { }); switchAutoFlow.click(function () { - console.log(switchAutoFlow.is(':checked')) + // console.log(switchAutoFlow.is(':checked')); if (switchAutoFlow.is(':checked')) { sendCommand({command: 'AUTOFLOWON'}) } diff --git a/src/main/resources/templates/music.html b/src/main/resources/templates/music.html index 360e1b0..b645aca 100644 --- a/src/main/resources/templates/music.html +++ b/src/main/resources/templates/music.html @@ -10,10 +10,42 @@ - - + + + @@ -89,60 +121,7 @@ add_circle_outline - -
    @@ -198,6 +177,74 @@
    + + + + + +
    - +