|
|
|
@ -4,6 +4,8 @@ using System.Text;
@@ -4,6 +4,8 @@ using System.Text;
|
|
|
|
|
using System.Text.RegularExpressions; |
|
|
|
|
using System.Web; |
|
|
|
|
using Combot.IRCServices.Messaging; |
|
|
|
|
using Newtonsoft.Json; |
|
|
|
|
using Newtonsoft.Json.Linq; |
|
|
|
|
|
|
|
|
|
namespace Combot.Modules.Plugins |
|
|
|
|
{ |
|
|
|
@ -51,7 +53,13 @@ namespace Combot.Modules.Plugins
@@ -51,7 +53,13 @@ namespace Combot.Modules.Plugins
|
|
|
|
|
{ |
|
|
|
|
case "text": |
|
|
|
|
Regex ytRegex = new Regex("(((youtube.*(v=|/v/))|(youtu\\.be/))(?<ID>[-_a-zA-Z0-9]+))"); |
|
|
|
|
if (!ytRegex.IsMatch(message.Message) || !Bot.Modules.Exists(mod => mod.Name == "YouTube")) |
|
|
|
|
if (ytRegex.IsMatch(message.Message)) |
|
|
|
|
{ |
|
|
|
|
Match ytMatch = ytRegex.Match(message.Message); |
|
|
|
|
string youtubeMessage = GetYoutubeDescription(ytMatch.Groups["ID"].Value); |
|
|
|
|
Bot.IRC.Command.SendPrivateMessage(message.Channel, youtubeMessage); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
WebClient x = new WebClient(); |
|
|
|
|
x.Encoding = Encoding.UTF8; |
|
|
|
@ -151,5 +159,62 @@ namespace Combot.Modules.Plugins
@@ -151,5 +159,62 @@ namespace Combot.Modules.Plugins
|
|
|
|
|
return string.Concat(bytes, " Bytes"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private string GetYoutubeDescription(string ID) |
|
|
|
|
{ |
|
|
|
|
string description = string.Empty; |
|
|
|
|
|
|
|
|
|
string urlTemplate = "http://gdata.youtube.com/feeds/api/videos/{0}?v=2&alt=jsonc"; |
|
|
|
|
Uri searchUrl = new Uri(string.Format(urlTemplate, ID)); |
|
|
|
|
WebClient web = new WebClient(); |
|
|
|
|
web.Encoding = Encoding.UTF8; |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
string page = web.DownloadString(searchUrl); |
|
|
|
|
|
|
|
|
|
JObject parsed = (JObject)JsonConvert.DeserializeObject(page); |
|
|
|
|
var data = parsed["data"]; |
|
|
|
|
|
|
|
|
|
description = string.Format("\u0002{0}\u000F", data["title"]); |
|
|
|
|
|
|
|
|
|
if (data["duration"] == null) |
|
|
|
|
{ |
|
|
|
|
return description; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
TimeSpan duration = TimeSpan.FromSeconds(data["duration"].Value<double>()); |
|
|
|
|
description += string.Format(" | Length: \u0002{0}\u000F", duration.ToString("g")); |
|
|
|
|
|
|
|
|
|
if (data["ratingCount"] != null) |
|
|
|
|
{ |
|
|
|
|
int likes = data["likeCount"].Value<int>(); |
|
|
|
|
string pluralLikes = (likes > 1) ? "s" : string.Empty; |
|
|
|
|
int dislikes = data["ratingCount"].Value<int>() - likes; |
|
|
|
|
string pluralDislikes = (dislikes > 1) ? "s" : string.Empty; |
|
|
|
|
double percent = 100.0 * ((double)likes / data["ratingCount"].Value<int>()); |
|
|
|
|
description += string.Format(" | Rating: {0} Like{1}, {2} Dislike{3} (\u0002{4}\u000F%)", likes, pluralLikes, dislikes, pluralDislikes, Math.Round(percent, 1)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (data["viewCount"] != null) |
|
|
|
|
{ |
|
|
|
|
description += string.Format(" | Views: \u0002{0}\u000F", data["viewCount"].Value<int>()); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
DateTime uploadDate = Convert.ToDateTime(data["uploaded"].Value<string>()); |
|
|
|
|
|
|
|
|
|
description += string.Format(" | Uploaded By: \u0002{0}\u000F on \u0002{1}\u000F", data["uploader"].Value<string>(), uploadDate.ToString("R")); |
|
|
|
|
|
|
|
|
|
if (data["contentRating"] != null) |
|
|
|
|
{ |
|
|
|
|
description += " | \u0002NSFW\u000F"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
catch (WebException ex) |
|
|
|
|
{ |
|
|
|
|
description = string.Empty; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return description; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|