[Class] Youtube Channel Information

Discussion in 'Resources' started by Retherz_, Mar 29, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Retherz_

    Code:java
    1. import java.io.BufferedReader;
    2. import java.io.IOException;
    3. import java.io.InputStreamReader;
    4. import java.net.MalformedURLException;
    5. import java.net.URL;
    6.  
    7. import org.json.simple.JSONObject;
    8. import org.json.simple.parser.JSONParser;
    9. import org.json.simple.parser.ParseException;
    10.  
    11. public class YoutubeChannel {
    12.  
    13. private JSONObject json;
    14. private String channelName;
    15. private URL channelURL;
    16. private String channelInformation;
    17.  
    18. public YoutubeChannel(String name) {
    19. channelName = name;
    20. try {
    21. channelURL = new URL("[URL]http://gdata.youtube.com/feeds/api/users/[/URL]" + name +"?v=2&alt=json");
    22. } catch (MalformedURLException e) {
    23. }
    24. update();
    25. }
    26.  
    27. public boolean update() {
    28. try {
    29. BufferedReader br = new BufferedReader(new InputStreamReader(channelURL.openStream()));
    30. channelInformation = br.readLine();
    31. br.close();
    32. try {
    33. json = (JSONObject) new JSONParser().parse(channelInformation);
    34. json = (JSONObject) json.get("entry");
    35. JSONObject json2 = (JSONObject) json.get("yt$firstName");
    36. channelName = (String) json2.get("$t");
    37. return true;
    38. } catch (ParseException e) {
    39. }
    40. } catch (IOException e) {
    41. }
    42. return false;
    43. }
    44.  
    45. public String getName() {
    46. return channelName;
    47. }
    48.  
    49. public String getDisplayName() {
    50. JSONObject json2 = (JSONObject) json.get("yt$username");
    51. return (String) json2.get("display");
    52. }
    53.  
    54. public String getLocation() {
    55. JSONObject json2 = (JSONObject) json.get("yt$location");
    56. return (String) json2.get("$t");
    57. }
    58.  
    59. public String getSummary() {
    60. JSONObject json2 = (JSONObject) json.get("summary");
    61. return (String) json2.get("$t");
    62. }
    63.  
    64. public String getGooglePlusId() {
    65. JSONObject json2 = (JSONObject) json.get("yt$googlePlusUserId");
    66. return (String) json2.get("$t");
    67. }
    68.  
    69. public int getSubscriberCount() {
    70. JSONObject json2 = (JSONObject) json.get("yt$statistics");
    71. String s = (String) json2.get("subscriberCount");
    72. int number = Integer.parseInt(s);
    73. return number;
    74. }
    75.  
    76. public int getTotalViews() {
    77. JSONObject json2 = (JSONObject) json.get("yt$statistics");
    78. String s = (String) json2.get("totalUploadViews");
    79. int number = Integer.parseInt(s);
    80. return number;
    81. }
    82.  
    83. }
    84.  

    Hope you find this useful.
    If you make something out of this please comment it, I would love to see what you could make with it.

    I made one for twitch too. :)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
    uTerrian, LCastr0 and ArthurMaker like this.
  2. Offline

    SoThatsIt

    Retherz_ this is cool! could be used to display the top 5 youtubers who play on your server.
     
    Plo124 likes this.
  3. Offline

    MiniDigger

    Retherz_ this don't work for me. Getting NPE:
    Caused by: java.lang.NullPointerException
    at eu.zonegames.api.YoutubeChannel.update(YoutubeChannel.java:38) ~[?:?]
    at eu.zonegames.api.YoutubeChannel.<init>(YoutubeChannel.java:26) ~[?:?]
    EDIT: Row 38 is this: channelName = (String) json2.get("$t");
     
  4. Offline

    LCastr0

    I'll try to make a plugin soon, let's see :p
     
  5. Offline

    Ba7marker

    That's an amazing Idea you could even make it so a certain amount of subscribers get a higher rank.
     
  6. Offline

    Retherz_

    I made that, gonna put it on bukkit.dev
     
  7. Offline

    Ba7marker

    Are you upload this as if not I might have a ago.
     
  8. Offline

    Retherz_

    Uploading it today :)
     
  9. Offline

    Retherz_

  10. Offline

    connorlinfoot

  11. Offline

    Retherz_

    It hasn't been approved yet.
     
    connorlinfoot likes this.
  12. Offline

    connorlinfoot


    How does your plugin work in terms of verifying the YouTube account? I just finished making a plugin that does it and was wondering how yours does this?
     
  13. Offline

    Retherz_

    Like this:
    Code:java
    1.  
    2.  
    3. try {
    4. YoutubeChannel channel = new YoutubeChannel(args[1]);
    5. if(channel.getDisplayName().contains("[" + sender.getName() + ":" + getConfig().getString("ServerName") + "]")) {
    6. if(channel.getSubscriberCount() >= getConfig().getInt("MinimumSubscribers")) {
    7.  
    8. for(String s : getConfig().getStringList("Commands")) {
    9. String command = s.replaceAll("{playername}", sender.getName());
    10. command = command.replaceAll("{channelurl}", "[url]http://youtube.com/[/url]" + args[1]);
    11. getServer().dispatchCommand(getServer().getConsoleSender(), command);
    12. }
    13. getConfig().getStringList("YouTubers").add(uuid);
    14. getConfig().getStringList("YoutubeChannels").add(args[1].toLowerCase());
    15. sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GRAY + "You" + ChatColor.WHITE + "Tube" + ChatColor.GREEN + "Reward"+ ChatColor.GOLD + "] " + ChatColor.AQUA
    16. + "You have sucessfully linked your channel with your account!");
    17. return true;
    18. } else {
    19. //Not enough subscribers
    20. sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GRAY + "You" + ChatColor.WHITE + "Tube" + ChatColor.GREEN + "Reward"+ ChatColor.GOLD + "] " + ChatColor.AQUA
    21. + "You have too few subscribers! You need " + ChatColor.GOLD + (getConfig().getInt("MinimumSubscribers") - channel.getSubscriberCount()) + ChatColor.AQUA + " more subscribers.");
    22. return true;
    23. }
    24. } else
    25. sender.sendMessage(ChatColor.AQUA
    26. + "- Change your account name to: " + ChatColor.GRAY + "[" + sender.getName() + ":" + getConfig().getString("ServerName") + "]");
    27. } catch(Exception e) {
    28. sender.sendMessage(ChatColor.GOLD + "[" + ChatColor.DARK_GRAY + "You" + ChatColor.WHITE + "Tube" + ChatColor.GREEN + "Reward"+ ChatColor.GOLD + "] " + ChatColor.AQUA
    29. + "An error has occured or invalid information was entered.");
    30. return true;
    31. }
    32.  
     
  14. Offline

    Retherz_

  15. Offline

    connorlinfoot


    I'm still waiting for mine to get approved :3 Also does yours actually check if they own the account in any way?
     
Thread Status:
Not open for further replies.

Share This Page