The easy to use and full featured Irc Bot everyone is talking about!
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

YouTube.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. }
  17. public override void ParseCommand(CommandMessage command)
  18. {
  19. Command foundCommand = Commands.Find(c => c.Triggers.Contains(command.Command));
  20. switch (foundCommand.Name)
  21. {
  22. case "YouTube Search":
  23. YoutubeSearch(command);
  24. break;
  25. }
  26. }
  27. private void YoutubeSearch(CommandMessage command)
  28. {
  29. string urlTemplate = "https://www.googleapis.com/youtube/v3/search?part=snippet&q={0}&key={1}";
  30. Uri searchUrl = new Uri(string.Format(urlTemplate, command.Arguments["Query"], GetOptionValue("API Key")));
  31. WebClient web = new WebClient();
  32. web.Encoding = Encoding.UTF8;
  33. try
  34. {
  35. string page = web.DownloadString(searchUrl);
  36. JObject parsed = (JObject) JsonConvert.DeserializeObject(page);
  37. if (parsed["items"].Any())
  38. {
  39. string videoID = parsed["items"].First()["id"].Value<string>("videoId");
  40. string vidDescription = GetYoutubeDescription(videoID);
  41. string youtubeMessage = string.Format("{0} - {1}", vidDescription, string.Format("http://youtu.be/{0}", videoID));
  42. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, youtubeMessage);
  43. }
  44. else
  45. {
  46. string noResults = string.Format("No results found for \u0002{0}\u000F.", command.Arguments["Query"]);
  47. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, noResults);
  48. }
  49. }
  50. catch (WebException ex)
  51. {
  52. if (ex.Response != null)
  53. {
  54. int code = (int)((HttpWebResponse)ex.Response).StatusCode;
  55. string errorCode = string.Format("Unable to search for \u0002{0}\u000F. Youtube returned status code \u0002{1}\u000F.", command.Arguments["Query"], code);
  56. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorCode);
  57. }
  58. else
  59. {
  60. string errorCode = string.Format("Unable to search for \u0002{0}\u000F.", command.Arguments["Query"]);
  61. SendResponse(command.MessageType, command.Location, command.Nick.Nickname, errorCode);
  62. }
  63. }
  64. }
  65. private string GetYoutubeDescription(string ID)
  66. {
  67. string description = string.Empty;
  68. string urlTemplate = "http://gdata.youtube.com/feeds/api/videos/{0}?v=2&alt=jsonc";
  69. Uri searchUrl = new Uri(string.Format(urlTemplate, ID));
  70. WebClient web = new WebClient();
  71. web.Encoding = Encoding.UTF8;
  72. try
  73. {
  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. }
  104. catch (WebException ex)
  105. {
  106. description = string.Empty;
  107. }
  108. return description;
  109. }
  110. }
  111. }