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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using Combot.IRCServices.Messaging;
  7. using Newtonsoft.Json;
  8. using Newtonsoft.Json.Linq;
  9. namespace Combot.Modules.Plugins
  10. {
  11. public class YouTube : Module
  12. {
  13. public override void Initialize()
  14. {
  15. Bot.CommandReceivedEvent += HandleCommandEvent;
  16. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
  17. }
  18. public override void ParseCommand(CommandMessage command)
  19. {
  20. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  21. switch (foundCommand.Name)
  22. {
  23. case "YouTube Search":
  24. YoutubeSearch(command);
  25. break;
  26. }
  27. }
  28. private void HandleChannelMessage(object sender, ChannelMessage message)
  29. {
  30. if (Enabled
  31. && !Bot.ServerConfig.ChannelBlacklist.Contains(message.Channel)
  32. && !Bot.ServerConfig.NickBlacklist.Contains(message.Sender.Nickname)
  33. && !ChannelBlacklist.Contains(message.Channel)
  34. && !NickBlacklist.Contains(message.Sender.Nickname)
  35. && !Bot.IsCommand(message.Message))
  36. {
  37. Regex urlRegex = new Regex("(((youtube.*(v=|/v/))|(youtu\\.be/))(?<ID>[-_a-zA-Z0-9]+))");
  38. if (urlRegex.IsMatch(message.Message))
  39. {
  40. Match urlMatch = urlRegex.Match(message.Message);
  41. string youtubeMessage = GetYoutubeDescription(urlMatch.Groups["ID"].Value);
  42. Bot.IRC.Command.SendPrivateMessage(message.Channel, youtubeMessage);
  43. }
  44. }
  45. }
  46. private void YoutubeSearch(CommandMessage command)
  47. {
  48. string urlTemplate = "http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=1&q={0}";
  49. Uri searchUrl = new Uri(string.Format(urlTemplate, command.Arguments["Query"]));
  50. WebClient web = new WebClient();
  51. web.Encoding = Encoding.UTF8;
  52. try
  53. {
  54. string page = web.DownloadString(searchUrl);
  55. JObject parsed = (JObject) JsonConvert.DeserializeObject(page);
  56. if (parsed["data"]["totalItems"].Value<int>() > 0)
  57. {
  58. string videoID = parsed["data"]["items"].First().Value<string>("id");
  59. string vidDescription = GetYoutubeDescription(videoID);
  60. string youtubeMessage = string.Format("{0} - {1}", vidDescription, string.Format("http://youtu.be/{0}", videoID));
  61. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, youtubeMessage);
  62. }
  63. else
  64. {
  65. string noResults = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  66. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noResults);
  67. }
  68. }
  69. catch (WebException ex)
  70. {
  71. if (ex.Response != null)
  72. {
  73. int code = (int)((HttpWebResponse)ex.Response).StatusCode;
  74. string errorCode = string.Format("Unable to search for \u0002{0}\u000F. Youtube returned status code \u0002{1}\u000F.", command.Arguments["Query"], code);
  75. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorCode);
  76. }
  77. else
  78. {
  79. string errorCode = string.Format("Unable to search for \u0002{0}\u000F.", command.Arguments["Query"]);
  80. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorCode);
  81. }
  82. }
  83. }
  84. private string GetYoutubeDescription(string ID)
  85. {
  86. string description = string.Empty;
  87. string urlTemplate = "http://gdata.youtube.com/feeds/api/videos/{0}?v=2&alt=jsonc";
  88. Uri searchUrl = new Uri(string.Format(urlTemplate, ID));
  89. WebClient web = new WebClient();
  90. web.Encoding = Encoding.UTF8;
  91. try
  92. {
  93. string page = web.DownloadString(searchUrl);
  94. JObject parsed = (JObject) JsonConvert.DeserializeObject(page);
  95. var data = parsed["data"];
  96. description = string.Format("\u0002{0}\u000F", data["title"]);
  97. if (data["duration"] == null)
  98. {
  99. return description;
  100. }
  101. TimeSpan duration = TimeSpan.FromSeconds(data["duration"].Value<double>());
  102. description += string.Format(" | Length: \u0002{0}\u000F", duration.ToString("g"));
  103. if (data["ratingCount"] != null)
  104. {
  105. int likes = data["likeCount"].Value<int>();
  106. string pluralLikes = (likes > 1) ? "s" : string.Empty;
  107. int dislikes = data["ratingCount"].Value<int>() - likes;
  108. string pluralDislikes = (dislikes > 1) ? "s" : string.Empty;
  109. double percent = 100.0*((double) likes/data["ratingCount"].Value<int>());
  110. description += string.Format(" | Rating: {0} Like{1}, {2} Dislike{3} (\u0002{4}\u000F%)", likes, pluralLikes, dislikes, pluralDislikes, Math.Round(percent, 1));
  111. }
  112. if (data["viewCount"] != null)
  113. {
  114. description += string.Format(" | Views: \u0002{0}\u000F", data["viewCount"].Value<int>());
  115. }
  116. DateTime uploadDate = Convert.ToDateTime(data["uploaded"].Value<string>());
  117. description += string.Format(" | Uploaded By: \u0002{0}\u000F on \u0002{1}\u000F", data["uploader"].Value<string>(), uploadDate.ToString("R"));
  118. if (data["contentRating"] != null)
  119. {
  120. description += " | \u0002NSFW\u000F";
  121. }
  122. }
  123. catch (WebException ex)
  124. {
  125. description = string.Empty;
  126. }
  127. return description;
  128. }
  129. }
  130. }