public IEnumerable <KeyValuePair <string, YoutubeChannelInfo[]> > BrowseChannels() { var url = "https://www.youtube.com/feed/guide_builder"; var jsonString = url.DownloadHTML().GetYtInitialData(); var obj = JObject.Parse(jsonString); var itemsAll = obj["contents"]["twoColumnBrowseResultsRenderer"]["tabs"][0]["tabRenderer"]["content"][ "sectionListRenderer"] ["contents"].Select(c => c["itemSectionRenderer"]["contents"][0]["shelfRenderer"]); foreach (var item in itemsAll) { var title = item["title"]["simpleText"] + string.Empty; var results = new List <YoutubeChannelInfo>(); var channels = item["content"]["horizontalListRenderer"]["items"].Select(c => c["gridChannelRenderer"]); foreach (var c in channels) { var channel = new YoutubeChannelInfo(); channel.Id = c["channelId"] + string.Empty; channel.Thumbnails = c["thumbnail"]["thumbnails"].Select(t => t["url"] + string.Empty).ToArray(); channel.SubscriptionCount = c["subscriberCountText"]?["simpleText"] +string.Empty; channel.Name = c["title"]["simpleText"] + string.Empty; results.Add(channel); } yield return(new KeyValuePair <string, YoutubeChannelInfo[]>(title, results.ToArray())); } }
public static string Generate(YoutubeChannelInfo channel) { var querySaveChannel = $@" if not exists(select top(1) 1 from Channels where Id='{channel.Id}') INSERT INTO [dbo].[Channels] ([Id] ,[Name] ,[LogoUrl] ,[Keywords] ,[Description] ,[SubscriptionCount] ,[Thumbnails] ,[Tags] ,[UserId] ,[FromChannelId]) VALUES ('{channel.Id}' ,N'{channel.Name.Sqlize()}' ,N'{channel.LogoUrl.Sqlize()}' ,N'{channel.Keywords.Sqlize()}' ,N'{channel.Description.Sqlize()}' ,N'{channel.SubscriptionCount.Sqlize()}' ,N'{channel.Thumbnails.JoinWith().Sqlize()}' ,N'{channel.Tags.JoinWith().Sqlize()}' ,N'{channel.UserId.Sqlize()}' ,N'{channel.FromChannelId.Sqlize()}') else UPDATE [dbo].[Channels] SET [Name] = N'{channel.Name.Sqlize()}' ,[LogoUrl] = N'{channel.LogoUrl.Sqlize()}' ,[Keywords] = N'{channel.Keywords.Sqlize()}' ,[Description] = N'{channel.Description.Sqlize()}' ,[SubscriptionCount] = N'{channel.SubscriptionCount.Sqlize()}' ,[Thumbnails] = N'{channel.Thumbnails.JoinWith().Sqlize()}' ,[Tags] = N'{channel.Tags.JoinWith().Sqlize()}' ,[UserId] = N'{channel.UserId.Sqlize()}' ,[FromChannelId] =ISNULL(FromChannelId,'')+',{channel.FromChannelId.Sqlize()}' WHERE [Id] = '{channel.Id}'; "; var sb = new StringBuilder(); sb.AppendLine(querySaveChannel); if (channel.Uploads != null) { foreach (var videoInfo in channel.Uploads) { var query = Generate(videoInfo); sb.AppendLine(query); } } return(sb.ToString()); }
public static void Save(YoutubeChannelInfo channel) { if (channel == null) { return; } if (string.IsNullOrEmpty(channel.Id)) { return; } C.Write("saving... " + channel.Name + "\t"); var query = QueryGenerator.Generate(channel); Sql.Execute(query); C.WriteLine("saved ... "); }
public YoutubeChannelInfo GetChannelInfo(string channelId) { if (string.IsNullOrEmpty(channelId)) { return(null); } if (string.IsNullOrWhiteSpace(channelId)) { return(null); } var url = $"https://www.youtube.com/channel/{channelId}?hl=en"; var channel = new YoutubeChannelInfo(); channel.Id = channelId; var html = url.DownloadHTML(); channel.Name = html.GetStringBetweenAll("<title>", "</title>").FirstOrDefault(s => s != "YouTube"); var keywordsAll = html.GetStringBetweenAll("<meta name=\"keywords\" content=\"", "\">").ToArray(); channel.Keywords = keywordsAll.FirstOrDefault(s => !s.Contains("video, sharing, camera phone, video phone, free, upload")); channel.Description = html.GetStringBetween("<meta property=\"og:description\" content=\"", "\">"); channel.Tags = html.GetStringBetweenAll("<meta property=\"og:video:tag\" content=\"", "\">").ToArray(); channel.UserId = html.GetStringBetween("/user/", "\"") + string.Empty; if (channel.UserId.EndsWith("/featured")) { channel.UserId = channel.UserId.Replace("/featured", string.Empty); } var ytInitialData = html.GetYtInitialData(); var obj = JObject.Parse(ytInitialData); var secondaryContents = obj["contents"]?["twoColumnBrowseResultsRenderer"]?["secondaryContents"]?[ "browseSecondaryContentsRenderer"]?["contents"]? .Select(c => c["verticalChannelSectionRenderer"]["items"]).ToArray(); if (secondaryContents != null) { var friendChannels = secondaryContents?[0]?.Select(c => c["miniChannelRenderer"]) ?? new JToken[0]; channel.FriendChannels = new List <YoutubeChannelInfo>(); foreach (var friendChannel in friendChannels) { var innerChannel = new YoutubeChannelInfo(); innerChannel.Id = friendChannel["channelId"] + string.Empty; innerChannel.Name = friendChannel["title"]["runs"][0]["text"] + string.Empty; innerChannel.SubscriptionCount = friendChannel["subscriberCountText"]?["simpleText"] +string.Empty; if (string.IsNullOrEmpty(innerChannel.SubscriptionCount)) { innerChannel.SubscriptionCount = friendChannel["subscriberCountText"]?["runs"]?[0]?["text"] +string.Empty; } if (string.IsNullOrEmpty(innerChannel.SubscriptionCount)) { ; } innerChannel.Thumbnails = friendChannel["thumbnail"]["thumbnails"] .Select(c => c["url"] + string.Empty).ToArray(); channel.FriendChannels.Add(innerChannel); } if (secondaryContents.Length == 2) { var relatedChannels = secondaryContents[1].Select(c => c["miniChannelRenderer"]); channel.RelatedChannels = new List <YoutubeChannelInfo>(); foreach (var relatedChannel in relatedChannels) { var innerChannel = new YoutubeChannelInfo(); innerChannel.Id = relatedChannel["channelId"] + string.Empty; innerChannel.Name = relatedChannel["title"]["runs"][0]["text"] + string.Empty; innerChannel.SubscriptionCount = relatedChannel["subscriberCountText"]["simpleText"] + string.Empty; innerChannel.Thumbnails = relatedChannel["thumbnail"]["thumbnails"] .Select(c => c["url"] + string.Empty).ToArray(); channel.RelatedChannels.Add(innerChannel); } } } else { ; } var tabs = obj["contents"]?["twoColumnBrowseResultsRenderer"]?["tabs"]?.Select(c => c["tabRenderer"]) .Where(t => t != null).ToArray() ?? new JToken[0]; foreach (var tab in tabs) { var _title = tab["title"] + string.Empty; if (_title == "Home") { channel.Uploads = new List <YoutubeVideoInfo>(); var contents = tab["content"]["sectionListRenderer"]["contents"]; var items = contents.Select(c => c["itemSectionRenderer"]["contents"][0]) .Select(c => c["channelVideoPlayerRenderer"] ?? c["shelfRenderer"]) ; foreach (var channelVideoPlayerRenderer in items) { if (channelVideoPlayerRenderer == null) { continue; } var video = new YoutubeVideoInfo(); video.Id = channelVideoPlayerRenderer["videoId"] + string.Empty; video.Title = channelVideoPlayerRenderer["title"]?["runs"] ?.Select(c => c?["text"] +string.Empty) .JoinWith("\n"); if (channelVideoPlayerRenderer["description"] != null) { if (channelVideoPlayerRenderer["description"]["simpleText"] != null) { video.Description = channelVideoPlayerRenderer["description"]["simpleText"] + string.Empty; } else { video.Description = channelVideoPlayerRenderer["description"]["runs"] .Select(c => c["text"]) .JoinWith("\n"); } } video.ViewsCount = channelVideoPlayerRenderer["viewCountText"]?["simpleText"] +string.Empty; video.PublishedTime = channelVideoPlayerRenderer["publishedTimeText"]?["simpleText"] +string.Empty; channel.Uploads.Add(video); } } } return(channel); }