mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
Applied codestyle to all .cs files
This commit is contained in:
@@ -7,6 +7,7 @@ using Google.Apis.YouTube.v3;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using System.Net;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml;
|
||||
|
||||
namespace NadekoBot.Services;
|
||||
|
||||
@@ -14,26 +15,162 @@ public class GoogleApiService : IGoogleApiService, INService
|
||||
{
|
||||
private const string SearchEngineId = "018084019232060951019:hs5piey28-e";
|
||||
|
||||
private static readonly Regex
|
||||
plRegex = new("(?:youtu\\.be\\/|list=)(?<id>[\\da-zA-Z\\-_]*)", RegexOptions.Compiled);
|
||||
|
||||
public IReadOnlyDictionary<string, string> Languages { get; } = new Dictionary<string, string>
|
||||
{
|
||||
{ "afrikaans", "af" },
|
||||
{ "albanian", "sq" },
|
||||
{ "arabic", "ar" },
|
||||
{ "armenian", "hy" },
|
||||
{ "azerbaijani", "az" },
|
||||
{ "basque", "eu" },
|
||||
{ "belarusian", "be" },
|
||||
{ "bengali", "bn" },
|
||||
{ "bulgarian", "bg" },
|
||||
{ "catalan", "ca" },
|
||||
{ "chinese-traditional", "zh-TW" },
|
||||
{ "chinese-simplified", "zh-CN" },
|
||||
{ "chinese", "zh-CN" },
|
||||
{ "croatian", "hr" },
|
||||
{ "czech", "cs" },
|
||||
{ "danish", "da" },
|
||||
{ "dutch", "nl" },
|
||||
{ "english", "en" },
|
||||
{ "esperanto", "eo" },
|
||||
{ "estonian", "et" },
|
||||
{ "filipino", "tl" },
|
||||
{ "finnish", "fi" },
|
||||
{ "french", "fr" },
|
||||
{ "galician", "gl" },
|
||||
{ "german", "de" },
|
||||
{ "georgian", "ka" },
|
||||
{ "greek", "el" },
|
||||
{ "haitian Creole", "ht" },
|
||||
{ "hebrew", "iw" },
|
||||
{ "hindi", "hi" },
|
||||
{ "hungarian", "hu" },
|
||||
{ "icelandic", "is" },
|
||||
{ "indonesian", "id" },
|
||||
{ "irish", "ga" },
|
||||
{ "italian", "it" },
|
||||
{ "japanese", "ja" },
|
||||
{ "korean", "ko" },
|
||||
{ "lao", "lo" },
|
||||
{ "latin", "la" },
|
||||
{ "latvian", "lv" },
|
||||
{ "lithuanian", "lt" },
|
||||
{ "macedonian", "mk" },
|
||||
{ "malay", "ms" },
|
||||
{ "maltese", "mt" },
|
||||
{ "norwegian", "no" },
|
||||
{ "persian", "fa" },
|
||||
{ "polish", "pl" },
|
||||
{ "portuguese", "pt" },
|
||||
{ "romanian", "ro" },
|
||||
{ "russian", "ru" },
|
||||
{ "serbian", "sr" },
|
||||
{ "slovak", "sk" },
|
||||
{ "slovenian", "sl" },
|
||||
{ "spanish", "es" },
|
||||
{ "swahili", "sw" },
|
||||
{ "swedish", "sv" },
|
||||
{ "tamil", "ta" },
|
||||
{ "telugu", "te" },
|
||||
{ "thai", "th" },
|
||||
{ "turkish", "tr" },
|
||||
{ "ukrainian", "uk" },
|
||||
{ "urdu", "ur" },
|
||||
{ "vietnamese", "vi" },
|
||||
{ "welsh", "cy" },
|
||||
{ "yiddish", "yi" },
|
||||
{ "af", "af" },
|
||||
{ "sq", "sq" },
|
||||
{ "ar", "ar" },
|
||||
{ "hy", "hy" },
|
||||
{ "az", "az" },
|
||||
{ "eu", "eu" },
|
||||
{ "be", "be" },
|
||||
{ "bn", "bn" },
|
||||
{ "bg", "bg" },
|
||||
{ "ca", "ca" },
|
||||
{ "zh-tw", "zh-TW" },
|
||||
{ "zh-cn", "zh-CN" },
|
||||
{ "hr", "hr" },
|
||||
{ "cs", "cs" },
|
||||
{ "da", "da" },
|
||||
{ "nl", "nl" },
|
||||
{ "en", "en" },
|
||||
{ "eo", "eo" },
|
||||
{ "et", "et" },
|
||||
{ "tl", "tl" },
|
||||
{ "fi", "fi" },
|
||||
{ "fr", "fr" },
|
||||
{ "gl", "gl" },
|
||||
{ "de", "de" },
|
||||
{ "ka", "ka" },
|
||||
{ "el", "el" },
|
||||
{ "ht", "ht" },
|
||||
{ "iw", "iw" },
|
||||
{ "hi", "hi" },
|
||||
{ "hu", "hu" },
|
||||
{ "is", "is" },
|
||||
{ "id", "id" },
|
||||
{ "ga", "ga" },
|
||||
{ "it", "it" },
|
||||
{ "ja", "ja" },
|
||||
{ "ko", "ko" },
|
||||
{ "lo", "lo" },
|
||||
{ "la", "la" },
|
||||
{ "lv", "lv" },
|
||||
{ "lt", "lt" },
|
||||
{ "mk", "mk" },
|
||||
{ "ms", "ms" },
|
||||
{ "mt", "mt" },
|
||||
{ "no", "no" },
|
||||
{ "fa", "fa" },
|
||||
{ "pl", "pl" },
|
||||
{ "pt", "pt" },
|
||||
{ "ro", "ro" },
|
||||
{ "ru", "ru" },
|
||||
{ "sr", "sr" },
|
||||
{ "sk", "sk" },
|
||||
{ "sl", "sl" },
|
||||
{ "es", "es" },
|
||||
{ "sw", "sw" },
|
||||
{ "sv", "sv" },
|
||||
{ "ta", "ta" },
|
||||
{ "te", "te" },
|
||||
{ "th", "th" },
|
||||
{ "tr", "tr" },
|
||||
{ "uk", "uk" },
|
||||
{ "ur", "ur" },
|
||||
{ "vi", "vi" },
|
||||
{ "cy", "cy" },
|
||||
{ "yi", "yi" }
|
||||
};
|
||||
|
||||
private readonly YouTubeService yt;
|
||||
private readonly UrlshortenerService sh;
|
||||
private readonly CustomsearchService cs;
|
||||
|
||||
|
||||
//private readonly Regex YtVideoIdRegex = new Regex(@"(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\?(?:\S*?&?v\=))|youtu\.be\/)(?<id>[a-zA-Z0-9_-]{6,11})", RegexOptions.Compiled);
|
||||
private readonly IBotCredentials _creds;
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
|
||||
public GoogleApiService(IBotCredentials creds, IHttpClientFactory factory)
|
||||
{
|
||||
_creds = creds;
|
||||
_httpFactory = factory;
|
||||
|
||||
var bcs = new BaseClientService.Initializer
|
||||
{
|
||||
ApplicationName = "Nadeko Bot",
|
||||
ApiKey = _creds.GoogleApiKey,
|
||||
};
|
||||
|
||||
var bcs = new BaseClientService.Initializer { ApplicationName = "Nadeko Bot", ApiKey = _creds.GoogleApiKey };
|
||||
|
||||
yt = new(bcs);
|
||||
sh = new(bcs);
|
||||
cs = new(bcs);
|
||||
}
|
||||
private static readonly Regex plRegex = new("(?:youtu\\.be\\/|list=)(?<id>[\\da-zA-Z\\-_]*)", RegexOptions.Compiled);
|
||||
|
||||
public async Task<IEnumerable<string>> GetPlaylistIdsByKeywordsAsync(string keywords, int count = 1)
|
||||
{
|
||||
await Task.Yield();
|
||||
@@ -44,10 +181,7 @@ public class GoogleApiService : IGoogleApiService, INService
|
||||
throw new ArgumentOutOfRangeException(nameof(count));
|
||||
|
||||
var match = plRegex.Match(keywords);
|
||||
if (match.Length > 1)
|
||||
{
|
||||
return new[] { match.Groups["id"].Value.ToString() };
|
||||
}
|
||||
if (match.Length > 1) return new[] { match.Groups["id"].Value };
|
||||
var query = yt.Search.List("snippet");
|
||||
query.MaxResults = count;
|
||||
query.Type = "playlist";
|
||||
@@ -56,10 +190,6 @@ public class GoogleApiService : IGoogleApiService, INService
|
||||
return (await query.ExecuteAsync()).Items.Select(i => i.Id.PlaylistId);
|
||||
}
|
||||
|
||||
//private readonly Regex YtVideoIdRegex = new Regex(@"(?:youtube\.com\/\S*(?:(?:\/e(?:mbed))?\/|watch\?(?:\S*?&?v\=))|youtu\.be\/)(?<id>[a-zA-Z0-9_-]{6,11})", RegexOptions.Compiled);
|
||||
private readonly IBotCredentials _creds;
|
||||
private readonly IHttpClientFactory _httpFactory;
|
||||
|
||||
// todo future add quota users
|
||||
public async Task<IEnumerable<string>> GetRelatedVideosAsync(string id, int count = 1)
|
||||
{
|
||||
@@ -93,7 +223,9 @@ public class GoogleApiService : IGoogleApiService, INService
|
||||
return (await query.ExecuteAsync()).Items.Select(i => "http://www.youtube.com/watch?v=" + i.Id.VideoId);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<(string Name, string Id, string Url)>> GetVideoInfosByKeywordAsync(string keywords, int count = 1)
|
||||
public async Task<IEnumerable<(string Name, string Id, string Url)>> GetVideoInfosByKeywordAsync(
|
||||
string keywords,
|
||||
int count = 1)
|
||||
{
|
||||
await Task.Yield();
|
||||
if (string.IsNullOrWhiteSpace(keywords))
|
||||
@@ -106,10 +238,12 @@ public class GoogleApiService : IGoogleApiService, INService
|
||||
query.MaxResults = count;
|
||||
query.Q = keywords;
|
||||
query.Type = "video";
|
||||
return (await query.ExecuteAsync()).Items.Select(i => (i.Snippet.Title.TrimTo(50), i.Id.VideoId, "http://www.youtube.com/watch?v=" + i.Id.VideoId));
|
||||
return (await query.ExecuteAsync()).Items.Select(i
|
||||
=> (i.Snippet.Title.TrimTo(50), i.Id.VideoId, "http://www.youtube.com/watch?v=" + i.Id.VideoId));
|
||||
}
|
||||
|
||||
public Task<string> ShortenUrl(Uri url) => ShortenUrl(url.ToString());
|
||||
public Task<string> ShortenUrl(Uri url)
|
||||
=> ShortenUrl(url.ToString());
|
||||
|
||||
public async Task<string> ShortenUrl(string url)
|
||||
{
|
||||
@@ -163,8 +297,7 @@ public class GoogleApiService : IGoogleApiService, INService
|
||||
|
||||
toReturn.AddRange(data.Items.Select(i => i.ContentDetails.VideoId));
|
||||
nextPageToken = data.NextPageToken;
|
||||
}
|
||||
while (count > 0 && !string.IsNullOrWhiteSpace(nextPageToken));
|
||||
} while (count > 0 && !string.IsNullOrWhiteSpace(nextPageToken));
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
@@ -189,12 +322,8 @@ public class GoogleApiService : IGoogleApiService, INService
|
||||
q.Id = string.Join(",", videoIdsList.Take(toGet));
|
||||
videoIdsList = videoIdsList.Skip(toGet).ToList();
|
||||
var items = (await q.ExecuteAsync()).Items;
|
||||
foreach (var i in items)
|
||||
{
|
||||
toReturn.Add(i.Id, System.Xml.XmlConvert.ToTimeSpan(i.ContentDetails.Duration));
|
||||
}
|
||||
}
|
||||
while (remaining > 0);
|
||||
foreach (var i in items) toReturn.Add(i.Id, XmlConvert.ToTimeSpan(i.ContentDetails.Duration));
|
||||
} while (remaining > 0);
|
||||
|
||||
return toReturn;
|
||||
}
|
||||
@@ -218,156 +347,24 @@ public class GoogleApiService : IGoogleApiService, INService
|
||||
return new(search.Items[0].Image, search.Items[0].Link);
|
||||
}
|
||||
|
||||
public IReadOnlyDictionary<string, string> Languages { get; } = new Dictionary<string, string>() {
|
||||
{ "afrikaans", "af"},
|
||||
{ "albanian", "sq"},
|
||||
{ "arabic", "ar"},
|
||||
{ "armenian", "hy"},
|
||||
{ "azerbaijani", "az"},
|
||||
{ "basque", "eu"},
|
||||
{ "belarusian", "be"},
|
||||
{ "bengali", "bn"},
|
||||
{ "bulgarian", "bg"},
|
||||
{ "catalan", "ca"},
|
||||
{ "chinese-traditional", "zh-TW"},
|
||||
{ "chinese-simplified", "zh-CN"},
|
||||
{ "chinese", "zh-CN"},
|
||||
{ "croatian", "hr"},
|
||||
{ "czech", "cs"},
|
||||
{ "danish", "da"},
|
||||
{ "dutch", "nl"},
|
||||
{ "english", "en"},
|
||||
{ "esperanto", "eo"},
|
||||
{ "estonian", "et"},
|
||||
{ "filipino", "tl"},
|
||||
{ "finnish", "fi"},
|
||||
{ "french", "fr"},
|
||||
{ "galician", "gl"},
|
||||
{ "german", "de"},
|
||||
{ "georgian", "ka"},
|
||||
{ "greek", "el"},
|
||||
{ "haitian Creole", "ht"},
|
||||
{ "hebrew", "iw"},
|
||||
{ "hindi", "hi"},
|
||||
{ "hungarian", "hu"},
|
||||
{ "icelandic", "is"},
|
||||
{ "indonesian", "id"},
|
||||
{ "irish", "ga"},
|
||||
{ "italian", "it"},
|
||||
{ "japanese", "ja"},
|
||||
{ "korean", "ko"},
|
||||
{ "lao", "lo"},
|
||||
{ "latin", "la"},
|
||||
{ "latvian", "lv"},
|
||||
{ "lithuanian", "lt"},
|
||||
{ "macedonian", "mk"},
|
||||
{ "malay", "ms"},
|
||||
{ "maltese", "mt"},
|
||||
{ "norwegian", "no"},
|
||||
{ "persian", "fa"},
|
||||
{ "polish", "pl"},
|
||||
{ "portuguese", "pt"},
|
||||
{ "romanian", "ro"},
|
||||
{ "russian", "ru"},
|
||||
{ "serbian", "sr"},
|
||||
{ "slovak", "sk"},
|
||||
{ "slovenian", "sl"},
|
||||
{ "spanish", "es"},
|
||||
{ "swahili", "sw"},
|
||||
{ "swedish", "sv"},
|
||||
{ "tamil", "ta"},
|
||||
{ "telugu", "te"},
|
||||
{ "thai", "th"},
|
||||
{ "turkish", "tr"},
|
||||
{ "ukrainian", "uk"},
|
||||
{ "urdu", "ur"},
|
||||
{ "vietnamese", "vi"},
|
||||
{ "welsh", "cy"},
|
||||
{ "yiddish", "yi"},
|
||||
|
||||
{ "af", "af"},
|
||||
{ "sq", "sq"},
|
||||
{ "ar", "ar"},
|
||||
{ "hy", "hy"},
|
||||
{ "az", "az"},
|
||||
{ "eu", "eu"},
|
||||
{ "be", "be"},
|
||||
{ "bn", "bn"},
|
||||
{ "bg", "bg"},
|
||||
{ "ca", "ca"},
|
||||
{ "zh-tw", "zh-TW"},
|
||||
{ "zh-cn", "zh-CN"},
|
||||
{ "hr", "hr"},
|
||||
{ "cs", "cs"},
|
||||
{ "da", "da"},
|
||||
{ "nl", "nl"},
|
||||
{ "en", "en"},
|
||||
{ "eo", "eo"},
|
||||
{ "et", "et"},
|
||||
{ "tl", "tl"},
|
||||
{ "fi", "fi"},
|
||||
{ "fr", "fr"},
|
||||
{ "gl", "gl"},
|
||||
{ "de", "de"},
|
||||
{ "ka", "ka"},
|
||||
{ "el", "el"},
|
||||
{ "ht", "ht"},
|
||||
{ "iw", "iw"},
|
||||
{ "hi", "hi"},
|
||||
{ "hu", "hu"},
|
||||
{ "is", "is"},
|
||||
{ "id", "id"},
|
||||
{ "ga", "ga"},
|
||||
{ "it", "it"},
|
||||
{ "ja", "ja"},
|
||||
{ "ko", "ko"},
|
||||
{ "lo", "lo"},
|
||||
{ "la", "la"},
|
||||
{ "lv", "lv"},
|
||||
{ "lt", "lt"},
|
||||
{ "mk", "mk"},
|
||||
{ "ms", "ms"},
|
||||
{ "mt", "mt"},
|
||||
{ "no", "no"},
|
||||
{ "fa", "fa"},
|
||||
{ "pl", "pl"},
|
||||
{ "pt", "pt"},
|
||||
{ "ro", "ro"},
|
||||
{ "ru", "ru"},
|
||||
{ "sr", "sr"},
|
||||
{ "sk", "sk"},
|
||||
{ "sl", "sl"},
|
||||
{ "es", "es"},
|
||||
{ "sw", "sw"},
|
||||
{ "sv", "sv"},
|
||||
{ "ta", "ta"},
|
||||
{ "te", "te"},
|
||||
{ "th", "th"},
|
||||
{ "tr", "tr"},
|
||||
{ "uk", "uk"},
|
||||
{ "ur", "ur"},
|
||||
{ "vi", "vi"},
|
||||
{ "cy", "cy"},
|
||||
{ "yi", "yi"},
|
||||
};
|
||||
|
||||
public async Task<string> Translate(string sourceText, string sourceLanguage, string targetLanguage)
|
||||
{
|
||||
await Task.Yield();
|
||||
string text;
|
||||
|
||||
if (!Languages.ContainsKey(sourceLanguage) ||
|
||||
!Languages.ContainsKey(targetLanguage))
|
||||
if (!Languages.ContainsKey(sourceLanguage) || !Languages.ContainsKey(targetLanguage))
|
||||
throw new ArgumentException(nameof(sourceLanguage) + "/" + nameof(targetLanguage));
|
||||
|
||||
|
||||
var url = new Uri(string.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
|
||||
var url = new Uri(string.Format(
|
||||
"https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
|
||||
ConvertToLanguageCode(sourceLanguage),
|
||||
ConvertToLanguageCode(targetLanguage),
|
||||
WebUtility.UrlEncode(sourceText)));
|
||||
using (var http = _httpFactory.CreateClient())
|
||||
{
|
||||
http.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
|
||||
http.DefaultRequestHeaders.Add("user-agent",
|
||||
"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
|
||||
text = await http.GetStringAsync(url);
|
||||
}
|
||||
|
||||
@@ -379,4 +376,4 @@ public class GoogleApiService : IGoogleApiService, INService
|
||||
Languages.TryGetValue(language, out var mode);
|
||||
return mode;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user