private Embed CreateEmbed(YouTubeRecord record, YouTubeToCheck youtube) { var embed = new EmbedBuilder(); var author = new EmbedAuthorBuilder { Name = record.AuthorName, Url = record.AuthorUrl, IconUrl = record.AuthorIconUrl }; var publishedAt = TimeZoneInfo.ConvertTime(record.PublishedAt, Helpers.CentralTimeZone()); var footer = new EmbedFooterBuilder { Text = $"Posted on {publishedAt:MMM d, yyyy} at {publishedAt:H:mm} Central" }; var descriptionField = new EmbedFieldBuilder { Name = "Description", Value = record.VideoDescription, IsInline = false }; embed.Author = author; embed.Footer = footer; embed.Color = new Color((uint)youtube.EmbedColor); embed.ImageUrl = record.ImageUrl; embed.Title = record.VideoTitle; embed.Url = "https://www.youtube.com/watch?v=" + record.VideoId; embed.AddField(descriptionField); return(embed.Build()); }
private void WorkerAnalyse_DoWork(object sender, DoWorkEventArgs e) { _workerAnalyse.ReportProgress(0); try { _records.Clear(); var uri = new Uri(teAnalyseUri.Text); var webClient = new WebClient { Encoding = Encoding.UTF8 }; _html = webClient.DownloadString(uri); _workerAnalyse.ReportProgress(1); var parser = new HtmlParser(); var angle = parser.Parse(_html); //IHtmlCollection<IElement> list = angle.All.Where(i => i.LocalName == "li"); var strArr = new string[1]; var index = 1; var reference = ""; var list = angle.QuerySelectorAll("li.yt-uix-scroller-scroll-unit"); foreach (var element in list) { var indexes = element.QuerySelectorAll("span.index"); if (indexes != null && indexes.Any()) { strArr = indexes[0].TextContent.Split('\n'); } if (strArr.Length == 3 && int.TryParse(strArr[1], out var result)) { index = result; } var references = element.QuerySelectorAll("a"); if (references != null && references.Any()) { reference = references[0].GetAttribute("href"); } var record = new YouTubeRecord() { Index = index, Title = element.GetAttribute("data-video-title"), Reference = reference }; _records.Add(record); } _workerAnalyse.ReportProgress(2); } catch (Exception ex) { _html = $"{ex.Source} - {ex.Message}"; _workerAnalyse.ReportProgress(-1); } }
/// <summary> /// Загрузка записи в определенную директорию. /// </summary> private void DownloadRecord(string link, string path, string name, YouTubeRecord record) { var tempFile = ""; name = name.Replace('/', '-'); try { BeginInvoke(new Action(() => { PublishEvent($"[{name}] - try to download..."); })); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } var videoInfos = DownloadUrlResolver.GetDownloadUrls(link, false); var video = videoInfos.First(); var finalFile = Path.Combine(path, ReplaceIllegalPathCharacters(name) + video.VideoExtension); if (!File.Exists(finalFile)) { tempFile = Path.Combine(path, ReplaceIllegalPathCharacters(name) + ".tmp"); var videoDownloader = new VideoDownloaderEx(video, tempFile, record); videoDownloader.DownloadProgressChanged += VideoDownloader_DownloadProgressChanged; videoDownloader.Execute(); RenameFile(tempFile, finalFile); } record.ProgressPercentage = 100; BeginInvoke(new Action(() => { PublishEvent($"[{name}] - was downloaded successfully"); })); } catch (Exception ex) { BeginInvoke(new Action(() => { PublishEvent($"{ex.Source} - {ex.Message}"); })); if (!string.IsNullOrEmpty(tempFile)) { DeleteFile(tempFile); } } }
public void Execute() { Log.Debug("Checking youtube updates for {channel}", _youTubeToCheck.FriendlyUsername); var videos = _youtubeService.GetYouTubeVideoListAsync(_youTubeToCheck.ChannelId).GetAwaiter() .GetResult(); if (videos.Items is null) { return; } var latestVideo = videos.Items.FirstOrDefault(); var latestVideoId = latestVideo.Id.VideoId; YouTubeRecord existingRecord; using (var db = new DggContext()) { existingRecord = db.YouTubeRecords.FirstOrDefault(r => r.ChannelId == _youTubeToCheck.ChannelId); } if (existingRecord != null && latestVideo.Snippet.PublishedAt.CompareTo(new DateTimeOffset(existingRecord.PublishedAt)) <= 0) { return; } var embed = SendMessageAsync(_youTubeToCheck, latestVideo).GetAwaiter().GetResult(); if (embed == null) { return; } using (var db = new DggContext()) { if (existingRecord == null) { existingRecord = new YouTubeRecord { ChannelId = _youTubeToCheck.ChannelId, VideoId = latestVideoId, VideoTitle = latestVideo.Snippet.Title, VideoDescription = embed.Description, ImageUrl = embed.Image.Value.Url, AuthorName = embed.Author.Value.Name, AuthorUrl = embed.Author.Value.Url, AuthorIconUrl = embed.Author.Value.IconUrl, PublishedAt = latestVideo.Snippet.PublishedAt.UtcDateTime }; db.YouTubeRecords.Add(existingRecord); } else { existingRecord.VideoId = latestVideoId; existingRecord.VideoTitle = latestVideo.Snippet.Title; existingRecord.VideoDescription = embed.Description; existingRecord.ImageUrl = embed.Image.Value.Url; existingRecord.AuthorName = embed.Author.Value.Name; existingRecord.AuthorUrl = embed.Author.Value.Url; existingRecord.AuthorIconUrl = embed.Author.Value.IconUrl; existingRecord.PublishedAt = latestVideo.Snippet.PublishedAt.UtcDateTime; db.YouTubeRecords.Update(existingRecord); } db.SaveChanges(); } }