Merge remote-tracking branch 'refs/remotes/Kwoth/master'

# Conflicts:
#	NadekoBot/NadekoBot.cs
This commit is contained in:
appelemac 2016-03-27 11:00:29 +02:00
commit 7c17cc24cd
8 changed files with 861 additions and 50 deletions

View File

@ -0,0 +1,62 @@
using NadekoBot.Extensions;
using System.Collections.Generic;
namespace NadekoBot.Classes.IMDB
{
public class ImdbMovie
{
public bool Status { get; set; }
public string Id { get; set; }
public string Title { get; set; }
public string OriginalTitle { get; set; }
public string Year { get; set; }
public string Rating { get; set; }
public string Plot { get; set; }
public string Poster { get; set; }
public List<string> Genres { get; set; }
//public ArrayList Directors { get; set; }
//public ArrayList Writers { get; set; }
//public ArrayList Cast { get; set; }
//public ArrayList Producers { get; set; }
//public ArrayList Musicians { get; set; }
//public ArrayList Cinematographers { get; set; }
//public ArrayList Editors { get; set; }
//public string MpaaRating { get; set; }
//public string ReleaseDate { get; set; }
//public ArrayList PlotKeywords { get; set; }
//public string PosterLarge { get; set; }
//public string PosterFull { get; set; }
//public string Runtime { get; set; }
//public string Top250 { get; set; }
//public string Oscars { get; set; }
//public string Awards { get; set; }
//public string Nominations { get; set; }
//public string Storyline { get; set; }
//public string Tagline { get; set; }
//public string Votes { get; set; }
//public ArrayList Languages { get; set; }
//public ArrayList Countries { get; set; }
//public Dictionary<string, string> ReleaseDates { get; set; }
//public ArrayList MediaImages { get; set; }
//public ArrayList RecommendedTitles { get; set; }
public string ImdbURL { get; set; }
public Dictionary<string, string> Aka { get; set; }
public override string ToString() =>
$@"`Title:` {Title} {(string.IsNullOrEmpty(OriginalTitle) ? "" : $"({OriginalTitle})")}
`Year:` {Year}
`Rating:` {Rating}
`Genre:` {GenresAsString}
`Link:` <{ImdbURL}>
`Plot:` {System.Net.WebUtility.HtmlDecode(Plot.TrimTo(500))}
`img:` " + Poster.ShortenUrl().Result;
//public string EnglishTitle => Aka.ContainsKey("USA") ? Aka["USA"] :
// (Aka.ContainsKey("UK") ? Aka["UK"] :
// (Aka.ContainsKey("(original title)") ? Aka["(original title)"] :
// (Aka.ContainsKey("(original)") ? Aka["(original)"] : OriginalTitle)));
public string GenresAsString =>
string.Join(", ", Genres);
}
}

View File

@ -0,0 +1,227 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
/*******************************************************************************
* Free ASP.net IMDb Scraper API for the new IMDb Template.
* Author: Abhinay Rathore
* Website: http://www.AbhinayRathore.com
* Blog: http://web3o.blogspot.com
* More Info: http://web3o.blogspot.com/2010/11/aspnetc-imdb-scraping-api.html
* Updated By: Gergo Torcsvari
* Last Updated: Feb, 2016
*******************************************************************************/
namespace NadekoBot.Classes.IMDB
{
public static class ImdbScraper
{
//Search Engine URLs
private static string GoogleSearch = "https://www.google.com/search?q=imdb+";
private static string BingSearch = "http://www.bing.com/search?q=imdb+";
private static string AskSearch = "http://www.ask.com/web?q=imdb+";
//Constructor
public static ImdbMovie ImdbScrape(string MovieName, bool GetExtraInfo = true)
{
ImdbMovie mov = new ImdbMovie();
string imdbUrl = GetIMDbUrl(System.Uri.EscapeUriString(MovieName));
mov.Status = false;
if (!string.IsNullOrEmpty(imdbUrl))
{
ParseIMDbPage(imdbUrl, GetExtraInfo, mov);
}
return mov;
}
public static ImdbMovie ImdbScrapeFromId(string imdbId, bool GetExtraInfo = true)
{
ImdbMovie mov = new ImdbMovie();
string imdbUrl = "http://www.imdb.com/title/" + imdbId + "/";
mov.Status = false;
if (!string.IsNullOrEmpty(imdbUrl))
{
ParseIMDbPage(imdbUrl, GetExtraInfo, mov);
}
return mov;
}
public static string GetIMDBId(string MovieName)
{
string imdbUrl = GetIMDbUrl(System.Uri.EscapeUriString(MovieName));
return match(@"http://www.imdb.com/title/(tt\d{7})", imdbUrl);
}
//Get IMDb URL from search results
private static string GetIMDbUrl(string MovieName, string searchEngine = "google")
{
string url = GoogleSearch + MovieName; //default to Google search
if (searchEngine.ToLower().Equals("bing")) url = BingSearch + MovieName;
if (searchEngine.ToLower().Equals("ask")) url = AskSearch + MovieName;
string html = GetUrlData(url);
ArrayList imdbUrls = MatchAll(@"<a href=""(http://www.imdb.com/title/tt\d{7}/)"".*?>.*?</a>", html);
if (imdbUrls.Count > 0)
return (string)imdbUrls[0]; //return first IMDb result
else if (searchEngine.ToLower().Equals("google")) //if Google search fails
return GetIMDbUrl(MovieName, "bing"); //search using Bing
else if (searchEngine.ToLower().Equals("bing")) //if Bing search fails
return GetIMDbUrl(MovieName, "ask"); //search using Ask
else //search fails
return string.Empty;
}
//Parse IMDb page data
private static void ParseIMDbPage(string imdbUrl, bool GetExtraInfo, ImdbMovie mov)
{
string html = GetUrlData(imdbUrl + "combined");
mov.Id = match(@"<link rel=""canonical"" href=""http://www.imdb.com/title/(tt\d{7})/combined"" />", html);
if (!string.IsNullOrEmpty(mov.Id))
{
mov.Status = true;
mov.Title = match(@"<title>(IMDb \- )*(.*?) \(.*?</title>", html, 2);
mov.OriginalTitle = match(@"title-extra"">(.*?)<", html);
mov.Year = match(@"<title>.*?\(.*?(\d{4}).*?\).*?</title>", match(@"(<title>.*?</title>)", html));
mov.Rating = match(@"<b>(\d.\d)/10</b>", html);
mov.Genres = MatchAll(@"<a.*?>(.*?)</a>", match(@"Genre.?:(.*?)(</div>|See more)", html)).Cast<string>().ToList();
mov.Plot = match(@"Plot:</h5>.*?<div class=""info-content"">(.*?)(<a|</div)", html);
//mov.Directors = matchAll(@"<td valign=""top""><a.*?href=""/name/.*?/"">(.*?)</a>", match(@"Directed by</a></h5>(.*?)</table>", html));
//mov.Writers = matchAll(@"<td valign=""top""><a.*?href=""/name/.*?/"">(.*?)</a>", match(@"Writing credits</a></h5>(.*?)</table>", html));
//mov.Producers = matchAll(@"<td valign=""top""><a.*?href=""/name/.*?/"">(.*?)</a>", match(@"Produced by</a></h5>(.*?)</table>", html));
//mov.Musicians = matchAll(@"<td valign=""top""><a.*?href=""/name/.*?/"">(.*?)</a>", match(@"Original Music by</a></h5>(.*?)</table>", html));
//mov.Cinematographers = matchAll(@"<td valign=""top""><a.*?href=""/name/.*?/"">(.*?)</a>", match(@"Cinematography by</a></h5>(.*?)</table>", html));
//mov.Editors = matchAll(@"<td valign=""top""><a.*?href=""/name/.*?/"">(.*?)</a>", match(@"Film Editing by</a></h5>(.*?)</table>", html));
//mov.Cast = matchAll(@"<td class=""nm""><a.*?href=""/name/.*?/"".*?>(.*?)</a>", match(@"<h3>Cast</h3>(.*?)</table>", html));
//mov.PlotKeywords = matchAll(@"<a.*?>(.*?)</a>", match(@"Plot Keywords:</h5>.*?<div class=""info-content"">(.*?)</div", html));
//mov.ReleaseDate = match(@"Release Date:</h5>.*?<div class=""info-content"">.*?(\d{1,2} (January|February|March|April|May|June|July|August|September|October|November|December) (19|20)\d{2})", html);
//mov.Runtime = match(@"Runtime:</h5><div class=""info-content"">(\d{1,4}) min[\s]*.*?</div>", html);
//mov.Top250 = match(@"Top 250: #(\d{1,3})<", html);
//mov.Oscars = match(@"Won (\d+) Oscars?\.", html);
//if (string.IsNullOrEmpty(mov.Oscars) && "Won Oscar.".Equals(match(@"(Won Oscar\.)", html))) mov.Oscars = "1";
//mov.Awards = match(@"(\d{1,4}) wins", html);
//mov.Nominations = match(@"(\d{1,4}) nominations", html);
//mov.Tagline = match(@"Tagline:</h5>.*?<div class=""info-content"">(.*?)(<a|</div)", html);
//mov.MpaaRating = match(@"MPAA</a>:</h5><div class=""info-content"">Rated (G|PG|PG-13|PG-14|R|NC-17|X) ", html);
//mov.Votes = match(@">(\d+,?\d*) votes<", html);
//mov.Languages = matchAll(@"<a.*?>(.*?)</a>", match(@"Language.?:(.*?)(</div>|>.?and )", html));
//mov.Countries = matchAll(@"<a.*?>(.*?)</a>", match(@"Country:(.*?)(</div>|>.?and )", html));
mov.Poster = match(@"<div class=""photo"">.*?<a name=""poster"".*?><img.*?src=""(.*?)"".*?</div>", html);
if (!string.IsNullOrEmpty(mov.Poster) && mov.Poster.IndexOf("media-imdb.com") > 0)
{
mov.Poster = Regex.Replace(mov.Poster, @"_V1.*?.jpg", "_V1._SY200.jpg");
//mov.PosterLarge = Regex.Replace(mov.Poster, @"_V1.*?.jpg", "_V1._SY500.jpg");
//mov.PosterFull = Regex.Replace(mov.Poster, @"_V1.*?.jpg", "_V1._SY0.jpg");
}
else
{
mov.Poster = string.Empty;
//mov.PosterLarge = string.Empty;
//mov.PosterFull = string.Empty;
}
mov.ImdbURL = "http://www.imdb.com/title/" + mov.Id + "/";
if (GetExtraInfo)
{
string plotHtml = GetUrlData(imdbUrl + "plotsummary");
//mov.Storyline = match(@"<p class=""plotpar"">(.*?)(<i>|</p>)", plotHtml);
GetReleaseDatesAndAka(mov);
//mov.MediaImages = getMediaImages(mov);
//mov.RecommendedTitles = getRecommendedTitles(mov);
}
}
}
//Get all release dates and aka-s
private static void GetReleaseDatesAndAka(ImdbMovie mov)
{
Dictionary<string, string> release = new Dictionary<string, string>();
string releasehtml = GetUrlData("http://www.imdb.com/title/" + mov.Id + "/releaseinfo");
foreach (string r in MatchAll(@"<tr class="".*?"">(.*?)</tr>", match(@"<table id=""release_dates"" class=""subpage_data spFirst"">\n*?(.*?)</table>", releasehtml)))
{
Match rd = new Regex(@"<td>(.*?)</td>\n*?.*?<td class=.*?>(.*?)</td>", RegexOptions.Multiline).Match(r);
release[StripHTML(rd.Groups[1].Value.Trim())] = StripHTML(rd.Groups[2].Value.Trim());
}
//mov.ReleaseDates = release;
Dictionary<string, string> aka = new Dictionary<string, string>();
ArrayList list = MatchAll(@".*?<tr class="".*?"">(.*?)</tr>", match(@"<table id=""akas"" class=.*?>\n*?(.*?)</table>", releasehtml));
foreach (string r in list)
{
Match rd = new Regex(@"\n*?.*?<td>(.*?)</td>\n*?.*?<td>(.*?)</td>", RegexOptions.Multiline).Match(r);
aka[StripHTML(rd.Groups[1].Value.Trim())] = StripHTML(rd.Groups[2].Value.Trim());
}
mov.Aka = aka;
}
//Get all media images
private static ArrayList GetMediaImages(ImdbMovie mov)
{
ArrayList list = new ArrayList();
string mediaurl = "http://www.imdb.com/title/" + mov.Id + "/mediaindex";
string mediahtml = GetUrlData(mediaurl);
int pagecount = MatchAll(@"<a href=""\?page=(.*?)"">", match(@"<span style=""padding: 0 1em;"">(.*?)</span>", mediahtml)).Count;
for (int p = 1; p <= pagecount + 1; p++)
{
mediahtml = GetUrlData(mediaurl + "?page=" + p);
foreach (Match m in new Regex(@"src=""(.*?)""", RegexOptions.Multiline).Matches(match(@"<div class=""thumb_list"" style=""font-size: 0px;"">(.*?)</div>", mediahtml)))
{
String image = m.Groups[1].Value;
list.Add(Regex.Replace(image, @"_V1\..*?.jpg", "_V1._SY0.jpg"));
}
}
return list;
}
//Get Recommended Titles
private static ArrayList GetRecommendedTitles(ImdbMovie mov)
{
ArrayList list = new ArrayList();
string recUrl = "http://www.imdb.com/widget/recommendations/_ajax/get_more_recs?specs=p13nsims%3A" + mov.Id;
string json = GetUrlData(recUrl);
list = MatchAll(@"title=\\""(.*?)\\""", json);
HashSet<String> set = new HashSet<string>();
foreach (String rec in list) set.Add(rec);
return new ArrayList(set.ToList());
}
/*******************************[ Helper Methods ]********************************/
//Match single instance
private static string match(string regex, string html, int i = 1)
{
return new Regex(regex, RegexOptions.Multiline).Match(html).Groups[i].Value.Trim();
}
//Match all instances and return as ArrayList
private static ArrayList MatchAll(string regex, string html, int i = 1)
{
ArrayList list = new ArrayList();
foreach (Match m in new Regex(regex, RegexOptions.Multiline).Matches(html))
list.Add(m.Groups[i].Value.Trim());
return list;
}
//Strip HTML Tags
private static string StripHTML(string inputString)
{
return Regex.Replace(inputString, @"<.*?>", string.Empty);
}
//Get URL Data
private static string GetUrlData(string url)
{
WebClient client = new WebClient();
Random r = new Random();
//Random IP Address
//client.Headers["X-Forwarded-For"] = r.Next(0, 255) + "." + r.Next(0, 255) + "." + r.Next(0, 255) + "." + r.Next(0, 255);
//Random User-Agent
client.Headers["User-Agent"] = "Mozilla/" + r.Next(3, 5) + ".0 (Windows NT " + r.Next(3, 5) + "." + r.Next(0, 2) + "; rv:37.0) Gecko/20100101 Firefox/" + r.Next(30, 37) + "." + r.Next(0, 5);
Stream datastream = client.OpenRead(url);
StreamReader reader = new StreamReader(datastream);
StringBuilder sb = new StringBuilder();
//TODO: Coud be reader error must catch and drop!!!
while (!reader.EndOfStream)
sb.Append(reader.ReadLine());
return sb.ToString();
}
}
}

View File

@ -1,20 +1,24 @@
using System;
using Discord.Commands;
using Discord.Modules;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using Discord.Commands;
using NadekoBot.Classes;
using NadekoBot.Classes.IMDB;
using NadekoBot.Commands;
using NadekoBot.Extensions;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using NadekoBot.Classes;
using NadekoBot.Commands;
using System.Net;
namespace NadekoBot.Modules {
internal class Searches : DiscordModule {
namespace NadekoBot.Modules
{
internal class Searches : DiscordModule
{
private readonly Random rng;
public Searches() {
public Searches()
{
commands.Add(new LoLCommands(this));
commands.Add(new StreamNotifications(this));
rng = new Random();
@ -22,8 +26,10 @@ namespace NadekoBot.Modules {
public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Searches;
public override void Install(ModuleManager manager) {
manager.CreateCommands("", cgb => {
public override void Install(ModuleManager manager)
{
manager.CreateCommands("", cgb =>
{
cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance);
@ -33,7 +39,8 @@ namespace NadekoBot.Modules {
.Description("Shows weather data for a specified city and a country BOTH ARE REQUIRED. Weather api is very random if you make a mistake.")
.Parameter("city", ParameterType.Required)
.Parameter("country", ParameterType.Required)
.Do(async e => {
.Do(async e =>
{
var city = e.GetArg("city").Replace(" ", "");
var country = e.GetArg("country").Replace(" ", "");
var response = await SearchHelper.GetResponseStringAsync($"http://api.lawlypopzz.xyz/nadekobot/weather/?city={city}&country={country}");
@ -51,7 +58,8 @@ $@"🌍 **Weather for** 【{obj["target"]}】
cgb.CreateCommand(Prefix + "yt")
.Parameter("query", ParameterType.Unparsed)
.Description("Searches youtubes and shows the first result")
.Do(async e => {
.Do(async e =>
{
if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query")))) return;
var shortUrl = await SearchHelper.ShortenUrl(await SearchHelper.FindYoutubeUrlByKeywords(e.GetArg("query")));
@ -62,12 +70,16 @@ $@"🌍 **Weather for** 【{obj["target"]}】
.Alias(Prefix + "anime", Prefix + "aq")
.Parameter("query", ParameterType.Unparsed)
.Description("Queries anilist for an anime and shows the first result.")
.Do(async e => {
.Do(async e =>
{
if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query")))) return;
string result;
try {
try
{
result = (await SearchHelper.GetAnimeData(e.GetArg("query"))).ToString();
} catch {
}
catch
{
await e.Channel.SendMessage("Failed to find that anime.");
return;
}
@ -75,16 +87,43 @@ $@"🌍 **Weather for** 【{obj["target"]}】
await e.Channel.SendMessage(result.ToString());
});
cgb.CreateCommand(Prefix + "imdb")
.Parameter("query", ParameterType.Unparsed)
.Description("Queries imdb for movies or series, show first result.")
.Do(async e =>
{
if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query")))) return;
await e.Channel.SendIsTyping();
string result;
try
{
var movie = ImdbScraper.ImdbScrape(e.GetArg("query"), true);
if (movie.Status) result = movie.ToString();
else result = "Failed to find that movie.";
}
catch
{
await e.Channel.SendMessage("Failed to find that movie.");
return;
}
await e.Channel.SendMessage(result.ToString());
});
cgb.CreateCommand(Prefix + "mang")
.Alias(Prefix + "manga").Alias(Prefix + "mq")
.Parameter("query", ParameterType.Unparsed)
.Description("Queries anilist for a manga and shows the first result.")
.Do(async e => {
.Do(async e =>
{
if (!(await SearchHelper.ValidateQuery(e.Channel, e.GetArg("query")))) return;
string result;
try {
try
{
result = (await SearchHelper.GetMangaData(e.GetArg("query"))).ToString();
} catch {
}
catch
{
await e.Channel.SendMessage("Failed to find that anime.");
return;
}
@ -93,7 +132,8 @@ $@"🌍 **Weather for** 【{obj["target"]}】
cgb.CreateCommand(Prefix + "randomcat")
.Description("Shows a random cat image.")
.Do(async e => {
.Do(async e =>
{
await e.Channel.SendMessage(JObject.Parse(
await SearchHelper.GetResponseStringAsync("http://www.random.cat/meow"))["file"].ToString());
});
@ -101,14 +141,18 @@ $@"🌍 **Weather for** 【{obj["target"]}】
cgb.CreateCommand(Prefix + "i")
.Description("Pulls the first image found using a search parameter. Use ~ir for different results.\n**Usage**: ~i cute kitten")
.Parameter("query", ParameterType.Unparsed)
.Do(async e => {
.Do(async e =>
{
if (string.IsNullOrWhiteSpace(e.GetArg("query")))
return;
try {
try
{
var reqString = $"https://www.googleapis.com/customsearch/v1?q={Uri.EscapeDataString(e.GetArg("query"))}&cx=018084019232060951019%3Ahs5piey28-e&num=1&searchType=image&fields=items%2Flink&key={NadekoBot.Creds.GoogleAPIKey}";
var obj = JObject.Parse(await SearchHelper.GetResponseStringAsync(reqString));
await e.Channel.SendMessage(obj["items"][0]["link"].ToString());
} catch (Exception ex) {
}
catch (Exception ex)
{
await e.Channel.SendMessage($"💢 {ex.Message}");
}
});
@ -116,21 +160,26 @@ $@"🌍 **Weather for** 【{obj["target"]}】
cgb.CreateCommand(Prefix + "ir")
.Description("Pulls a random image using a search parameter.\n**Usage**: ~ir cute kitten")
.Parameter("query", ParameterType.Unparsed)
.Do(async e => {
.Do(async e =>
{
if (string.IsNullOrWhiteSpace(e.GetArg("query")))
return;
try {
try
{
var reqString = $"https://www.googleapis.com/customsearch/v1?q={Uri.EscapeDataString(e.GetArg("query"))}&cx=018084019232060951019%3Ahs5piey28-e&num=1&searchType=image&start={ rng.Next(1, 150) }&fields=items%2Flink&key={NadekoBot.Creds.GoogleAPIKey}";
var obj = JObject.Parse(await SearchHelper.GetResponseStringAsync(reqString));
await e.Channel.SendMessage(obj["items"][0]["link"].ToString());
} catch (Exception ex) {
}
catch (Exception ex)
{
await e.Channel.SendMessage($"💢 {ex.Message}");
}
});
cgb.CreateCommand(Prefix + "lmgtfy")
.Description("Google something for an idiot.")
.Parameter("ffs", ParameterType.Unparsed)
.Do(async e => {
.Do(async e =>
{
if (e.GetArg("ffs") == null || e.GetArg("ffs").Length < 1) return;
await e.Channel.SendMessage(await $"http://lmgtfy.com/?q={ Uri.EscapeUriString(e.GetArg("ffs").ToString()) }".ShortenUrl());
});
@ -138,31 +187,38 @@ $@"🌍 **Weather for** 【{obj["target"]}】
cgb.CreateCommand(Prefix + "hs")
.Description("Searches for a Hearthstone card and shows its image. Takes a while to complete.\n**Usage**:~hs Ysera")
.Parameter("name", ParameterType.Unparsed)
.Do(async e => {
.Do(async e =>
{
var arg = e.GetArg("name");
if (string.IsNullOrWhiteSpace(arg)) {
if (string.IsNullOrWhiteSpace(arg))
{
await e.Channel.SendMessage("💢 Please enter a card name to search for.");
return;
}
await e.Channel.SendIsTyping();
var headers = new Dictionary<string, string> { { "X-Mashape-Key", NadekoBot.Creds.MashapeKey } };
var res = await SearchHelper.GetResponseStringAsync($"https://omgvamp-hearthstone-v1.p.mashape.com/cards/search/{Uri.EscapeUriString(arg)}", headers);
try {
try
{
var items = JArray.Parse(res);
var images = new List<Image>();
if (items == null)
throw new KeyNotFoundException("Cannot find a card by that name");
var cnt = 0;
items.Shuffle();
foreach (var item in items.TakeWhile(item => cnt++ < 4).Where(item => item.HasValues && item["img"] != null)) {
foreach (var item in items.TakeWhile(item => cnt++ < 4).Where(item => item.HasValues && item["img"] != null))
{
images.Add(
Image.FromStream(await SearchHelper.GetResponseStreamAsync(item["img"].ToString())));
}
if (items.Count > 4) {
if (items.Count > 4)
{
await e.Channel.SendMessage("⚠ Found over 4 images. Showing random 4.");
}
await e.Channel.SendFile(arg + ".png", (await images.MergeAsync()).ToStream(System.Drawing.Imaging.ImageFormat.Png));
} catch (Exception ex) {
}
catch (Exception ex)
{
await e.Channel.SendMessage($"💢 Error {ex.Message}");
}
});
@ -170,22 +226,30 @@ $@"🌍 **Weather for** 【{obj["target"]}】
cgb.CreateCommand(Prefix + "osu")
.Description("Shows osu stats for a player.\n**Usage**:~osu Name")
.Parameter("usr", ParameterType.Unparsed)
.Do(async e => {
.Do(async e =>
{
if (string.IsNullOrWhiteSpace(e.GetArg("usr")))
return;
using (WebClient cl = new WebClient()) {
try {
using (WebClient cl = new WebClient())
{
try
{
cl.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
cl.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Windows NT 6.2; Win64; x64)");
cl.DownloadDataAsync(new Uri($"http://lemmmy.pw/osusig/sig.php?uname={ e.GetArg("usr") }&flagshadow&xpbar&xpbarhex&pp=2"));
cl.DownloadDataCompleted += async (s, cle) => {
try {
cl.DownloadDataCompleted += async (s, cle) =>
{
try
{
await e.Channel.SendFile($"{e.GetArg("usr")}.png", new MemoryStream(cle.Result));
await e.Channel.SendMessage($"`Profile Link:`https://osu.ppy.sh/u/{Uri.EscapeDataString(e.GetArg("usr"))}\n`Image provided by https://lemmmy.pw/osusig`");
} catch { }
}
catch { }
};
} catch {
}
catch
{
await e.Channel.SendMessage("💢 Failed retrieving osu signature :\\");
}
}
@ -194,23 +258,28 @@ $@"🌍 **Weather for** 【{obj["target"]}】
cgb.CreateCommand(Prefix + "ud")
.Description("Searches Urban Dictionary for a word.\n**Usage**:~ud Pineapple")
.Parameter("query", ParameterType.Unparsed)
.Do(async e => {
.Do(async e =>
{
var arg = e.GetArg("query");
if (string.IsNullOrWhiteSpace(arg)) {
if (string.IsNullOrWhiteSpace(arg))
{
await e.Channel.SendMessage("💢 Please enter a search term.");
return;
}
await e.Channel.SendIsTyping();
var headers = new Dictionary<string, string> { { "X-Mashape-Key", NadekoBot.Creds.MashapeKey } };
var res = await SearchHelper.GetResponseStringAsync($"https://mashape-community-urban-dictionary.p.mashape.com/define?term={Uri.EscapeUriString(arg)}", headers);
try {
try
{
var items = JObject.Parse(res);
var sb = new System.Text.StringBuilder();
sb.AppendLine($"`Term:` {items["list"][0]["word"].ToString()}");
sb.AppendLine($"`Definition:` {items["list"][0]["definition"].ToString()}");
sb.Append($"`Link:` <{await items["list"][0]["permalink"].ToString().ShortenUrl()}>");
await e.Channel.SendMessage(sb.ToString());
} catch {
}
catch
{
await e.Channel.SendMessage("💢 Failed finding a definition for that term.");
}
});
@ -218,30 +287,36 @@ $@"🌍 **Weather for** 【{obj["target"]}】
cgb.CreateCommand(Prefix + "#")
.Description("Searches Tagdef.com for a hashtag.\n**Usage**:~# ff")
.Parameter("query", ParameterType.Unparsed)
.Do(async e => {
.Do(async e =>
{
var arg = e.GetArg("query");
if (string.IsNullOrWhiteSpace(arg)) {
if (string.IsNullOrWhiteSpace(arg))
{
await e.Channel.SendMessage("💢 Please enter a search term.");
return;
}
await e.Channel.SendIsTyping();
var headers = new Dictionary<string, string> { { "X-Mashape-Key", NadekoBot.Creds.MashapeKey } };
var res = await SearchHelper.GetResponseStringAsync($"https://tagdef.p.mashape.com/one.{Uri.EscapeUriString(arg)}.json", headers);
try {
try
{
var items = JObject.Parse(res);
var sb = new System.Text.StringBuilder();
sb.AppendLine($"`Hashtag:` {items["defs"]["def"]["hashtag"].ToString()}");
sb.AppendLine($"`Definition:` {items["defs"]["def"]["text"].ToString()}");
sb.Append($"`Link:` <{await items["defs"]["def"]["uri"].ToString().ShortenUrl()}>");
await e.Channel.SendMessage(sb.ToString());
} catch {
}
catch
{
await e.Channel.SendMessage("💢 Failed finidng a definition for that tag.");
}
});
cgb.CreateCommand(Prefix + "quote")
.Description("Shows a random quote.")
.Do(async e => {
.Do(async e =>
{
await
e.Channel.SendMessage(
NadekoBot.Config.Quotes[new Random().Next(0, NadekoBot.Config.Quotes.Count)].ToString());

View File

@ -0,0 +1,325 @@
// Copyright (c) 2015 Ravi Bhavnani
// License: Code Project Open License
// http://www.codeproject.com/info/cpol10.aspx
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
namespace NadekoBot.Modules.Translator.Helpers
{
/// <summary>
/// Translates text using Google's online language tools.
/// </summary>
public class GoogleTranslator
{
#region Properties
/// <summary>
/// Gets the supported languages.
/// </summary>
public static IEnumerable<string> Languages {
get {
GoogleTranslator.EnsureInitialized();
return GoogleTranslator._languageModeMap.Keys.OrderBy(p => p);
}
}
/// <summary>
/// Gets the time taken to perform the translation.
/// </summary>
public TimeSpan TranslationTime {
get;
private set;
}
/// <summary>
/// Gets the url used to speak the translation.
/// </summary>
/// <value>The url used to speak the translation.</value>
public string TranslationSpeechUrl {
get;
private set;
}
/// <summary>
/// Gets the error.
/// </summary>
public Exception Error {
get;
private set;
}
#endregion
#region Public methods
/// <summary>
/// Translates the specified source text.
/// </summary>
/// <param name="sourceText">The source text.</param>
/// <param name="sourceLanguage">The source language.</param>
/// <param name="targetLanguage">The target language.</param>
/// <returns>The translation.</returns>
public string Translate
(string sourceText,
string sourceLanguage,
string targetLanguage)
{
// Initialize
this.Error = null;
this.TranslationSpeechUrl = null;
this.TranslationTime = TimeSpan.Zero;
DateTime tmStart = DateTime.Now;
string translation = string.Empty;
try
{
// Download translation
string url = string.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
GoogleTranslator.LanguageEnumToIdentifier(sourceLanguage),
GoogleTranslator.LanguageEnumToIdentifier(targetLanguage),
HttpUtility.UrlEncode(sourceText));
string outputFile = Path.GetTempFileName();
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.DownloadFile(url, outputFile);
}
// 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
int startQuote = text.IndexOf('\"');
if (startQuote != -1)
{
int endQuote = text.IndexOf('\"', startQuote + 1);
if (endQuote != -1)
{
translation = text.Substring(startQuote + 1, endQuote - startQuote - 1);
}
}
}
else {
// Translation of phrase
text = text.Substring(0, index);
text = text.Replace("],[", ",");
text = text.Replace("]", string.Empty);
text = text.Replace("[", string.Empty);
text = text.Replace("\",\"", "\"");
// Get translated phrases
string[] phrases = text.Split(new[] { '\"' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; (i < phrases.Count()); i += 2)
{
string translatedPhrase = phrases[i];
if (translatedPhrase.StartsWith(",,"))
{
i--;
continue;
}
translation += translatedPhrase + " ";
}
}
// Fix up translation
translation = translation.Trim();
translation = translation.Replace(" ?", "?");
translation = translation.Replace(" !", "!");
translation = translation.Replace(" ,", ",");
translation = translation.Replace(" .", ".");
translation = translation.Replace(" ;", ";");
// And translation speech URL
this.TranslationSpeechUrl = string.Format("https://translate.googleapis.com/translate_tts?ie=UTF-8&q={0}&tl={1}&total=1&idx=0&textlen={2}&client=gtx",
HttpUtility.UrlEncode(translation), GoogleTranslator.LanguageEnumToIdentifier(targetLanguage), translation.Length);
}
}
catch (Exception ex)
{
this.Error = ex;
}
// Return result
this.TranslationTime = DateTime.Now - tmStart;
return translation;
}
#endregion
#region Private methods
/// <summary>
/// Converts a language to its identifier.
/// </summary>
/// <param name="language">The language."</param>
/// <returns>The identifier or <see cref="string.Empty"/> if none.</returns>
private static string LanguageEnumToIdentifier
(string language)
{
string mode = string.Empty;
GoogleTranslator.EnsureInitialized();
GoogleTranslator._languageModeMap.TryGetValue(language, out mode);
return mode;
}
/// <summary>
/// Ensures the translator has been initialized.
/// </summary>
public static void EnsureInitialized()
{
if (GoogleTranslator._languageModeMap == null)
{
GoogleTranslator._languageModeMap = new Dictionary<string, string>();
GoogleTranslator._languageModeMap.Add("afrikaans", "af");
GoogleTranslator._languageModeMap.Add("albanian", "sq");
GoogleTranslator._languageModeMap.Add("arabic", "ar");
GoogleTranslator._languageModeMap.Add("armenian", "hy");
GoogleTranslator._languageModeMap.Add("azerbaijani", "az");
GoogleTranslator._languageModeMap.Add("basque", "eu");
GoogleTranslator._languageModeMap.Add("belarusian", "be");
GoogleTranslator._languageModeMap.Add("bengali", "bn");
GoogleTranslator._languageModeMap.Add("bulgarian", "bg");
GoogleTranslator._languageModeMap.Add("catalan", "ca");
GoogleTranslator._languageModeMap.Add("chinese", "zh-CN");
GoogleTranslator._languageModeMap.Add("croatian", "hr");
GoogleTranslator._languageModeMap.Add("czech", "cs");
GoogleTranslator._languageModeMap.Add("danish", "da");
GoogleTranslator._languageModeMap.Add("dutch", "nl");
GoogleTranslator._languageModeMap.Add("english", "en");
GoogleTranslator._languageModeMap.Add("esperanto", "eo");
GoogleTranslator._languageModeMap.Add("estonian", "et");
GoogleTranslator._languageModeMap.Add("filipino", "tl");
GoogleTranslator._languageModeMap.Add("finnish", "fi");
GoogleTranslator._languageModeMap.Add("french", "fr");
GoogleTranslator._languageModeMap.Add("galician", "gl");
GoogleTranslator._languageModeMap.Add("german", "de");
GoogleTranslator._languageModeMap.Add("georgian", "ka");
GoogleTranslator._languageModeMap.Add("greek", "el");
GoogleTranslator._languageModeMap.Add("haitian Creole", "ht");
GoogleTranslator._languageModeMap.Add("hebrew", "iw");
GoogleTranslator._languageModeMap.Add("hindi", "hi");
GoogleTranslator._languageModeMap.Add("hungarian", "hu");
GoogleTranslator._languageModeMap.Add("icelandic", "is");
GoogleTranslator._languageModeMap.Add("indonesian", "id");
GoogleTranslator._languageModeMap.Add("irish", "ga");
GoogleTranslator._languageModeMap.Add("italian", "it");
GoogleTranslator._languageModeMap.Add("japanese", "ja");
GoogleTranslator._languageModeMap.Add("korean", "ko");
GoogleTranslator._languageModeMap.Add("lao", "lo");
GoogleTranslator._languageModeMap.Add("latin", "la");
GoogleTranslator._languageModeMap.Add("latvian", "lv");
GoogleTranslator._languageModeMap.Add("lithuanian", "lt");
GoogleTranslator._languageModeMap.Add("macedonian", "mk");
GoogleTranslator._languageModeMap.Add("malay", "ms");
GoogleTranslator._languageModeMap.Add("maltese", "mt");
GoogleTranslator._languageModeMap.Add("norwegian", "no");
GoogleTranslator._languageModeMap.Add("persian", "fa");
GoogleTranslator._languageModeMap.Add("polish", "pl");
GoogleTranslator._languageModeMap.Add("portuguese", "pt");
GoogleTranslator._languageModeMap.Add("romanian", "ro");
GoogleTranslator._languageModeMap.Add("russian", "ru");
GoogleTranslator._languageModeMap.Add("serbian", "sr");
GoogleTranslator._languageModeMap.Add("slovak", "sk");
GoogleTranslator._languageModeMap.Add("slovenian", "sl");
GoogleTranslator._languageModeMap.Add("spanish", "es");
GoogleTranslator._languageModeMap.Add("swahili", "sw");
GoogleTranslator._languageModeMap.Add("swedish", "sv");
GoogleTranslator._languageModeMap.Add("tamil", "ta");
GoogleTranslator._languageModeMap.Add("telugu", "te");
GoogleTranslator._languageModeMap.Add("thai", "th");
GoogleTranslator._languageModeMap.Add("turkish", "tr");
GoogleTranslator._languageModeMap.Add("ukrainian", "uk");
GoogleTranslator._languageModeMap.Add("urdu", "ur");
GoogleTranslator._languageModeMap.Add("vietnamese", "vi");
GoogleTranslator._languageModeMap.Add("welsh", "cy");
GoogleTranslator._languageModeMap.Add("yiddish", "yi");
GoogleTranslator._languageModeMap.Add("af", "af");
GoogleTranslator._languageModeMap.Add("sq", "sq");
GoogleTranslator._languageModeMap.Add("ar", "ar");
GoogleTranslator._languageModeMap.Add("hy", "hy");
GoogleTranslator._languageModeMap.Add("az", "az");
GoogleTranslator._languageModeMap.Add("eu", "eu");
GoogleTranslator._languageModeMap.Add("be", "be");
GoogleTranslator._languageModeMap.Add("bn", "bn");
GoogleTranslator._languageModeMap.Add("bg", "bg");
GoogleTranslator._languageModeMap.Add("ca", "ca");
GoogleTranslator._languageModeMap.Add("zh-CN", "zh-CN");
GoogleTranslator._languageModeMap.Add("hr", "hr");
GoogleTranslator._languageModeMap.Add("cs", "cs");
GoogleTranslator._languageModeMap.Add("da", "da");
GoogleTranslator._languageModeMap.Add("nl", "nl");
GoogleTranslator._languageModeMap.Add("en", "en");
GoogleTranslator._languageModeMap.Add("eo", "eo");
GoogleTranslator._languageModeMap.Add("et", "et");
GoogleTranslator._languageModeMap.Add("tl", "tl");
GoogleTranslator._languageModeMap.Add("fi", "fi");
GoogleTranslator._languageModeMap.Add("fr", "fr");
GoogleTranslator._languageModeMap.Add("gl", "gl");
GoogleTranslator._languageModeMap.Add("de", "de");
GoogleTranslator._languageModeMap.Add("ka", "ka");
GoogleTranslator._languageModeMap.Add("el", "el");
GoogleTranslator._languageModeMap.Add("ht", "ht");
GoogleTranslator._languageModeMap.Add("iw", "iw");
GoogleTranslator._languageModeMap.Add("hi", "hi");
GoogleTranslator._languageModeMap.Add("hu", "hu");
GoogleTranslator._languageModeMap.Add("is", "is");
GoogleTranslator._languageModeMap.Add("id", "id");
GoogleTranslator._languageModeMap.Add("ga", "ga");
GoogleTranslator._languageModeMap.Add("it", "it");
GoogleTranslator._languageModeMap.Add("ja", "ja");
GoogleTranslator._languageModeMap.Add("ko", "ko");
GoogleTranslator._languageModeMap.Add("lo", "lo");
GoogleTranslator._languageModeMap.Add("la", "la");
GoogleTranslator._languageModeMap.Add("lv", "lv");
GoogleTranslator._languageModeMap.Add("lt", "lt");
GoogleTranslator._languageModeMap.Add("mk", "mk");
GoogleTranslator._languageModeMap.Add("ms", "ms");
GoogleTranslator._languageModeMap.Add("mt", "mt");
GoogleTranslator._languageModeMap.Add("no", "no");
GoogleTranslator._languageModeMap.Add("fa", "fa");
GoogleTranslator._languageModeMap.Add("pl", "pl");
GoogleTranslator._languageModeMap.Add("pt", "pt");
GoogleTranslator._languageModeMap.Add("ro", "ro");
GoogleTranslator._languageModeMap.Add("ru", "ru");
GoogleTranslator._languageModeMap.Add("sr", "sr");
GoogleTranslator._languageModeMap.Add("sk", "sk");
GoogleTranslator._languageModeMap.Add("sl", "sl");
GoogleTranslator._languageModeMap.Add("es", "es");
GoogleTranslator._languageModeMap.Add("sw", "sw");
GoogleTranslator._languageModeMap.Add("sv", "sv");
GoogleTranslator._languageModeMap.Add("ta", "ta");
GoogleTranslator._languageModeMap.Add("te", "te");
GoogleTranslator._languageModeMap.Add("th", "th");
GoogleTranslator._languageModeMap.Add("tr", "tr");
GoogleTranslator._languageModeMap.Add("uk", "uk");
GoogleTranslator._languageModeMap.Add("ur", "ur");
GoogleTranslator._languageModeMap.Add("vi", "vi");
GoogleTranslator._languageModeMap.Add("cy", "cy");
GoogleTranslator._languageModeMap.Add("yi", "yi");
}
}
#endregion
#region Fields
/// <summary>
/// The language to translation mode map.
/// </summary>
public static Dictionary<string, string> _languageModeMap;
#endregion
}
}

View File

@ -0,0 +1,40 @@
using Discord.Commands;
using NadekoBot.Commands;
using NadekoBot.Modules.Translator.Helpers;
using System;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Translator
{
class TranslateCommand : DiscordCommand
{
public TranslateCommand(DiscordModule module) : base(module) { }
internal override void Init(CommandGroupBuilder cgb)
{
cgb.CreateCommand(Module.Prefix + "trans")
.Description("Translates from>to text. From the given language to the destiation language.")
.Parameter("langs", ParameterType.Required)
.Parameter("text", ParameterType.Unparsed)
.Do(TranslateFunc());
}
private GoogleTranslator t = new GoogleTranslator();
private Func<CommandEventArgs, Task> TranslateFunc() => async e =>
{
try
{
await e.Channel.SendIsTyping();
string from = e.GetArg("langs").ToLowerInvariant().Split('>')[0];
string to = e.GetArg("langs").ToLowerInvariant().Split('>')[1];
string translation = t.Translate(e.GetArg("text"), from, to);
await e.Channel.SendMessage(translation);
}
catch
{
await e.Channel.SendMessage("Bad input format, or sth went wrong...");
}
};
}
}

View File

@ -0,0 +1,26 @@
using Discord.Modules;
using NadekoBot.Extensions;
namespace NadekoBot.Modules.Translator
{
internal class TranslatorModule : DiscordModule
{
public TranslatorModule()
{
commands.Add(new TranslateCommand(this));
commands.Add(new ValidLanguagesCommand(this));
}
public override string Prefix { get; } = NadekoBot.Config.CommandPrefixes.Searches;
public override void Install(ModuleManager manager)
{
manager.CreateCommands("", cgb =>
{
cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance);
commands.ForEach(cmd => cmd.Init(cgb));
});
}
}
}

View File

@ -0,0 +1,50 @@
using Discord.Commands;
using NadekoBot.Commands;
using NadekoBot.Modules.Translator.Helpers;
using System;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Translator
{
class ValidLanguagesCommand : DiscordCommand
{
public ValidLanguagesCommand(DiscordModule module) : base(module) { }
internal override void Init(CommandGroupBuilder cgb)
{
cgb.CreateCommand(Module.Prefix + "translangs")
.Description("List the valid languages for translation.")
.Parameter("search", ParameterType.Optional)
.Do(ListLanguagesFunc());
}
private Func<CommandEventArgs, Task> ListLanguagesFunc() => async e =>
{
try
{
GoogleTranslator.EnsureInitialized();
string s = e.GetArg("search");
string ret = "";
foreach (string key in GoogleTranslator._languageModeMap.Keys)
{
if (!s.Equals(""))
{
if (key.ToLower().Contains(s))
{
ret += " " + key + ";";
}
}
else
{
ret += " " + key + ";";
}
}
await e.Channel.SendMessage(ret);
}
catch
{
await e.Channel.SendMessage("Bad input format, or sth went wrong...");
}
};
}
}

View File

@ -118,6 +118,8 @@
<Compile Include="Classes\ClashOfClans\ClashOfClans.cs" />
<Compile Include="Classes\DBHandler.cs" />
<Compile Include="Classes\FlowersHandler.cs" />
<Compile Include="Classes\IMDB\ImdbMovie.cs" />
<Compile Include="Classes\IMDB\ImdbScraper.cs" />
<Compile Include="Classes\IncidentsHandler.cs" />
<Compile Include="Classes\JSONModels\AnimeResult.cs" />
<Compile Include="Classes\JSONModels\Configuration.cs" />
@ -188,6 +190,10 @@
<Compile Include="Commands\RatelimitCommand.cs" />
<Compile Include="Modules\PokemonModule.cs" />
<Compile Include="Modules\Searches.cs" />
<Compile Include="Modules\Translator\Helpers\GoogleTranslator.cs" />
<Compile Include="Modules\Translator\TranslateCommand.cs" />
<Compile Include="Modules\Translator\TranslatorModule.cs" />
<Compile Include="Modules\Translator\ValidLanguagesCommand.cs" />
<Compile Include="Modules\Trello.cs" />
<Compile Include="NadekoBot.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />