The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

YouTube.cs 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Text;
  5. using Newtonsoft.Json;
  6. using Newtonsoft.Json.Linq;
  7. namespace Combot.Modules.ModuleClasses
  8. {
  9. public class YouTube : Module
  10. {
  11. public override void Initialize()
  12. {
  13. Bot.CommandReceivedEvent += HandleCommandEvent;
  14. }
  15. public override void ParseCommand(CommandMessage command)
  16. {
  17. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  18. switch (foundCommand.Name)
  19. {
  20. case "YouTube Search":
  21. YoutubeSearch(command);
  22. break;
  23. }
  24. }
  25. private void YoutubeSearch(CommandMessage command)
  26. {
  27. string urlTemplate = "http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=1&q={0}";
  28. Uri searchUrl = new Uri(string.Format(urlTemplate, command.Arguments["Query"]));
  29. WebClient web = new WebClient();
  30. web.Encoding = Encoding.UTF8;
  31. string page = web.DownloadString(searchUrl);
  32. JObject parsed = (JObject)JsonConvert.DeserializeObject(page);
  33. if (parsed["data"]["totalItems"].Value<int>() > 0)
  34. {
  35. string videoID = parsed["data"]["items"].First().Value<string>("id");
  36. string vidDescription = GetYoutubeDescription(videoID);
  37. string youtubeMessage = string.Format("{0} - {1}.", vidDescription, string.Format("http://youtu.be/{0}", videoID));
  38. switch (command.MessageType)
  39. {
  40. case MessageType.Channel:
  41. Bot.IRC.SendPrivateMessage(command.Location, youtubeMessage);
  42. break;
  43. case MessageType.Query:
  44. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, youtubeMessage);
  45. break;
  46. case MessageType.Notice:
  47. Bot.IRC.SendNotice(command.Nick.Nickname, youtubeMessage);
  48. break;
  49. }
  50. }
  51. else
  52. {
  53. string noResults = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  54. switch (command.MessageType)
  55. {
  56. case MessageType.Channel:
  57. Bot.IRC.SendPrivateMessage(command.Location, noResults);
  58. break;
  59. case MessageType.Query:
  60. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noResults);
  61. break;
  62. case MessageType.Notice:
  63. Bot.IRC.SendNotice(command.Nick.Nickname, noResults);
  64. break;
  65. }
  66. }
  67. }
  68. private string GetYoutubeDescription(string ID)
  69. {
  70. string description = string.Empty;
  71. string urlTemplate = "http://gdata.youtube.com/feeds/api/videos/{0}?v=2&alt=jsonc";
  72. Uri searchUrl = new Uri(string.Format(urlTemplate, ID));
  73. WebClient web = new WebClient();
  74. web.Encoding = Encoding.UTF8;
  75. string page = web.DownloadString(searchUrl);
  76. JObject parsed = (JObject)JsonConvert.DeserializeObject(page);
  77. var data = parsed["data"];
  78. description = string.Format("\u0002{0}\u000F", data["title"]);
  79. if (data["duration"] == null)
  80. {
  81. return description;
  82. }
  83. TimeSpan duration = TimeSpan.FromSeconds(data["duration"].Value<double>());
  84. description += string.Format(" | Length: \u0002{0}\u000F", duration.ToString("g"));
  85. if (data["ratingCount"] != null)
  86. {
  87. int likes = data["likeCount"].Value<int>();
  88. string pluralLikes = (likes > 1) ? "s" : string.Empty;
  89. int dislikes = data["ratingCount"].Value<int>() - likes;
  90. string pluralDislikes = (dislikes > 1) ? "s" : string.Empty;
  91. double percent = 100.0 * ((double)likes / data["ratingCount"].Value<int>());
  92. description += string.Format(" | Rating: {0} Like{1}, {2} Dislike{3} (\u0002{4}\u000F%)", likes, pluralLikes, dislikes, pluralDislikes, Math.Round(percent, 1));
  93. }
  94. if (data["viewCount"] != null)
  95. {
  96. description += string.Format(" | Views: \u0002{0}\u000F", data["viewCount"].Value<int>());
  97. }
  98. DateTime uploadDate = Convert.ToDateTime(data["uploaded"].Value<string>());
  99. description += string.Format(" | Uploaded By: \u0002{0}\u000F on \u0002{1}\u000F", data["uploader"].Value<string>(), uploadDate.ToString("R"));
  100. if (data["contentRating"] != null)
  101. {
  102. description += " | \u0002NSFW\u000F";
  103. }
  104. return description;
  105. }
  106. }
  107. }