/// <summary> /// Generate video link /// Note: /// 1. UserAgent (MSIE10) and Referrer must be set to the downloader, otherwise server will return 500 /// 2. Exception may be raised if something goes wrong (e.g. no video found) /// </summary> /// <param name="contentUrl"></param> /// <returns></returns> private async Task <List <string> > GenerateGeneralVideoLink(string contentUrl) { // BGetCore library related stuff var videoInfoCrawler = new VideoInfoCrawler(); var videoInfo = await videoInfoCrawler.GetVideoInfo(contentUrl); var videoUrlGrabber = new VideoUrlCrawler(); var videoUrl = await videoUrlGrabber.GetUrlBySingleContentId(videoInfo); var urlList = new List <string>(); // Just a practice of LINQ... if (Settings.Default.PreferFlv) { videoUrl.Durl.ForEach(url => urlList.Add(url.Url)); } else { urlList.AddRange( videoUrl.Durl.Where(url => url.BackupUrl != null && url.BackupUrl.Url.Count != 0) .SelectMany(url => url.BackupUrl.Url)); } return(urlList); }
private static void Main(string[] args) { Console.Write("[INFO] Enter your video ID, e.g. av349183: "); var inputVideo = Console.ReadLine(); Console.WriteLine("[INFO] Please wait..."); var videoInfoCrawler = new VideoInfoCrawler(); var videoInfo = videoInfoCrawler.GetVideoInfo(inputVideo).Result; var cid = videoInfo.ContentId; Console.WriteLine("[DEBUG] CID is " + cid); Console.WriteLine("[INFO] Title: " + videoInfo.Title); Console.WriteLine("[INFO] Author: " + videoInfo.Author); Console.WriteLine("[INFO] Description: " + videoInfo.Description + "\n\n\n"); var videoUrlGrabber = new VideoUrlCrawler(); var videoUrl = videoUrlGrabber.GetUrlBySingleContentId(videoInfo).Result; Console.WriteLine("[INFO] Got {0} video sections.", videoUrl.Durl.Count); foreach (var url in videoUrl.Durl) { Console.WriteLine("[INFO] Flash video URL: {0}", url.Url); if (url.BackupUrl != null && url.BackupUrl.Url.Count != 0) { foreach (var mp4Url in url.BackupUrl.Url) { Console.WriteLine("[INFO] MP4 backup video URL: {0}", mp4Url); } } } Console.Read(); }
public BgetVideoInfoTest() { videoInfoCrawler = new VideoInfoCrawler(); }
public BgetVideoUrlTest() { _videoUrlCrawler = new VideoUrlCrawler(); _videoInfoCrawler = new VideoInfoCrawler(); }
public async Task <List <VideoInfo> > GetAllVideoInfoByKeyword(string keyword, bool deepSearch = false) { // Set initial page from page 1 var pageCount = 1; var videoInfoList = new List <VideoInfo>(); var searchResult = await SearchByKeyword(keyword, SearchType.Video, pageCount); // "Iterate" the pages while (searchResult.IsSuccessful) { // Parse HTML content var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(searchResult.RawHtml); // Get Video URLs and titles var videoTitleAndUrlNodes = htmlDoc.DocumentNode.SelectNodes("//a[@class=\"title\"]"); // Deep search will (wayyyyyyy much) slower the fetching speed, so it's not recommend unless tag is required. if (deepSearch) { var videoInfoCrawler = new VideoInfoCrawler(); // The URL does not have its initial "https:", instead it starts with "//www.bilibili.com". // As a result, append a "https:" before it before further crawling. foreach (var singleNode in videoTitleAndUrlNodes) { var url = string.Format("https:{0}", singleNode.Attributes["href"].Value); videoInfoList.Add(await videoInfoCrawler.GetVideoInfo(url)); } } else { // Get video descriptions var videoDescriptionNodes = htmlDoc.DocumentNode.SelectNodes("//div[@class=\"des hide\"]"); // Get video uploaders var videoUploaderNodes = htmlDoc.DocumentNode.SelectNodes("//a[@class=\"up-name\"]"); // Iterate all video info from result (without tags) videoInfoList.AddRange(videoDescriptionNodes.Select((t, videoCountIndex) => new VideoInfo { // Extract author ID from URL Author = Regex.Match(videoUploaderNodes[videoCountIndex].Attributes["href"].Value, @"\/(\d+)\?").Groups[1].Value, // Extract description, new lines and tabs are removed Description = Regex.Match(t.InnerText, "[^\t|\n|\r]+").Value, // Extract video URL VideoPage = string.Format("https:{0}", videoTitleAndUrlNodes[videoCountIndex].Attributes["href"].Value), // Extract video ID ContentId = Regex.Match(videoTitleAndUrlNodes[videoCountIndex].Attributes["href"].Value, @"\/av(\d+)\?").Groups[1].Value, // Extract title Title = videoTitleAndUrlNodes[videoCountIndex].Attributes["title"].Value })); } // Update page count and move to next page... searchResult = await SearchByKeyword(keyword, SearchType.Video, ++pageCount); } return(videoInfoList); }