NadekoBot/NadekoBot.Modules.Music/Common/SongResolver/Strategies/YoutubeResolveStrategy.cs

70 lines
2.3 KiB
C#
Raw Normal View History

using NadekoBot.Core.Services.Database.Models;
using NadekoBot.Core.Services.Impl;
2017-07-11 01:16:56 +00:00
using NLog;
using System;
using System.Globalization;
using System.Threading.Tasks;
2017-07-17 02:37:51 +00:00
namespace NadekoBot.Modules.Music.Common.SongResolver.Strategies
2017-07-11 01:16:56 +00:00
{
public class YoutubeResolveStrategy : IResolveStrategy
{
private readonly Logger _log;
public YoutubeResolveStrategy()
{
_log = LogManager.GetCurrentClassLogger();
}
public async Task<SongInfo> ResolveSong(string query)
{
_log.Info("Getting link");
string[] data;
try
{
using (var ytdl = new YtdlOperation())
{
data = (await ytdl.GetDataAsync(query)).Split('\n');
}
if (data.Length < 6)
{
_log.Info("No song found. Data less than 6");
return null;
}
TimeSpan time;
if (!TimeSpan.TryParseExact(data[4], new[] { "ss", "m\\:ss", "mm\\:ss", "h\\:mm\\:ss", "hh\\:mm\\:ss", "hhh\\:mm\\:ss" }, CultureInfo.InvariantCulture, out time))
time = TimeSpan.FromHours(24);
return new SongInfo()
{
Title = data[0],
VideoId = data[1],
Uri = async () =>
{
using (var ytdl = new YtdlOperation())
{
data = (await ytdl.GetDataAsync(query)).Split('\n');
}
if (data.Length < 6)
{
_log.Info("No song found. Data less than 6");
return null;
}
return data[2];
},
Thumbnail = data[3],
TotalTime = time,
Provider = "YouTube",
ProviderType = MusicType.YouTube,
Query = "https://youtube.com/watch?v=" + data[1],
};
}
catch (Exception ex)
{
_log.Warn(ex);
return null;
}
}
}
}