crafterjuli

YoutubeStats

Jun 6th, 2015
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. package juliandrees;
  2.  
  3.  
  4. import java.net.URL;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.command.Command;
  8. import org.bukkit.command.CommandSender;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.plugin.java.JavaPlugin;
  11. import org.xml.sax.InputSource;
  12. import org.xml.sax.XMLReader;
  13. import org.xml.sax.helpers.XMLReaderFactory;
  14.  
  15. public class YoutubeStats extends JavaPlugin {
  16.  
  17. public static int subCount = 0;
  18. public static int viewCount = 0;
  19.  
  20. @Override
  21. public void onEnable() {
  22. Bukkit.getConsoleSender().sendMessage("§6[§4You§rTubeStats§6] §5enabled!");
  23. this.getCommand("youtube").setExecutor(this);
  24. }
  25.  
  26. @Override
  27. public void onDisable() {
  28. Bukkit.getConsoleSender().sendMessage("§6[§4You§rTubeStats§6] §5disabled!!");
  29. }
  30.  
  31. @Override
  32. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
  33.  
  34. if (sender instanceof Player) {
  35. Player p = (Player) sender;
  36.  
  37. if (args.length != 1) {
  38. p.sendMessage("§6[§4You§rTubeStats§6] §cUse /yt <channel>");
  39. }
  40.  
  41. String channel = args[0];
  42.  
  43. p.sendMessage("§8[] === [§4You§rTubeStats§8] §8=== [] ");
  44. p.sendMessage(" ");
  45. p.sendMessage(" §eChannel: §7" + channel);
  46. p.sendMessage(" §eSubscribers: §7" + getSubscribers(channel));
  47. p.sendMessage(" §eViews: §7" + getTotalViews(channel));
  48. p.sendMessage(" ");
  49. p.sendMessage("§8[] === [§4You§rTubeStats§8] §8=== [] ");
  50.  
  51. }
  52.  
  53. return true;
  54. }
  55.  
  56. public int getSubscribers(String channel) {
  57.  
  58. try {
  59.  
  60. XMLReader xml = XMLReaderFactory.createXMLReader();
  61. InputSource source = new InputSource(new URL("http://gdata.youtube.com/feeds/api/users/" + channel).openStream());
  62. xml.setContentHandler(new CustomContentHandler());
  63. xml.parse(source);
  64.  
  65. } catch(Exception ex) {
  66. subCount = 0;
  67. }
  68.  
  69. return subCount;
  70.  
  71. }
  72.  
  73. public int getTotalViews(String channel) {
  74.  
  75. try {
  76. XMLReader xml = XMLReaderFactory.createXMLReader();
  77. InputSource source = new InputSource(new URL("http://gdata.youtube.com/feeds/api/users/" + channel).openStream());
  78. xml.setContentHandler(new CustomContentHandler());
  79. xml.parse(source);
  80. } catch(Exception ex) {
  81. viewCount = 0;
  82. }
  83.  
  84. return viewCount;
  85.  
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment