2017-09-14 22:11:19 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord.Commands;
|
2017-10-12 16:34:10 +02:00
|
|
|
|
using Geekbot.net.Lib;
|
2017-09-14 22:11:19 +02:00
|
|
|
|
using Google.Apis.Services;
|
|
|
|
|
using Google.Apis.YouTube.v3;
|
|
|
|
|
using StackExchange.Redis;
|
|
|
|
|
|
2017-10-02 21:57:48 +02:00
|
|
|
|
namespace Geekbot.net.Commands
|
2017-09-14 22:11:19 +02:00
|
|
|
|
{
|
|
|
|
|
public class Youtube : ModuleBase
|
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
private readonly IErrorHandler _errorHandler;
|
2017-12-29 01:53:50 +01:00
|
|
|
|
private readonly IDatabase _redis;
|
2017-09-15 22:56:03 +02:00
|
|
|
|
|
2017-10-26 00:55:04 +02:00
|
|
|
|
public Youtube(IDatabase redis, IErrorHandler errorHandler)
|
2017-09-14 22:11:19 +02:00
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
_redis = redis;
|
|
|
|
|
_errorHandler = errorHandler;
|
2017-09-14 22:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Command("yt", RunMode = RunMode.Async)]
|
2017-10-12 16:34:10 +02:00
|
|
|
|
[Remarks(CommandCategories.Helpers)]
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Summary("Search for something on youtube.")]
|
|
|
|
|
public async Task Yt([Remainder] [Summary("Title")] string searchQuery)
|
2017-09-14 22:11:19 +02:00
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
var key = _redis.StringGet("youtubeKey");
|
2017-09-14 22:11:19 +02:00
|
|
|
|
if (key.IsNullOrEmpty)
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("No youtube key set, please tell my senpai to set one");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-09-15 22:56:03 +02:00
|
|
|
|
var youtubeService = new YouTubeService(new BaseClientService.Initializer
|
2017-09-14 22:11:19 +02:00
|
|
|
|
{
|
|
|
|
|
ApiKey = key.ToString(),
|
2017-09-15 22:56:03 +02:00
|
|
|
|
ApplicationName = GetType().ToString()
|
2017-09-14 22:11:19 +02:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var searchListRequest = youtubeService.Search.List("snippet");
|
|
|
|
|
searchListRequest.Q = searchQuery;
|
|
|
|
|
searchListRequest.MaxResults = 2;
|
|
|
|
|
|
|
|
|
|
var searchListResponse = await searchListRequest.ExecuteAsync();
|
|
|
|
|
|
|
|
|
|
var result = searchListResponse.Items[0];
|
|
|
|
|
|
|
|
|
|
await ReplyAsync(
|
|
|
|
|
$"\"{result.Snippet.Title}\" from \"{result.Snippet.ChannelTitle}\" https://youtu.be/{result.Id.VideoId}");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
2017-09-14 22:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-04-17 12:55:20 +02:00
|
|
|
|
}
|