Merge remote-tracking branch 'refs/remotes/origin/master' into dev

# Conflicts:
#	NadekoBot/Modules/Translator/Helpers/GoogleTranslator.cs
This commit is contained in:
Master Kwoth 2016-06-15 16:29:45 +02:00
commit 819d130dc4
8 changed files with 116 additions and 29 deletions

View File

@ -8,6 +8,7 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace NadekoBot.Extensions namespace NadekoBot.Extensions
@ -180,6 +181,52 @@ namespace NadekoBot.Extensions
} }
} }
public static string GetOnPage<T>(this IEnumerable<T> source, int pageIndex, int itemsPerPage = 5)
{
var items = source.Skip(pageIndex * itemsPerPage).Take(itemsPerPage);
if (!items.Any())
{
return $"No items on page {pageIndex + 1}.";
}
var sb = new StringBuilder($"---page {pageIndex + 1} --\n");
var itemsDC = items as IEnumerable<KeyValuePair<string, IEnumerable<string>>>;
var itemsDS = items as IEnumerable<KeyValuePair<string, string>>;
if (itemsDC != null)
{
foreach (var item in itemsDC)
{
sb.Append($"{ Format.Code(item.Key)}\n");
int i = 1;
var last = item.Value.Last();
foreach (var value in item.Value)
{
if (last != value)
sb.AppendLine(" `├" + i++ + "─`" + Format.Bold(value));
else
sb.AppendLine(" `└" + i++ + "─`" + Format.Bold(value));
}
}
}
else if (itemsDS != null)
{
foreach (var item in itemsDS)
{
sb.Append($"{ Format.Code(item.Key)}\n");
sb.AppendLine(" `└─`" + Format.Bold(item.Value));
}
}
else
{
foreach (var item in items)
{
sb.Append($"{ Format.Code(item.ToString())} \n");
}
}
return sb.ToString();
}
/// <summary> /// <summary>
/// Gets the program runtime /// Gets the program runtime
/// </summary> /// </summary>

View File

@ -1,9 +1,11 @@
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using Discord.Modules; using Discord.Modules;
using NadekoBot.Classes.Conversations.Commands; using NadekoBot.Classes.Conversations.Commands;
using NadekoBot.DataModels; using NadekoBot.DataModels;
using NadekoBot.Extensions; using NadekoBot.Extensions;
using NadekoBot.Classes;
using System.Drawing.Drawing2D;
using NadekoBot.Modules.Permissions.Classes; using NadekoBot.Modules.Permissions.Classes;
using NadekoBot.Properties; using NadekoBot.Properties;
using System; using System;
@ -194,12 +196,13 @@ namespace NadekoBot.Modules.Conversations
return; return;
var usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault(); var usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault();
var text = ""; var text = "";
var avatar = await GetAvatar(usr.AvatarUrl);
text = usr?.Name ?? e.GetArg("user"); text = usr?.Name ?? e.GetArg("user");
await e.Channel.SendFile("ripzor_m8.png", var file = RipUser(text, avatar, string.IsNullOrWhiteSpace(e.GetArg("year"))
RipName(text, string.IsNullOrWhiteSpace(e.GetArg("year"))
? null ? null
: e.GetArg("year"))) : e.GetArg("year"));
.ConfigureAwait(false); await e.Channel.SendFile("ripzor_m8.png",
file);
}); });
if (!NadekoBot.Config.DontJoinServers) if (!NadekoBot.Config.DontJoinServers)
{ {
@ -317,14 +320,19 @@ namespace NadekoBot.Modules.Conversations
}); });
} }
public Stream RipName(string name, string year = null)
/// <summary>
/// Create a RIP image of the given name and avatar, with an optional year
/// </summary>
/// <param name="name"></param>
/// <param name="avatar"></param>
/// <param name="year"></param>
/// <returns></returns>
public Stream RipUser(string name, Image avatar, string year = null)
{ {
var bm = Resources.rip; var bm = Resources.rip;
var offset = name.Length * 2;
var offset = name.Length * 5;
var fontSize = 20; var fontSize = 20;
if (name.Length > 10) if (name.Length > 10)
{ {
fontSize -= (name.Length - 10) / 2; fontSize -= (name.Length - 10) / 2;
@ -332,14 +340,35 @@ namespace NadekoBot.Modules.Conversations
//TODO use measure string //TODO use measure string
var g = Graphics.FromImage(bm); var g = Graphics.FromImage(bm);
g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, 100 - offset, 200); g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, 100 - offset, 220);
g.DrawString((year ?? "?") + " - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 235); g.DrawString((year ?? "?") + " - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 240);
g.DrawImage(avatar, 80, 135);
g.DrawImage((Image)Resources.rose_overlay, 0, 0);
g.Flush(); g.Flush();
g.Dispose(); g.Dispose();
return bm.ToStream(ImageFormat.Png); return bm.ToStream(ImageFormat.Png);
} }
public static async Task<Image> GetAvatar(string url)
{
var stream = await SearchHelper.GetResponseStreamAsync(url);
Bitmap bmp = new Bitmap(100, 100);
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddEllipse(0, 0, bmp.Width, bmp.Height);
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.SetClip(gp);
gr.DrawImage(Image.FromStream(stream), Point.Empty);
}
}
return bmp;
}
private static Func<CommandEventArgs, Task> SayYes() private static Func<CommandEventArgs, Task> SayYes()
=> async e => await e.Channel.SendMessage("Yes. :)").ConfigureAwait(false); => async e => await e.Channel.SendMessage("Yes. :)").ConfigureAwait(false);
} }

View File

@ -17,6 +17,8 @@ namespace NadekoBot.Modules.Help
public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Help; public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Help;
public override void Install(ModuleManager manager) public override void Install(ModuleManager manager)
{ {
manager.CreateCommands("", cgb => manager.CreateCommands("", cgb =>

View File

@ -4,7 +4,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Web; using System.Web;
@ -75,6 +74,7 @@ namespace NadekoBot.Modules.Translator.Helpers
this.TranslationTime = TimeSpan.Zero; this.TranslationTime = TimeSpan.Zero;
DateTime tmStart = DateTime.Now; DateTime tmStart = DateTime.Now;
string translation = string.Empty; string translation = string.Empty;
string text = string.Empty;
try try
{ {
@ -83,31 +83,26 @@ namespace NadekoBot.Modules.Translator.Helpers
GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage), GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage),
GoogleTranslator.LanguageEnumToIdentifier(targetLanguage), GoogleTranslator.LanguageEnumToIdentifier(targetLanguage),
HttpUtility.UrlEncode(sourceText)); HttpUtility.UrlEncode(sourceText));
string outputFile = Path.GetTempFileName();
using (WebClient wc = new WebClient()) using (WebClient wc = new WebClient())
{ {
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"); wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
wc.DownloadFile(url, outputFile); text = wc.DownloadString(url);
} }
// Get translated text // Get translated text
if (File.Exists(outputFile)) // Get phrase collection
// string text = File.ReadAllText(outputFile);
int index = text.IndexOf(string.Format(",,\"{0}\"", GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage)));
if (index == -1)
{ {
// Translation of single word
// Get phrase collection int startQuote = text.IndexOf('\"');
string text = File.ReadAllText(outputFile); if (startQuote != -1)
int index = text.IndexOf(string.Format(",,\"{0}\"", GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage)));
if (index == -1)
{ {
// Translation of single word int endQuote = text.IndexOf('\"', startQuote + 1);
int startQuote = text.IndexOf('\"'); if (endQuote != -1)
if (startQuote != -1)
{ {
int endQuote = text.IndexOf('\"', startQuote + 1); translation = text.Substring(startQuote + 1, endQuote - startQuote - 1);
if (endQuote != -1)
{
translation = text.Substring(startQuote + 1, endQuote - startQuote - 1);
}
} }
} }
else else

View File

@ -492,6 +492,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="lib\ScaredFingers.UnitsConversion.dll" /> <Content Include="lib\ScaredFingers.UnitsConversion.dll" />
<None Include="resources\images\rose_overlay.png" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. <!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@ -730,6 +730,16 @@ namespace NadekoBot.Properties {
} }
} }
/// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary>
public static System.Drawing.Bitmap rose_overlay {
get {
object obj = ResourceManager.GetObject("rose_overlay", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
/// <summary> /// <summary>
/// Looks up a localized resource of type System.Drawing.Bitmap. /// Looks up a localized resource of type System.Drawing.Bitmap.
/// </summary> /// </summary>

View File

@ -322,4 +322,7 @@
<data name="_9_of_spades" type="System.Resources.ResXFileRef, System.Windows.Forms"> <data name="_9_of_spades" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\resources\images\cards\9_of_spades.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value> <value>..\resources\images\cards\9_of_spades.jpg;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data> </data>
<data name="rose_overlay" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\resources\images\rose_overlay.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root> </root>

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB