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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. string page = web.DownloadString(searchUrl);
  53. JObject parsed = (JObject)JsonConvert.DeserializeObject(page);
  54. if (parsed["data"]["totalItems"].Value<int>() > 0)
  55. {
  56. string videoID = parsed["data"]["items"].First().Value<string>("id");
  57. string vidDescription = GetYoutubeDescription(videoID);
  58. string youtubeMessage = string.Format("{0} - {1}", vidDescription, string.Format("http://youtu.be/{0}", videoID));
  59. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, youtubeMessage);
  60. }
  61. else
  62. {
  63. string noResults = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  64. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noResults);
  65. }
  66. }
  67. private string GetYoutubeDescription(string ID)
  68. {
  69. string description = string.Empty;
  70. string urlTemplate = "http://gdata.youtube.com/feeds/api/videos/{0}?v=2&alt=jsonc";
  71. Uri searchUrl = new Uri(string.Format(urlTemplate, ID));
  72. WebClient web = new WebClient();
  73. web.Encoding = Encoding.UTF8;
  74. string page = web.DownloadString(searchUrl);
  75. JObject parsed = (JObject)JsonConvert.DeserializeObject(page);
  76. var data = parsed["data"];
  77. description = string.Format("\u0002{0}\u000F", data["title"]);
  78. if (data["duration"] == null)
  79. {
  80. return description;
  81. }
  82. TimeSpan duration = TimeSpan.FromSeconds(data["duration"].Value<double>());
  83. description += string.Format(" | Length: \u0002{0}\u000F", duration.ToString("g"));
  84. if (data["ratingCount"] != null)
  85. {
  86. int likes = data["likeCount"].Value<int>();
  87. string pluralLikes = (likes > 1) ? "s" : string.Empty;
  88. int dislikes = data["ratingCount"].Value<int>() - likes;
  89. string pluralDislikes = (dislikes > 1) ? "s" : string.Empty;
  90. double percent = 100.0 * ((double)likes / data["ratingCount"].Value<int>());
  91. description += string.Format(" | Rating: {0} Like{1}, {2} Dislike{3} (\u0002{4}\u000F%)", likes, pluralLikes, dislikes, pluralDislikes, Math.Round(percent, 1));
  92. }
  93. if (data["viewCount"] != null)
  94. {
  95. description += string.Format(" | Views: \u0002{0}\u000F", data["viewCount"].Value<int>());
  96. }
  97. DateTime uploadDate = Convert.ToDateTime(data["uploaded"].Value<string>());
  98. description += string.Format(" | Uploaded By: \u0002{0}\u000F on \u0002{1}\u000F", data["uploader"].Value<string>(), uploadDate.ToString("R"));
  99. if (data["contentRating"] != null)
  100. {
  101. description += " | \u0002NSFW\u000F";
  102. }
  103. return description;
  104. }
  105. }
  106. }