/// <summary> /// Embed creator to build a better Message /// </summary> /// <param name="clip"></param> /// <returns></returns> private Embed CreateEmbed(ClipInfo clip) { EmbedBuilder clipInfo = new EmbedBuilder() { Description = clip.title }; string creatorName = "Name is missing"; if (clip.creator_name != "") { creatorName = clip.creator_name; } EmbedFieldBuilder field1 = new EmbedFieldBuilder() { Name = "Creator", Value = creatorName, IsInline = true }; EmbedFieldBuilder field2 = new EmbedFieldBuilder() { Name = "Created at", Value = clip.created_at.ToString("dd.MM.yyyy"), IsInline = true }; clipInfo.AddField(field1); clipInfo.AddField(field2); Embed finished = clipInfo.Build(); return(finished); }
/// <summary> /// Save the whole clip information into the database /// </summary> /// <param name="clip"></param> /// <returns></returns> public async Task ClipToDatabase(ClipInfo clip) { try { using (MySqlCommand command = new MySqlCommand()) { command.Connection = connection; command.CommandText = "INSERT INTO " + "clips " + "(id)" + " VALUES " + "(@id)"; command.Parameters.AddWithValue("@id", clip.id); await command.ExecuteNonQueryAsync(); } } catch (Exception ex) { List <string> msg = new List <string>(); msg.Add(ex.Message); msg.Add(ex.StackTrace); Program.Logging.Log(msg); Environment.Exit(0); } }
/// <summary> /// Upload the Clip into the discord channel /// </summary> /// <param name="filepath"></param> /// <param name="clip"></param> /// <returns></returns> public async Task UploadClipToDiscord(string filepath, ClipInfo clip) { // Get Server to send clips to SocketGuild server = client.GetGuild(ServerId) as SocketGuild; // Get Channel to send clips to SocketTextChannel channel = server.GetChannel(ChannelId) as SocketTextChannel; IDisposable typing = channel.EnterTypingState(); await channel.SendFileAsync(filepath, "", false, CreateEmbed(clip)); typing.Dispose(); }
/// <summary> /// Get the clips from twitch /// </summary> /// <param name="fiveMinutesAgo"></param> /// <param name="database"></param> /// <param name="configuration"></param> /// <param name="discord"></param> /// <param name="getAllClips"></param> /// <param name="wholeDay"></param> /// <returns></returns> private async Task ConfigClipSearch(Database database, Discord discord, bool getAllClips, DateTime currentTime, bool wholeDay = false, TwitchClass import = null, int daysToGoThrough = 0) { await Task.Run(async() => { // DateTime currentTime = DateTime.UtcNow; // if (bypass != null) // { // currentTime = bypass.Value.AddMinutes(-10); // } DateTime past = currentTime.AddMinutes(-30); var counter = 0; TwitchClass streamTwitch = null; int endDay = 30; int endMonth = 1; int endYear = 2019; string finalDate = ""; // List<ClipInfo> clips = new List<ClipInfo>(); if (getAllClips || import != null) { if (import != null) { clips = import.data; clips = clips.OrderByDescending(d => d.created_at).ToList(); ClipInfo newest = clips[0]; DateTime newEnd = newest.created_at.AddDays(-3); endMonth = newEnd.Month; endDay = newEnd.Day; endYear = newEnd.Year; } } string broadcasterId = configuration.GetSettings("Broadcaster_ID"); double amountsOfRunningThrough = 1; if (timeToSearch > 30) { amountsOfRunningThrough = Math.Ceiling(Convert.ToDouble(timeToSearch) / 30); } if (wholeDay) { amountsOfRunningThrough = 96; } if (daysToGoThrough > 0) { amountsOfRunningThrough = 48 * daysToGoThrough; } do { if (counter > 0) { currentTime = currentTime.AddMinutes(-30); past = past.AddMinutes(-30); } counter++; string endDate = currentTime.ToString("yyyy") + "-" + currentTime.ToString("MM") + "-" + currentTime.ToString("dd") + "T" + currentTime.ToString("HH") + ":" + currentTime.ToString("mm") + ":" + currentTime.ToString("ss") + "Z"; string date = past.ToString("yyyy") + "-" + past.ToString("MM") + "-" + past.ToString("dd") + "T" + past.ToString("HH") + ":" + past.ToString("mm") + ":" + past.ToString("ss") + "Z"; int PaginationCounter = 0; do { string url = "https://api.twitch.tv/helix/clips?broadcaster_id=" + broadcasterId + "&started_at="; if (PaginationCounter == 0) { PaginationCounter++; url = url + date + "&ended_at=" + endDate; } else { url = url + date + "&ended_at=" + endDate + "&after=" + streamTwitch.pagination.cursor; } string responseBody = await twitch.GetAsync(url).Result.Content.ReadAsStringAsync(); streamTwitch = JsonSerializer.Deserialize <TwitchClass>(responseBody); foreach (ClipInfo clip in streamTwitch.data) { lock (_lockObj) { clips.Add(clip); } } // Console.Clear(); // Console.WriteLine("Got " + clips.Count + " Clips"); } while (streamTwitch.pagination.cursor != null); finalDate = endDate; amountsOfRunningThrough--; if (amountsOfRunningThrough == 0 && getAllClips == false) { break; } else if (getAllClips && (currentTime.Month == endMonth && currentTime.Day == endDay && currentTime.Year == endYear)) { break; } } while (true); // Filter out duplicates // lock (_lockObj) // { // clips = clips.Distinct().ToList(); // } // Console.WriteLine("done getting clips " + finalDate); // Console.WriteLine("Got " + clips.Count + " Clips"); // Console.WriteLine(); // if (clips.Count > 0) // { // // Order from Oldest to Newest // // clips = clips.OrderBy(d => d.created_at).ToList(); // // await DownloadClips(discord, database, clips); // } }); }