Renamed to google service and using urlshortner api lib too. .roles are ordered by position now.
This commit is contained in:
parent
09ff6aefb7
commit
5e4628ec10
@ -26,7 +26,7 @@ $@"`Title:` {WebUtility.HtmlDecode(Title)} {(string.IsNullOrEmpty(OriginalTitle)
|
|||||||
`Genre:` {GenresAsString}
|
`Genre:` {GenresAsString}
|
||||||
`Link:` <{ImdbURL}>
|
`Link:` <{ImdbURL}>
|
||||||
`Plot:` {System.Net.WebUtility.HtmlDecode(Plot.TrimTo(500))}
|
`Plot:` {System.Net.WebUtility.HtmlDecode(Plot.TrimTo(500))}
|
||||||
`img:` " + Poster.ShortenUrl().Result;
|
`img:` " + Poster;
|
||||||
public string GenresAsString =>
|
public string GenresAsString =>
|
||||||
string.Join(", ", Genres);
|
string.Join(", ", Genres);
|
||||||
}
|
}
|
||||||
|
@ -20,11 +20,11 @@ namespace NadekoBot.Modules.Searches
|
|||||||
[Module("~", AppendSpace = false)]
|
[Module("~", AppendSpace = false)]
|
||||||
public partial class Searches : DiscordModule
|
public partial class Searches : DiscordModule
|
||||||
{
|
{
|
||||||
private IYoutubeService _yt { get; }
|
private IGoogleApiService _google { get; }
|
||||||
|
|
||||||
public Searches(ILocalization loc, CommandService cmds, IBotConfiguration config, DiscordSocketClient client, IYoutubeService youtube) : base(loc, cmds, config, client)
|
public Searches(ILocalization loc, CommandService cmds, IBotConfiguration config, DiscordSocketClient client, IGoogleApiService youtube) : base(loc, cmds, config, client)
|
||||||
{
|
{
|
||||||
_yt = youtube;
|
_google = youtube;
|
||||||
}
|
}
|
||||||
|
|
||||||
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
|
||||||
@ -54,7 +54,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】
|
|||||||
{
|
{
|
||||||
var channel = imsg.Channel as ITextChannel;
|
var channel = imsg.Channel as ITextChannel;
|
||||||
if (!(await ValidateQuery(imsg.Channel as ITextChannel, query).ConfigureAwait(false))) return;
|
if (!(await ValidateQuery(imsg.Channel as ITextChannel, query).ConfigureAwait(false))) return;
|
||||||
var result = (await _yt.FindVideosByKeywordsAsync(query, 1)).FirstOrDefault();
|
var result = (await _google.FindVideosByKeywordsAsync(query, 1)).FirstOrDefault();
|
||||||
if (string.IsNullOrWhiteSpace(result))
|
if (string.IsNullOrWhiteSpace(result))
|
||||||
{
|
{
|
||||||
await channel.SendMessageAsync("No results found for that query.");
|
await channel.SendMessageAsync("No results found for that query.");
|
||||||
@ -183,7 +183,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】
|
|||||||
if (string.IsNullOrWhiteSpace(ffs))
|
if (string.IsNullOrWhiteSpace(ffs))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
await channel.SendMessageAsync(await $"<http://lmgtfy.com/?q={ Uri.EscapeUriString(ffs) }>".ShortenUrl())
|
await channel.SendMessageAsync(await _google.ShortenUrl($"<http://lmgtfy.com/?q={ Uri.EscapeUriString(ffs) }>"))
|
||||||
.ConfigureAwait(false);
|
.ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,7 +270,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】
|
|||||||
var sb = new System.Text.StringBuilder();
|
var sb = new System.Text.StringBuilder();
|
||||||
sb.AppendLine($"`Term:` {items["list"][0]["word"].ToString()}");
|
sb.AppendLine($"`Term:` {items["list"][0]["word"].ToString()}");
|
||||||
sb.AppendLine($"`Definition:` {items["list"][0]["definition"].ToString()}");
|
sb.AppendLine($"`Definition:` {items["list"][0]["definition"].ToString()}");
|
||||||
sb.Append($"`Link:` <{await items["list"][0]["permalink"].ToString().ShortenUrl().ConfigureAwait(false)}>");
|
sb.Append($"`Link:` <{await _google.ShortenUrl(items["list"][0]["permalink"].ToString()).ConfigureAwait(false)}>");
|
||||||
await channel.SendMessageAsync(sb.ToString());
|
await channel.SendMessageAsync(sb.ToString());
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@ -306,7 +306,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】
|
|||||||
var items = JObject.Parse(res);
|
var items = JObject.Parse(res);
|
||||||
var str = $@"`Hashtag:` {items["defs"]["def"]["hashtag"].ToString()}
|
var str = $@"`Hashtag:` {items["defs"]["def"]["hashtag"].ToString()}
|
||||||
`Definition:` {items["defs"]["def"]["text"].ToString()}
|
`Definition:` {items["defs"]["def"]["text"].ToString()}
|
||||||
`Link:` <{await items["defs"]["def"]["uri"].ToString().ShortenUrl().ConfigureAwait(false)}>";
|
`Link:` <{await _google.ShortenUrl(items["defs"]["def"]["uri"].ToString()).ConfigureAwait(false)}>";
|
||||||
await channel.SendMessageAsync(str);
|
await channel.SendMessageAsync(str);
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@ -463,7 +463,7 @@ $@"🌍 **Weather for** 【{obj["target"]}】
|
|||||||
await channel.SendMessageAsync("Invalid user specified.").ConfigureAwait(false);
|
await channel.SendMessageAsync("Invalid user specified.").ConfigureAwait(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await channel.SendMessageAsync(await usr.AvatarUrl.ShortenUrl()).ConfigureAwait(false);
|
await channel.SendMessageAsync(await _google.ShortenUrl(usr.AvatarUrl).ConfigureAwait(false)).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<string> GetSafebooruImageLink(string tag)
|
public static async Task<string> GetSafebooruImageLink(string tag)
|
||||||
|
@ -121,11 +121,11 @@ namespace NadekoBot.Modules.Utility
|
|||||||
var guild = (msg.Channel as ITextChannel).Guild;
|
var guild = (msg.Channel as ITextChannel).Guild;
|
||||||
if (target != null)
|
if (target != null)
|
||||||
{
|
{
|
||||||
await msg.Reply($"`List of roles for **{target.Username}**:` \n• " + string.Join("\n• ", target.Roles.Except(new[] { guild.EveryoneRole })));
|
await msg.Reply($"`List of roles for **{target.Username}**:` \n• " + string.Join("\n• ", target.Roles.Except(new[] { guild.EveryoneRole }).OrderBy(r => r.Position)));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await msg.Reply("`List of roles:` \n• " + string.Join("\n• ", (msg.Channel as ITextChannel).Guild.Roles.Except(new[] { guild.EveryoneRole })));
|
await msg.Reply("`List of roles:` \n• " + string.Join("\n• ", (msg.Channel as ITextChannel).Guild.Roles.Except(new[] { guild.EveryoneRole }).OrderBy(r=>r.Position)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ namespace NadekoBot
|
|||||||
depMap.Add<IBotConfiguration>(Config);
|
depMap.Add<IBotConfiguration>(Config);
|
||||||
depMap.Add<DiscordSocketClient>(Client);
|
depMap.Add<DiscordSocketClient>(Client);
|
||||||
depMap.Add<CommandService>(Commands);
|
depMap.Add<CommandService>(Commands);
|
||||||
depMap.Add<IYoutubeService>(Youtube);
|
depMap.Add<IGoogleApiService>(Youtube);
|
||||||
|
|
||||||
//connect
|
//connect
|
||||||
await Client.LoginAsync(TokenType.Bot, Credentials.Token);
|
await Client.LoginAsync(TokenType.Bot, Credentials.Token);
|
||||||
|
@ -3,10 +3,12 @@ using System.Threading.Tasks;
|
|||||||
|
|
||||||
namespace NadekoBot.Services
|
namespace NadekoBot.Services
|
||||||
{
|
{
|
||||||
public interface IYoutubeService
|
public interface IGoogleApiService
|
||||||
{
|
{
|
||||||
Task<IEnumerable<string>> FindVideosByKeywordsAsync(string keywords, int count = 1);
|
Task<IEnumerable<string>> FindVideosByKeywordsAsync(string keywords, int count = 1);
|
||||||
Task<IEnumerable<string>> FindPlaylistIdsByKeywordsAsync(string keywords, int count = 1);
|
Task<IEnumerable<string>> FindPlaylistIdsByKeywordsAsync(string keywords, int count = 1);
|
||||||
Task<IEnumerable<string>> FindRelatedVideosAsync(string url, int count = 1);
|
Task<IEnumerable<string>> FindRelatedVideosAsync(string url, int count = 1);
|
||||||
|
|
||||||
|
Task<string> ShortenUrl(string url);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,26 +5,29 @@ using System.Threading.Tasks;
|
|||||||
using Google.Apis.YouTube.v3;
|
using Google.Apis.YouTube.v3;
|
||||||
using Google.Apis.Services;
|
using Google.Apis.Services;
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Diagnostics.Contracts;
|
using Google.Apis.Urlshortener.v1;
|
||||||
|
using Google.Apis.Urlshortener.v1.Data;
|
||||||
|
|
||||||
namespace NadekoBot.Services.Impl
|
namespace NadekoBot.Services.Impl
|
||||||
{
|
{
|
||||||
public class YoutubeService : IYoutubeService
|
public class GoogleApiService : IGoogleApiService
|
||||||
{
|
{
|
||||||
private YouTubeService yt;
|
private YouTubeService yt;
|
||||||
|
private UrlshortenerService sh;
|
||||||
|
|
||||||
public YoutubeService()
|
public GoogleApiService()
|
||||||
{
|
{
|
||||||
yt = new YouTubeService(new BaseClientService.Initializer {
|
var bcs = new BaseClientService.Initializer
|
||||||
|
{
|
||||||
ApplicationName = "Nadeko Bot",
|
ApplicationName = "Nadeko Bot",
|
||||||
ApiKey = NadekoBot.Credentials.GoogleApiKey
|
ApiKey = NadekoBot.Credentials.GoogleApiKey
|
||||||
});
|
};
|
||||||
|
|
||||||
|
yt = new YouTubeService(bcs);
|
||||||
|
sh = new UrlshortenerService(bcs);
|
||||||
}
|
}
|
||||||
public async Task<IEnumerable<string>> FindPlaylistIdsByKeywordsAsync(string keywords, int count = 1)
|
public async Task<IEnumerable<string>> FindPlaylistIdsByKeywordsAsync(string keywords, int count = 1)
|
||||||
{
|
{
|
||||||
//Contract.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(keywords));
|
|
||||||
//Contract.Requires<ArgumentOutOfRangeException>(count > 0);
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(keywords))
|
if (string.IsNullOrWhiteSpace(keywords))
|
||||||
throw new ArgumentNullException(nameof(keywords));
|
throw new ArgumentNullException(nameof(keywords));
|
||||||
|
|
||||||
@ -77,5 +80,14 @@ namespace NadekoBot.Services.Impl
|
|||||||
query.Type = "video";
|
query.Type = "video";
|
||||||
return (await query.ExecuteAsync()).Items.Select(i => "http://www.youtube.com/watch?v=" + i.Id.VideoId);
|
return (await query.ExecuteAsync()).Items.Select(i => "http://www.youtube.com/watch?v=" + i.Id.VideoId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<string> ShortenUrl(string url)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(url))
|
||||||
|
throw new ArgumentNullException(nameof(url));
|
||||||
|
|
||||||
|
var response = await sh.Url.Insert(new Url { LongUrl = url }).ExecuteAsync();
|
||||||
|
return response.Id;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -81,39 +81,6 @@ namespace NadekoBot.Extensions
|
|||||||
return ch.SendTableAsync("", items, howToPrint, columns);
|
return ch.SendTableAsync("", items, howToPrint, columns);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static async Task<string> ShortenUrl(this string url)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(NadekoBot.Credentials.GoogleApiKey)) return url;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var httpWebRequest =
|
|
||||||
(HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" +
|
|
||||||
NadekoBot.Credentials.GoogleApiKey);
|
|
||||||
httpWebRequest.ContentType = "application/json";
|
|
||||||
httpWebRequest.Method = "POST";
|
|
||||||
|
|
||||||
using (var streamWriter = new StreamWriter(await httpWebRequest.GetRequestStreamAsync().ConfigureAwait(false)))
|
|
||||||
{
|
|
||||||
var json = "{\"longUrl\":\"" + Uri.EscapeDataString(url) + "\"}";
|
|
||||||
streamWriter.Write(json);
|
|
||||||
}
|
|
||||||
|
|
||||||
var httpResponse = (await httpWebRequest.GetResponseAsync().ConfigureAwait(false)) as HttpWebResponse;
|
|
||||||
var responseStream = httpResponse.GetResponseStream();
|
|
||||||
using (var streamReader = new StreamReader(responseStream))
|
|
||||||
{
|
|
||||||
var responseText = await streamReader.ReadToEndAsync().ConfigureAwait(false);
|
|
||||||
return Regex.Match(responseText, @"""id"": ?""(?<id>.+)""").Groups["id"].Value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Console.WriteLine("Shortening of this url failed: " + url);
|
|
||||||
Console.WriteLine(ex.ToString());
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// returns an IEnumerable with randomized element order
|
/// returns an IEnumerable with randomized element order
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
@ -22,6 +22,7 @@
|
|||||||
"Discord.Net.Commands": "1.0.0-dev",
|
"Discord.Net.Commands": "1.0.0-dev",
|
||||||
"System.Resources.ResourceWriter": "4.0.0-beta-22816",
|
"System.Resources.ResourceWriter": "4.0.0-beta-22816",
|
||||||
"Google.Apis.YouTube.v3": "1.15.0.582",
|
"Google.Apis.YouTube.v3": "1.15.0.582",
|
||||||
|
"Google.Apis.Urlshortener.v1": "1.15.0.138",
|
||||||
"System.Diagnostics.Contracts": "4.0.1",
|
"System.Diagnostics.Contracts": "4.0.1",
|
||||||
"NLog": "4.4.0-betaV15"
|
"NLog": "4.4.0-betaV15"
|
||||||
},
|
},
|
||||||
|
@ -88,6 +88,19 @@
|
|||||||
"lib/netstandard1.3/Google.Apis.Core.dll": {}
|
"lib/netstandard1.3/Google.Apis.Core.dll": {}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"Google.Apis.Urlshortener.v1/1.15.0.138": {
|
||||||
|
"type": "package",
|
||||||
|
"dependencies": {
|
||||||
|
"Google.Apis": "1.15.0",
|
||||||
|
"Google.Apis.Auth": "1.15.0"
|
||||||
|
},
|
||||||
|
"compile": {
|
||||||
|
"lib/netstandard1.3/Google.Apis.Urlshortener.v1.dll": {}
|
||||||
|
},
|
||||||
|
"runtime": {
|
||||||
|
"lib/netstandard1.3/Google.Apis.Urlshortener.v1.dll": {}
|
||||||
|
}
|
||||||
|
},
|
||||||
"Google.Apis.YouTube.v3/1.15.0.582": {
|
"Google.Apis.YouTube.v3/1.15.0.582": {
|
||||||
"type": "package",
|
"type": "package",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -2654,6 +2667,24 @@
|
|||||||
"lib/portable-net45+sl50+netcore45+wpa81+wp8/Google.Apis.Core.xml"
|
"lib/portable-net45+sl50+netcore45+wpa81+wp8/Google.Apis.Core.xml"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"Google.Apis.Urlshortener.v1/1.15.0.138": {
|
||||||
|
"sha512": "67USnpqrk8tWO3LAgaK9qDQT6h8A7i7eUIOKm+OISThZoQuHiLCn6dbg46FVb597LUh57AxClSSbhnweYcYC3Q==",
|
||||||
|
"type": "package",
|
||||||
|
"path": "Google.Apis.Urlshortener.v1/1.15.0.138",
|
||||||
|
"files": [
|
||||||
|
"Google.Apis.Urlshortener.v1.1.15.0.138.nupkg.sha512",
|
||||||
|
"Google.Apis.Urlshortener.v1.nuspec",
|
||||||
|
"lib/netstandard1.3/Google.Apis.Urlshortener.v1.dll",
|
||||||
|
"lib/netstandard1.3/Google.Apis.Urlshortener.v1.pdb",
|
||||||
|
"lib/netstandard1.3/Google.Apis.Urlshortener.v1.xml",
|
||||||
|
"lib/portable-net40+sl50+netcore45+wpa81+wp8/Google.Apis.Urlshortener.v1.dll",
|
||||||
|
"lib/portable-net40+sl50+netcore45+wpa81+wp8/Google.Apis.Urlshortener.v1.pdb",
|
||||||
|
"lib/portable-net40+sl50+netcore45+wpa81+wp8/Google.Apis.Urlshortener.v1.xml",
|
||||||
|
"lib/portable-net45+netcore45+wpa81+wp8/Google.Apis.Urlshortener.v1.dll",
|
||||||
|
"lib/portable-net45+netcore45+wpa81+wp8/Google.Apis.Urlshortener.v1.pdb",
|
||||||
|
"lib/portable-net45+netcore45+wpa81+wp8/Google.Apis.Urlshortener.v1.xml"
|
||||||
|
]
|
||||||
|
},
|
||||||
"Google.Apis.YouTube.v3/1.15.0.582": {
|
"Google.Apis.YouTube.v3/1.15.0.582": {
|
||||||
"sha512": "isR8FdI417PKLgLlNdOVDhduO+8yqPJ+vfID1Zx0MjAa/y3q655Plk2E/KNmsrjvXkqSSWwDCQHPz/Q1fat4tA==",
|
"sha512": "isR8FdI417PKLgLlNdOVDhduO+8yqPJ+vfID1Zx0MjAa/y3q655Plk2E/KNmsrjvXkqSSWwDCQHPz/Q1fat4tA==",
|
||||||
"type": "package",
|
"type": "package",
|
||||||
@ -7855,6 +7886,7 @@
|
|||||||
"": [
|
"": [
|
||||||
"Discord.Net >= 1.0.0-dev",
|
"Discord.Net >= 1.0.0-dev",
|
||||||
"Discord.Net.Commands >= 1.0.0-dev",
|
"Discord.Net.Commands >= 1.0.0-dev",
|
||||||
|
"Google.Apis.Urlshortener.v1 >= 1.15.0.138",
|
||||||
"Google.Apis.YouTube.v3 >= 1.15.0.582",
|
"Google.Apis.YouTube.v3 >= 1.15.0.582",
|
||||||
"Microsoft.Extensions.DependencyInjection >= 1.0.0",
|
"Microsoft.Extensions.DependencyInjection >= 1.0.0",
|
||||||
"Microsoft.Extensions.DependencyInjection.Abstractions >= 1.0.0",
|
"Microsoft.Extensions.DependencyInjection.Abstractions >= 1.0.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user