Search in sources :

Example 1 with YoutubeVideo

use of fredboat.util.rest.YoutubeVideo in project FredBoat by Frederikam.

the class AudioLoader method loadSplit.

private void loadSplit(AudioTrack at, IdentifierContext ic) {
    if (!(at instanceof YoutubeAudioTrack)) {
        ic.reply(ic.i18n("loadSplitNotYouTube"));
        return;
    }
    YoutubeAudioTrack yat = (YoutubeAudioTrack) at;
    YoutubeVideo yv = youtubeAPI.getVideoFromID(yat.getIdentifier(), true);
    String desc = yv.getDescription();
    Matcher m = SPLIT_DESCRIPTION_PATTERN.matcher(desc);
    ArrayList<Pair<Long, String>> pairs = new ArrayList<>();
    while (m.find()) {
        long timestamp;
        try {
            timestamp = TextUtils.parseTimeString(m.group(2));
        } catch (NumberFormatException e) {
            continue;
        }
        String title1 = m.group(1);
        String title2 = m.group(3);
        if (title1.length() > title2.length()) {
            pairs.add(new ImmutablePair<>(timestamp, title1));
        } else {
            pairs.add(new ImmutablePair<>(timestamp, title2));
        }
    }
    if (pairs.size() < 2) {
        ic.reply(ic.i18n("loadSplitNotResolves"));
        return;
    }
    ArrayList<SplitAudioTrackContext> list = new ArrayList<>();
    int i = 0;
    for (Pair<Long, String> pair : pairs) {
        long startPos;
        long endPos;
        if (i != pairs.size() - 1) {
            // Not last
            startPos = pair.getLeft();
            endPos = pairs.get(i + 1).getLeft();
        } else {
            // Last
            startPos = pair.getLeft();
            endPos = at.getDuration();
        }
        AudioTrack newAt = at.makeClone();
        newAt.setPosition(startPos);
        SplitAudioTrackContext atc = new SplitAudioTrackContext(jdaEntityProvider, newAt, ic.getMember(), startPos, endPos, pair.getRight());
        list.add(atc);
        gplayer.queue(atc);
        i++;
    }
    MessageBuilder mb = CentralMessaging.getClearThreadLocalMessageBuilder().append(ic.i18n("loadFollowingTracksAdded")).append("\n");
    for (SplitAudioTrackContext atc : list) {
        mb.append("`[").append(TextUtils.formatTime(atc.getEffectiveDuration())).append("]` ").append(TextUtils.escapeAndDefuse(atc.getEffectiveTitle())).append("\n");
    }
    // This is pretty spammy .. let's use a shorter one
    if (mb.length() > 800) {
        mb = CentralMessaging.getClearThreadLocalMessageBuilder().append(ic.i18nFormat("loadPlaylistTooMany", list.size()));
    }
    context.reply(mb.build());
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) YoutubeVideo(fredboat.util.rest.YoutubeVideo) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) YoutubeAudioTrack(com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioTrack) YoutubeAudioTrack(com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioTrack) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Example 2 with YoutubeVideo

use of fredboat.util.rest.YoutubeVideo in project FredBoat by Frederikam.

the class NowplayingCommand method getYoutubeEmbed.

private EmbedBuilder getYoutubeEmbed(AudioTrackContext atc, GuildPlayer player, YoutubeAudioTrack at) {
    YoutubeVideo yv = youtubeAPI.getVideoFromID(at.getIdentifier(), true);
    String timeField = "[" + TextUtils.formatTime(atc.getEffectivePosition(player)) + "/" + TextUtils.formatTime(atc.getEffectiveDuration()) + "]";
    String desc = yv.getDescription();
    // Shorten it to about 400 chars if it's too long
    if (desc.length() > 450) {
        desc = TextUtils.substringPreserveWords(desc, 400) + " [...]";
    }
    EmbedBuilder eb = CentralMessaging.getClearThreadLocalEmbedBuilder().setTitle(atc.getEffectiveTitle(), "https://www.youtube.com/watch?v=" + at.getIdentifier()).addField("Time", timeField, true);
    if (!desc.equals("")) {
        eb.addField(atc.i18n("npDescription"), desc, false);
    }
    eb.setColor(new Color(205, 32, 31)).setThumbnail("https://i.ytimg.com/vi/" + at.getIdentifier() + "/hqdefault.jpg").setAuthor(yv.getChannelTitle(), yv.getChannelUrl(), yv.getChannelThumbUrl());
    return eb;
}
Also used : EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) YoutubeVideo(fredboat.util.rest.YoutubeVideo)

Aggregations

YoutubeVideo (fredboat.util.rest.YoutubeVideo)2 YoutubeAudioTrack (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioTrack)1 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)1 MessageBuilder (net.dv8tion.jda.core.MessageBuilder)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1