Browse Source

Moved youtube url parsing to the Url Parsing module.

master
Teknikode 7 years ago
parent
commit
65bb76ed82
  1. 14
      Combot/Bot.cs
  2. 4
      Modules/Url Parsing/Url Parsing.csproj
  3. 67
      Modules/Url Parsing/Url_Parsing.cs
  4. 1
      Modules/Url Parsing/packages.config
  5. 20
      Modules/YouTube/YouTube.cs

14
Combot/Bot.cs

@ -93,7 +93,7 @@ namespace Combot @@ -93,7 +93,7 @@ namespace Combot
}
i++;
}
catch (SocketException ex)
catch (SocketException)
{
break;
}
@ -280,7 +280,6 @@ namespace Combot @@ -280,7 +280,6 @@ namespace Combot
break;
case AccessType.Owner:
return true;
break;
}
}
}
@ -437,14 +436,7 @@ namespace Combot @@ -437,14 +436,7 @@ namespace Combot
switch (mode.Mode)
{
case ChannelMode.k:
if (mode.Set)
{
channel.Key = mode.Parameter;
}
else
{
channel.Key = string.Empty;
}
channel.Key = (mode.Set) ? mode.Parameter : string.Empty;
ServerConfig.Save();
break;
}
@ -496,7 +488,7 @@ namespace Combot @@ -496,7 +488,7 @@ namespace Combot
case IRCErrorCode.ERR_NICKNAMEINUSE:
if (LoggedIn == false)
{
string nick = string.Empty;
string nick;
if (IRC.Nickname == ServerConfig.Nicknames[CurNickChoice] && ServerConfig.Nicknames.Count > CurNickChoice + 1)
{
GhostSent = false;

4
Modules/Url Parsing/Url Parsing.csproj

@ -32,6 +32,10 @@ @@ -32,6 +32,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Web" />

67
Modules/Url Parsing/Url_Parsing.cs

@ -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;
}
}
}

1
Modules/Url Parsing/packages.config

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="GitVersionTask" version="2.0.1" targetFramework="net451" developmentDependency="true" />
<package id="Newtonsoft.Json" version="6.0.8" targetFramework="net451" />
</packages>

20
Modules/YouTube/YouTube.cs

@ -14,7 +14,6 @@ namespace Combot.Modules.Plugins @@ -14,7 +14,6 @@ namespace Combot.Modules.Plugins
public override void Initialize()
{
Bot.CommandReceivedEvent += HandleCommandEvent;
Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
}
public override void ParseCommand(CommandMessage command)
@ -29,25 +28,6 @@ namespace Combot.Modules.Plugins @@ -29,25 +28,6 @@ namespace Combot.Modules.Plugins
}
}
private void HandleChannelMessage(object sender, ChannelMessage message)
{
if (Enabled
&& !Bot.ServerConfig.ChannelBlacklist.Contains(message.Channel)
&& !Bot.ServerConfig.NickBlacklist.Contains(message.Sender.Nickname)
&& !ChannelBlacklist.Contains(message.Channel)
&& !NickBlacklist.Contains(message.Sender.Nickname)
&& !Bot.IsCommand(message.Message))
{
Regex urlRegex = new Regex("(((youtube.*(v=|/v/))|(youtu\\.be/))(?<ID>[-_a-zA-Z0-9]+))");
if (urlRegex.IsMatch(message.Message))
{
Match urlMatch = urlRegex.Match(message.Message);
string youtubeMessage = GetYoutubeDescription(urlMatch.Groups["ID"].Value);
Bot.IRC.Command.SendPrivateMessage(message.Channel, youtubeMessage);
}
}
}
private void YoutubeSearch(CommandMessage command)
{
string urlTemplate = "http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&max-results=1&q={0}";

Loading…
Cancel
Save