co.mcme.barry.UrlWatcher.java Source code

Java tutorial

Introduction

Here is the source code for co.mcme.barry.UrlWatcher.java

Source

/**
 * This file is part of Barry, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2014 Henry Slawniak <http://mcme.co/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package co.mcme.barry;

import co.mcme.barry.util.Util;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.bukkit.Server;
import org.bukkit.event.Listener;

public class UrlWatcher implements Listener {

    private static Server s;
    private static BarryPlugin b;

    public UrlWatcher(Server server, BarryPlugin inst) {
        s = server;
        b = inst;
    }

    public static JsonObject getRawYoutubeInfo(String daturl) {
        Gson gson = new Gson();
        try {
            JsonObject obj = gson.fromJson(Util.readUrl(daturl), JsonObject.class);
            return obj;
        } catch (Exception ex) {
        }
        return null;
    }

    public static YoutubeVideo getYoutubeInfo(String daturl) {
        JsonObject obj = getRawYoutubeInfo(daturl);
        obj = obj.get("entry").getAsJsonObject();
        String title = obj.get("title").getAsJsonObject().get("$t").getAsString();
        int views = obj.get("yt$statistics").getAsJsonObject().get("viewCount").getAsInt();
        JsonArray authora = obj.get("author").getAsJsonArray();
        JsonObject authorobj = authora.get(0).getAsJsonObject();
        String author = authorobj.get("name").getAsJsonObject().get("$t").getAsString();
        JsonObject mg = obj.get("media$group").getAsJsonObject();
        JsonArray mediacontent = mg.get("media$content").getAsJsonArray();
        JsonObject inner = mediacontent.get(0).getAsJsonObject();
        int duration = inner.get("duration").getAsInt();
        int hours = duration / 3600;
        int minutes = (duration % 3600) / 60;
        int seconds = duration % 60;
        String prettyduration;
        if (hours > 0) {
            prettyduration = hours + "h" + minutes + "m" + seconds + "s";
        } else {
            prettyduration = minutes + "m" + seconds + "s";
        }
        YoutubeVideo video = new YoutubeVideo(title, author, prettyduration, views, duration);
        return video;
    }
}