work on searches

This commit is contained in:
Master Kwoth 2016-03-01 16:22:41 +01:00
parent 1f2b9cd3d3
commit 470de71901
6 changed files with 149 additions and 159 deletions

View File

@ -310,7 +310,7 @@ namespace NadekoBot.Classes.Music {
private static async Task<string> HandleStreamContainers(string query) { private static async Task<string> HandleStreamContainers(string query) {
string file = null; string file = null;
try { try {
file = await SearchHelper.GetResponseAsync(query); file = await SearchHelper.GetResponseStringAsync(query);
} }
catch { catch {
return query; return query;

View File

@ -15,7 +15,7 @@ namespace NadekoBot.Classes.Music {
if (string.IsNullOrWhiteSpace(NadekoBot.Creds.SoundCloudClientID)) if (string.IsNullOrWhiteSpace(NadekoBot.Creds.SoundCloudClientID))
throw new ArgumentNullException(nameof(NadekoBot.Creds.SoundCloudClientID)); throw new ArgumentNullException(nameof(NadekoBot.Creds.SoundCloudClientID));
var response = await SearchHelper.GetResponseAsync($"http://api.soundcloud.com/resolve?url={url}&client_id={NadekoBot.Creds.SoundCloudClientID}"); var response = await SearchHelper.GetResponseStringAsync($"http://api.soundcloud.com/resolve?url={url}&client_id={NadekoBot.Creds.SoundCloudClientID}");
var responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject<SoundCloudVideo>(response); var responseObj = Newtonsoft.Json.JsonConvert.DeserializeObject<SoundCloudVideo>(response);
if (responseObj?.Kind != "track") if (responseObj?.Kind != "track")

View File

@ -4,6 +4,7 @@ using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Net; using System.Net;
using System.Net.Http; using System.Net.Http;
using System.Security.Authentication; using System.Security.Authentication;
@ -13,84 +14,91 @@ using NadekoBot.Classes.JSONModels;
namespace NadekoBot.Classes { namespace NadekoBot.Classes {
public enum RequestHttpMethod { public enum RequestHttpMethod {
Get, Post Get,
Post
} }
public static class SearchHelper { public static class SearchHelper {
private static DateTime lastRefreshed = DateTime.MinValue;
private static string token = "";
public static async Task<Stream> GetResponseStream(string query, RequestHttpMethod method = RequestHttpMethod.Get) { public static async Task<Stream> GetResponseStreamAsync(string url,
if (string.IsNullOrWhiteSpace(query)) IEnumerable<KeyValuePair<string, string>> headers = null, RequestHttpMethod method = RequestHttpMethod.Get) {
throw new ArgumentNullException(nameof(query)); if (string.IsNullOrWhiteSpace(url))
var wr = (HttpWebRequest)WebRequest.Create(query); throw new ArgumentNullException(nameof(url));
using (var response = await wr.GetResponseAsync()) {
var stream = response?.GetResponseStream();
if (stream == null)
throw new InvalidOperationException("Did not receive a response.");
return stream;
}
}
public static async Task<string> GetResponseAsync(string url, RequestHttpMethod method = RequestHttpMethod.Get, params Tuple<string,string>[] headers) {
using (var httpClient = new HttpClient()) { using (var httpClient = new HttpClient()) {
switch (method) {
case RequestHttpMethod.Get:
if (headers != null) { if (headers != null) {
foreach (var header in headers) { foreach (var header in headers) {
httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
} }
} }
return await httpClient.GetStringAsync(url); return await httpClient.GetStreamAsync(url);
case RequestHttpMethod.Post:
FormUrlEncodedContent formContent = null;
if (headers != null) {
formContent = new FormUrlEncodedContent(headers);
}
var message = await httpClient.PostAsync(url, formContent);
return await message.Content.ReadAsStreamAsync();
default:
throw new NotImplementedException("That type of request is unsupported.");
}
}
}
public static async Task<string> GetResponseStringAsync(string url,
IEnumerable<KeyValuePair<string, string>> headers = null,
RequestHttpMethod method = RequestHttpMethod.Get) {
using (var streamReader = new StreamReader(await GetResponseStreamAsync(url, headers, method))) {
return await streamReader.ReadToEndAsync();
} }
} }
private static string token = "";
public static async Task<AnimeResult> GetAnimeQueryResultLink(string query) { public static async Task<AnimeResult> GetAnimeQueryResultLink(string query) {
if (string.IsNullOrWhiteSpace(query)) if (string.IsNullOrWhiteSpace(query))
throw new ArgumentNullException(nameof(query)); throw new ArgumentNullException(nameof(query));
RefreshAnilistToken(); await RefreshAnilistToken();
var link = "http://anilist.co/api/anime/search/" + Uri.EscapeUriString(query); var link = "http://anilist.co/api/anime/search/" + Uri.EscapeUriString(query);
Dictionary<string, string> headers = new {"access_token" = token}; var headers = new Dictionary<string, string> { { "access_token", token } };
var smallContent = await GetResponseAsync(link, headers); var smallContent = await GetResponseStringAsync(link, headers);
var smallObj = JArray.Parse(await httpClient.GetStringAsync(link))[0]; var smallObj = JArray.Parse(smallContent)[0];
var content = await httpClient.GetStringAsync("anime/" + smallObj["id"]); var content = await GetResponseStringAsync("http://anilist.co/api/anime/" + smallObj["id"], headers);
return await Task.Run(() => JsonConvert.DeserializeObject<AnimeResult>(content)); return await Task.Run(() => JsonConvert.DeserializeObject<AnimeResult>(content));
} }
//todo kick out RestSharp and make it truly async
public static async Task<MangaResult> GetMangaQueryResultLink(string query) { public static async Task<MangaResult> GetMangaQueryResultLink(string query) {
try { if (string.IsNullOrWhiteSpace(query))
RefreshAnilistToken(); throw new ArgumentNullException(nameof(query));
var cl = new RestSharp.RestClient("http://anilist.co/api"); await RefreshAnilistToken();
var rq = new RestSharp.RestRequest("/auth/access_token", RestSharp.Method.POST);
rq = new RestSharp.RestRequest("/manga/search/" + Uri.EscapeUriString(query));
rq.AddParameter("access_token", token);
var smallObj = JArray.Parse(cl.Execute(rq).Content)[0]; var link = "http://anilist.co/api/manga/search/" + Uri.EscapeUriString(query);
rq = new RestSharp.RestRequest("manga/" + smallObj["id"]); var headers = new Dictionary<string, string> { { "access_token", token } };
rq.AddParameter("access_token", token); var smallContent = await GetResponseStringAsync(link, headers);
return await Task.Run(() => JsonConvert.DeserializeObject<MangaResult>(cl.Execute(rq).Content)); var smallObj = JArray.Parse(smallContent)[0];
} catch (Exception ex) { var content = await GetResponseStringAsync("http://anilist.co/api/manga/" + smallObj["id"], headers);
Console.WriteLine(ex.ToString());
return null; return await Task.Run(() => JsonConvert.DeserializeObject<MangaResult>(content));
}
} }
private static void RefreshAnilistToken() { private static async Task RefreshAnilistToken() {
try { var headers = new Dictionary<string, string> {
var cl = new RestSharp.RestClient("http://anilist.co/api"); {"grant_type", "client_credentials"},
var rq = new RestSharp.RestRequest("/auth/access_token", RestSharp.Method.POST); {"client_id", "kwoth-w0ki9"},
rq.AddParameter("grant_type", "client_credentials"); {"client_secret", "Qd6j4FIAi1ZK6Pc7N7V4Z"},
rq.AddParameter("client_id", "kwoth-w0ki9"); };
rq.AddParameter("client_secret", "Qd6j4FIAi1ZK6Pc7N7V4Z"); var content =
var exec = cl.Execute(rq); await GetResponseStringAsync("http://anilist.co/api/auth/access_token", headers, RequestHttpMethod.Post);
token = JObject.Parse(exec.Content)["access_token"].ToString(); token = JObject.Parse(content)["access_token"].ToString();
} catch (Exception ex) {
Console.WriteLine($"Failed refreshing anilist token:\n {ex}");
}
} }
public static async Task<bool> ValidateQuery(Discord.Channel ch, string query) { public static async Task<bool> ValidateQuery(Discord.Channel ch, string query) {
@ -102,128 +110,107 @@ namespace NadekoBot.Classes {
public static async Task<string> FindYoutubeUrlByKeywords(string keywords) { public static async Task<string> FindYoutubeUrlByKeywords(string keywords) {
if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey)) if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey))
throw new InvalidCredentialException("Google API Key is missing."); throw new InvalidCredentialException("Google API Key is missing.");
if (string.IsNullOrWhiteSpace(keywords))
throw new ArgumentNullException(nameof(keywords), "Query not specified.");
if (keywords.Length > 150) if (keywords.Length > 150)
throw new ArgumentException("Query is too long."); throw new ArgumentException("Query is too long.");
//maybe it is already a youtube url, in which case we will just extract the id and prepend it with youtube.com?v= //maybe it is already a youtube url, in which case we will just extract the id and prepend it with youtube.com?v=
var match = new Regex("(?:youtu\\.be\\/|v=)(?<id>[\\da-zA-Z\\-_]*)").Match(keywords); var match = new Regex("(?:youtu\\.be\\/|v=)(?<id>[\\da-zA-Z\\-_]*)").Match(keywords);
if (match.Length > 1) { if (match.Length > 1) {
return $"http://www.youtube.com?v={ match.Groups["id"].Value }"; return $"http://www.youtube.com?v={match.Groups["id"].Value}";
} }
var wr = var response =
WebRequest.Create( await
$"https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q={Uri.EscapeDataString(keywords)}&key={NadekoBot.Creds.GoogleAPIKey}"); GetResponseStringAsync($"https://www.googleapis.com/youtube/v3/search?" +
try { $"part=snippet&maxResults=1" +
using (var response = await wr.GetResponseAsync()) $"&q={Uri.EscapeDataString(keywords)}" +
using (var stream = response.GetResponseStream()) { $"&key={NadekoBot.Creds.GoogleAPIKey}");
try { dynamic obj = JObject.Parse(response);
using (var sr = new StreamReader(stream)) {
dynamic obj = JObject.Parse(await sr.ReadToEndAsync());
return "http://www.youtube.com/watch?v=" + obj.items[0].id.videoId.ToString(); return "http://www.youtube.com/watch?v=" + obj.items[0].id.videoId.ToString();
} }
} catch (Exception ex) {
ex.Message
}
}
} catch (Exception ex) {
Console.WriteLine($"Error in findyoutubeurl: {ex.Message}");
return string.Empty;
}
}
public static async Task<string> GetPlaylistIdByKeyword(string v) { public static async Task<string> GetPlaylistIdByKeyword(string query) {
if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey)) { if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey))
Console.WriteLine("ERROR: No google api key found. Playing `Never gonna give you up`."); throw new ArgumentNullException(nameof(query));
return string.Empty;
}
try {
WebRequest wr = WebRequest.Create($"https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=1&q={Uri.EscapeDataString(v)}&type=playlist&key={NadekoBot.Creds.GoogleAPIKey}");
var sr = new StreamReader((await wr.GetResponseAsync()).GetResponseStream()); var link = $"https://www.googleapis.com/youtube/v3/search?part=snippet" +
$"&maxResults=1" +
$"&q={Uri.EscapeDataString(query)}" +
$"&type=playlist" +
$"&key={NadekoBot.Creds.GoogleAPIKey}";
var response = await GetResponseStringAsync(link);
dynamic obj = JObject.Parse(response);
dynamic obj = JObject.Parse(await sr.ReadToEndAsync());
return obj.items[0].id.playlistId.ToString(); return obj.items[0].id.playlistId.ToString();
} catch (Exception ex) {
Console.WriteLine($"Error in GetPlaylistId: {ex.Message}");
return string.Empty;
}
} }
public static async Task<List<string>> GetVideoIDs(string v) { public static async Task<IEnumerable<string>> GetVideoIDs(string playlist) {
List<string> toReturn = new List<string>();
if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey)) { if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey)) {
Console.WriteLine("ERROR: No google api key found. Playing `Never gonna give you up`."); throw new ArgumentNullException(nameof(playlist));
return toReturn;
} }
try { var link =
WebRequest wr = WebRequest.Create($"https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults={30}&playlistId={v}&key={ NadekoBot.Creds.GoogleAPIKey }"); $"https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails" +
var response = await wr.GetResponseAsync(); $"&maxResults={30}" +
if (response == null) return toReturn; $"&playlistId={playlist}" +
var responseStream = response.GetResponseStream(); $"&key={NadekoBot.Creds.GoogleAPIKey}";
if (responseStream == null) return toReturn;
var sr = new StreamReader(responseStream);
dynamic obj = JObject.Parse(await sr.ReadToEndAsync()); var response = await GetResponseStringAsync(link);
var obj = await Task.Run(() => JObject.Parse(response));
foreach (var item in obj.items) { return obj["items"].Select(item => "http://www.youtube.com/watch?v=" + item["contentDetails"]["videoId"]);
toReturn.Add("http://www.youtube.com/watch?v=" + item.contentDetails.videoId);
}
return toReturn;
} catch (Exception ex) {
Console.WriteLine($"Error in GetPlaylistId: {ex.Message}");
return new List<string>();
}
} }
public static async Task<string> GetDanbooruImageLink(string tag) { public static async Task<string> GetDanbooruImageLink(string tag) {
try {
var rng = new Random(); var rng = new Random();
if (tag == "loli") //loli doesn't work for some reason atm if (tag == "loli") //loli doesn't work for some reason atm
tag = "flat_chest"; tag = "flat_chest";
var webpage = await GetResponseAsync($"http://danbooru.donmai.us/posts?page={ rng.Next(0, 15) }&tags={ tag.Replace(" ", "_") }"); var link = $"http://danbooru.donmai.us/posts?" +
$"page={rng.Next(0, 15)}";
if (!string.IsNullOrWhiteSpace(tag))
link += $"&tags={tag.Replace(" ", "_")}";
var webpage = await GetResponseStringAsync(link);
var matches = Regex.Matches(webpage, "data-large-file-url=\"(?<id>.*?)\""); var matches = Regex.Matches(webpage, "data-large-file-url=\"(?<id>.*?)\"");
return await $"http://danbooru.donmai.us{ matches[rng.Next(0, matches.Count)].Groups["id"].Value }".ShortenUrl(); return $"http://danbooru.donmai.us" +
} catch { $"{matches[rng.Next(0, matches.Count)].Groups["id"].Value}";
return null;
}
} }
public static async Task<string> GetGelbooruImageLink(string tag) { public static async Task<string> GetGelbooruImageLink(string tag) {
try {
var rng = new Random(); var rng = new Random();
var url = $"http://gelbooru.com/index.php?page=post&s=list&pid={ rng.Next(0, 10) * 42 }&tags={ tag.Replace(" ", "_") }"; var url =
var webpage = await GetResponseAsync(url); // first extract the post id and go to that posts page $"http://gelbooru.com/index.php?page=post&s=list&pid={rng.Next(0, 10) * 42}&tags={tag.Replace(" ", "_")}";
var matches = Regex.Matches(webpage, "span id=\"s(?<id>\\d*)\""); var webpage = await GetResponseStringAsync(url); // first extract the post id and go to that posts page
var postLink = $"http://gelbooru.com/index.php?page=post&s=view&id={ matches[rng.Next(0, matches.Count)].Groups["id"].Value }"; //src="htp://gelbooru.com/thumbnails/1b/5e/thumbnail_1b5e1dae36237ef0cd030575b93b5bd2.jpg?3064956"
webpage = await GetResponseAsync(postLink); var matches = Regex.Matches(webpage, @"src=\""http:\/\/gelbooru\.com\/thumbnails\/" +
//now extract the image from post page @"(?<folder>.*\/.*?)\/thumbnail_(?<id>.*?)\""");
var match = Regex.Match(webpage, "\"(?<url>http://simg4.gelbooru.com//images.*?)\""); if (matches.Count == 0)
return match.Groups["url"].Value; throw new FileNotFoundException();
} catch { var match = matches[rng.Next(0, matches.Count)];
return null; //http://simg4.gelbooru.com//images/58/20/58209047098e86c2f96c323fb85b8691.jpg?3076643
} return $"http://simg4.gelbooru.com//images/" +
$"{match.Groups["folder"]}/{match.Groups["id"]}";
} }
internal static async Task<string> GetE621ImageLink(string tags) { internal static async Task<string> GetE621ImageLink(string tags) {
try {
var rng = new Random(); var rng = new Random();
var url = $"https://e621.net/post/index/{rng.Next(0, 5)}/{Uri.EscapeUriString(tags)}"; var url = $"https://e621.net/post/index/{rng.Next(0, 5)}/{Uri.EscapeUriString(tags)}";
var webpage = await GetResponseAsync(url); // first extract the post id and go to that posts page var webpage = await GetResponseStringAsync(url); // first extract the post id and go to that posts page
var matches = Regex.Matches(webpage, "\"file_url\":\"(?<url>.*?)\""); var matches = Regex.Matches(webpage, "\"file_url\":\"(?<url>.*?)\"");
return matches[rng.Next(0, matches.Count)].Groups["url"].Value; return matches[rng.Next(0, matches.Count)].Groups["url"].Value;
} catch {
return null;
}
} }
public static async Task<string> ShortenUrl(string url) { public static async Task<string> ShortenUrl(string url) {
if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey)) return url; if (string.IsNullOrWhiteSpace(NadekoBot.Creds.GoogleAPIKey)) return url;
try { try {
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + NadekoBot.Creds.GoogleAPIKey); var httpWebRequest =
(HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" +
NadekoBot.Creds.GoogleAPIKey);
httpWebRequest.ContentType = "application/json"; httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST"; httpWebRequest.Method = "POST";
@ -240,7 +227,10 @@ namespace NadekoBot.Classes {
var responseText = await streamReader.ReadToEndAsync(); var responseText = await streamReader.ReadToEndAsync();
return Regex.Match(responseText, @"""id"": ?""(?<id>.+)""").Groups["id"].Value; return Regex.Match(responseText, @"""id"": ?""(?<id>.+)""").Groups["id"].Value;
} }
} catch (Exception ex) { Console.WriteLine(ex.ToString()); return url; } } catch (Exception ex) {
Console.WriteLine(ex.ToString());
return url;
}
} }
} }
} }

View File

@ -76,7 +76,7 @@ namespace NadekoBot.Commands {
await e.Channel.SendFile("champ.png", champ.ImageStream); await e.Channel.SendFile("champ.png", champ.ImageStream);
return; return;
} }
var allData = JArray.Parse(await Classes.SearchHelper.GetResponseAsync($"http://api.champion.gg/champion/{name}?api_key={NadekoBot.Creds.LOLAPIKey}")); var allData = JArray.Parse(await Classes.SearchHelper.GetResponseStringAsync($"http://api.champion.gg/champion/{name}?api_key={NadekoBot.Creds.LOLAPIKey}"));
JToken data = null; JToken data = null;
if (role != null) { if (role != null) {
for (int i = 0; i < allData.Count; i++) { for (int i = 0; i < allData.Count; i++) {
@ -112,7 +112,7 @@ namespace NadekoBot.Commands {
if (roles[i] == role) if (roles[i] == role)
roles[i] = ">" + roles[i] + "<"; roles[i] = ">" + roles[i] + "<";
} }
var general = JArray.Parse(await Classes.SearchHelper.GetResponseAsync($"http://api.champion.gg/stats/champs/{name}?api_key={NadekoBot.Creds.LOLAPIKey}")) var general = JArray.Parse(await Classes.SearchHelper.GetResponseStringAsync($"http://api.champion.gg/stats/champs/{name}?api_key={NadekoBot.Creds.LOLAPIKey}"))
.Where(jt => jt["role"].ToString() == role) .Where(jt => jt["role"].ToString() == role)
.FirstOrDefault()?["general"]; .FirstOrDefault()?["general"];
if (general == null) { if (general == null) {
@ -251,7 +251,7 @@ Assists: {general["assists"]} Ban: {general["banRate"]}%
var data = JObject.Parse( var data = JObject.Parse(
await Classes await Classes
.SearchHelper .SearchHelper
.GetResponseAsync($"http://api.champion.gg/stats/champs/mostBanned?api_key={NadekoBot.Creds.LOLAPIKey}&page=1&limit={showCount}"))["data"] as JArray; .GetResponseStringAsync($"http://api.champion.gg/stats/champs/mostBanned?api_key={NadekoBot.Creds.LOLAPIKey}&page=1&limit={showCount}"))["data"] as JArray;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.AppendLine($"**Showing {showCount} top banned champions.**"); sb.AppendLine($"**Showing {showCount} top banned champions.**");

View File

@ -65,7 +65,7 @@ namespace NadekoBot.Modules {
.Description("Real adult content.") .Description("Real adult content.")
.Do(async e => { .Do(async e => {
try { try {
var obj = JArray.Parse(await SearchHelper.GetResponseAsync($"http://api.oboobs.ru/boobs/{_r.Next(0, 9304)}"))[0]; var obj = JArray.Parse(await SearchHelper.GetResponseStringAsync($"http://api.oboobs.ru/boobs/{_r.Next(0, 9304)}"))[0];
await e.Channel.SendMessage($"http://media.oboobs.ru/{ obj["preview"].ToString() }"); await e.Channel.SendMessage($"http://media.oboobs.ru/{ obj["preview"].ToString() }");
} catch (Exception ex) { } catch (Exception ex) {
await e.Channel.SendMessage($"💢 {ex.Message}"); await e.Channel.SendMessage($"💢 {ex.Message}");

View File

@ -74,7 +74,7 @@ namespace NadekoBot.Modules {
.Do(async e => { .Do(async e => {
try { try {
await e.Channel.SendMessage(JObject.Parse( await e.Channel.SendMessage(JObject.Parse(
await SearchHelper.GetResponseAsync("http://www.random.cat/meow"))["file"].ToString()); await SearchHelper.GetResponseStringAsync("http://www.random.cat/meow"))["file"].ToString());
} }
catch {} catch {}
}); });
@ -87,7 +87,7 @@ namespace NadekoBot.Modules {
return; 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 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.GetResponseAsync(reqString)); var obj = JObject.Parse(await SearchHelper.GetResponseStringAsync(reqString));
await e.Channel.SendMessage(obj["items"][0]["link"].ToString()); await e.Channel.SendMessage(obj["items"][0]["link"].ToString());
} catch (Exception ex) { } catch (Exception ex) {
await e.Channel.SendMessage($"💢 {ex.Message}"); await e.Channel.SendMessage($"💢 {ex.Message}");
@ -102,7 +102,7 @@ namespace NadekoBot.Modules {
return; 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 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.GetResponseAsync(reqString)); var obj = JObject.Parse(await SearchHelper.GetResponseStringAsync(reqString));
await e.Channel.SendMessage(obj["items"][0]["link"].ToString()); await e.Channel.SendMessage(obj["items"][0]["link"].ToString());
} catch (Exception ex) { } catch (Exception ex) {
await e.Channel.SendMessage($"💢 {ex.Message}"); await e.Channel.SendMessage($"💢 {ex.Message}");
@ -128,7 +128,7 @@ namespace NadekoBot.Modules {
} }
await e.Channel.SendIsTyping(); await e.Channel.SendIsTyping();
var headers = new WebHeaderCollection {{"X-Mashape-Key", NadekoBot.Creds.MashapeKey}}; var headers = new WebHeaderCollection {{"X-Mashape-Key", NadekoBot.Creds.MashapeKey}};
var res = await SearchHelper.GetResponseAsync($"https://omgvamp-hearthstone-v1.p.mashape.com/cards/search/{Uri.EscapeUriString(arg)}", headers); 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 items = JArray.Parse(res);
List<System.Drawing.Image> images = new List<System.Drawing.Image>(); List<System.Drawing.Image> images = new List<System.Drawing.Image>();
@ -142,7 +142,7 @@ namespace NadekoBot.Modules {
if (!item.HasValues || item["img"] == null) if (!item.HasValues || item["img"] == null)
continue; continue;
cnt++; cnt++;
images.Add(System.Drawing.Bitmap.FromStream(await SearchHelper.GetResponseStream(item["img"].ToString()))); images.Add(System.Drawing.Bitmap.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.SendMessage("⚠ Found over 4 images. Showing random 4.");
@ -189,7 +189,7 @@ namespace NadekoBot.Modules {
await e.Channel.SendIsTyping(); await e.Channel.SendIsTyping();
var headers = new WebHeaderCollection(); var headers = new WebHeaderCollection();
headers.Add("X-Mashape-Key", NadekoBot.Creds.MashapeKey); headers.Add("X-Mashape-Key", NadekoBot.Creds.MashapeKey);
var res = await SearchHelper.GetResponseAsync($"https://mashape-community-urban-dictionary.p.mashape.com/define?term={Uri.EscapeUriString(arg)}", headers); 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 items = JObject.Parse(res);
var sb = new System.Text.StringBuilder(); var sb = new System.Text.StringBuilder();
@ -214,7 +214,7 @@ namespace NadekoBot.Modules {
await e.Channel.SendIsTyping(); await e.Channel.SendIsTyping();
var headers = new WebHeaderCollection(); var headers = new WebHeaderCollection();
headers.Add("X-Mashape-Key", NadekoBot.Creds.MashapeKey); headers.Add("X-Mashape-Key", NadekoBot.Creds.MashapeKey);
var res = await SearchHelper.GetResponseAsync($"https://tagdef.p.mashape.com/one.{Uri.EscapeUriString(arg)}.json", headers); var res = await SearchHelper.GetResponseStringAsync($"https://tagdef.p.mashape.com/one.{Uri.EscapeUriString(arg)}.json", headers);
try { try {
var items = JObject.Parse(res); var items = JObject.Parse(res);
var sb = new System.Text.StringBuilder(); var sb = new System.Text.StringBuilder();