The easy to use and full featured Irc Bot everyone is talking about!
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

YouTube.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.ModuleClasses
  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. Regex urlRegex = new Regex("(((youtube.*(v=|/v/))|(youtu\\.be/))(?<ID>[-_a-zA-Z0-9]+))");
  31. if (urlRegex.IsMatch(message.Message))
  32. {
  33. Match urlMatch = urlRegex.Match(message.Message);
  34. string youtubeMessage = GetYoutubeDescription(urlMatch.Groups["ID"].Value);
  35. Bot.IRC.SendPrivateMessage(message.Channel, youtubeMessage);
  36. }
  37. }
  38. private void YoutubeSearch(CommandMessage command)
  39. {
  40. string urlTemplate = "http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=1&q={0}";
  41. Uri searchUrl = new Uri(string.Format(urlTemplate, command.Arguments["Query"]));
  42. WebClient web = new WebClient();
  43. web.Encoding = Encoding.UTF8;
  44. string page = web.DownloadString(searchUrl);
  45. JObject parsed = (JObject)JsonConvert.DeserializeObject(page);
  46. if (parsed["data"]["totalItems"].Value<int>() > 0)
  47. {
  48. string videoID = parsed["data"]["items"].First().Value<string>("id");
  49. string vidDescription = GetYoutubeDescription(videoID);
  50. string youtubeMessage = string.Format("{0} - {1}", vidDescription, string.Format("http://youtu.be/{0}", videoID));
  51. switch (command.MessageType)
  52. {
  53. case MessageType.Channel:
  54. Bot.IRC.SendPrivateMessage(command.Location, youtubeMessage);
  55. break;
  56. case MessageType.Query:
  57. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, youtubeMessage);
  58. break;
  59. case MessageType.Notice:
  60. Bot.IRC.SendNotice(command.Nick.Nickname, youtubeMessage);
  61. break;
  62. }
  63. }
  64. else
  65. {
  66. string noResults = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  67. switch (command.MessageType)
  68. {
  69. case MessageType.Channel:
  70. Bot.IRC.SendPrivateMessage(command.Location, noResults);
  71. break;
  72. case MessageType.Query:
  73. Bot.IRC.SendPrivateMessage(command.Nick.Nickname, noResults);
  74. break;
  75. case MessageType.Notice:
  76. Bot.IRC.SendNotice(command.Nick.Nickname, noResults);
  77. break;
  78. }
  79. }
  80. }
  81. private string GetYoutubeDescription(string ID)
  82. {
  83. string description = string.Empty;
  84. string urlTemplate = "http://gdata.youtube.com/feeds/api/videos/{0}?v=2&alt=jsonc";
  85. Uri searchUrl = new Uri(string.Format(urlTemplate, ID));
  86. WebClient web = new WebClient();
  87. web.Encoding = Encoding.UTF8;
  88. string page = web.DownloadString(searchUrl);
  89. JObject parsed = (JObject)JsonConvert.DeserializeObject(page);
  90. var data = parsed["data"];
  91. description = string.Format("\u0002{0}\u000F", data["title"]);
  92. if (data["duration"] == null)
  93. {
  94. return description;
  95. }
  96. TimeSpan duration = TimeSpan.FromSeconds(data["duration"].Value<double>());
  97. description += string.Format(" | Length: \u0002{0}\u000F", duration.ToString("g"));
  98. if (data["ratingCount"] != null)
  99. {
  100. int likes = data["likeCount"].Value<int>();
  101. string pluralLikes = (likes > 1) ? "s" : string.Empty;
  102. int dislikes = data["ratingCount"].Value<int>() - likes;
  103. string pluralDislikes = (dislikes > 1) ? "s" : string.Empty;
  104. double percent = 100.0 * ((double)likes / data["ratingCount"].Value<int>());
  105. description += string.Format(" | Rating: {0} Like{1}, {2} Dislike{3} (\u0002{4}\u000F%)", likes, pluralLikes, dislikes, pluralDislikes, Math.Round(percent, 1));
  106. }
  107. if (data["viewCount"] != null)
  108. {
  109. description += string.Format(" | Views: \u0002{0}\u000F", data["viewCount"].Value<int>());
  110. }
  111. DateTime uploadDate = Convert.ToDateTime(data["uploaded"].Value<string>());
  112. description += string.Format(" | Uploaded By: \u0002{0}\u000F on \u0002{1}\u000F", data["uploader"].Value<string>(), uploadDate.ToString("R"));
  113. if (data["contentRating"] != null)
  114. {
  115. description += " | \u0002NSFW\u000F";
  116. }
  117. return description;
  118. }
  119. }
  120. }