// DoWork event handler for the file download BackgroundWorker. // The selected TreeNode (either a module, folder, or file) is passed in the DoWorkEventArgs e private void DownloadBW_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; TreeNode selectedNode = e.Argument as TreeNode; scraper.DownloadProgress.BeginJob(worker); // -- DETERMINE TYPE OF NODE SELECTED -- // Module selected if (selectedNode.Tag.GetType() == typeof(BbModule)) { BbModule module = selectedNode.Tag as BbModule; scraper.DownloadProgress.TotalWork = module.Content.CountAllFiles(); scraper.DownloadModuleFiles(module); } // Single folder selected else if (selectedNode.Tag.GetType() == typeof(BbContentDirectory)) { BbContentDirectory folder = selectedNode.Tag as BbContentDirectory; scraper.DownloadProgress.TotalWork = folder.CountAllFiles(); scraper.DownloadFolder(folder, scraper.OutputDirectory + BbUtils.CleanDirectory(folder.Name) + @"\"); } // Single file selected else if (selectedNode.Tag.GetType() == typeof(BbContentItem)) { BbContentItem file = selectedNode.Tag as BbContentItem; scraper.DownloadProgress.TotalWork = 1; scraper.DownloadFile(file); } }
// Return true if other BbContentDirectory has the same url public bool Equals(BbContentDirectory other) { if (other == null) { return(false); } return(this.url.Equals(other.Url)); }
public BbContentItem(string name, Uri url, BbContentDirectory folder, string linkType = "local") { this.name = name; this.url = url; this.linkType = linkType; this.folder = folder; filename = BbUtils.CleanFileName(name).Truncate(40); }
public BbContentDirectory(string name, Uri url, BbContentDirectory parentFolder) { this.url = url; this.name = name; this.folder = parentFolder; subFolders = new List <BbContentDirectory>(); files = new List <BbContentItem>(); }
public BbContentItem(string name, Uri url, BbContentDirectory folder, string linkType="local") { this.name = name; this.url = url; this.linkType = linkType; this.folder = folder; filename = BbUtils.CleanFileName(name).Truncate(40); }
public BbContentDirectory(string name, Uri url, BbContentDirectory parentFolder) { this.url = url; this.name = name; this.folder = parentFolder; subFolders = new List<BbContentDirectory>(); files = new List<BbContentItem>(); }
// Downloads all files in folders. Used recursively for each subfolder found. public void DownloadFolder(BbContentDirectory folder, string directory) { foreach (BbContentItem file in folder.Files) { DownloadFile(file, directory); } foreach (BbContentDirectory subFolder in folder.SubFolders) { DownloadFolder(subFolder, directory + BbUtils.CleanDirectory(subFolder.Name) + "\\"); //Add subfolder name to directory } }
public static int GetViewChoice(BbContentDirectory folder) { bool parsed; int choice; parsed = Int32.TryParse(Console.ReadLine(), out choice); while (!parsed || choice < -1 || choice > folder.SubFolders.Count + folder.Files.Count) { Console.WriteLine("Invalid option. Enter the number of your choice"); parsed = Int32.TryParse(Console.ReadLine(), out choice); } return(choice); }
// Populates content in a learning unit, which is like a folder but with a tree-like navigation // and content is generally displayed in iframes to the right. public void PopulateLearningUnit(BbContentDirectory folder) { string pageSource = http.DownloadString(folder.Url.AbsoluteUri); // Get the link to the next content item in a learning unit by following the next arrow link // until there are no more left. Uri nextURL = folder.Url; HtmlNode nextLink; while (nextURL != null) { populateProgress.AppendStatus("."); string contentSource = http.DownloadString(nextURL); List <HtmlNode> contentLinks = HTMLParser.GetLearningUnitContent(contentSource); // for each content link found, add a file. Usually only one foreach (HtmlNode link in contentLinks) { Uri contentURL = new Uri(folder.Url, link.Attributes["href"].Value); string linkType = HTMLParser.GetLinkType(contentURL); string linkName = HTMLParser.GetLinkText(link); if (linkName == "DefaultText") { linkName = HTMLParser.GetPageTitle(contentSource); } folder.AddFile(new BbContentItem(linkName, contentURL, folder, linkType)); } if (contentLinks.Count == 0) // if no content links found, look for content source in iFrame { string iFrameLink = HTMLParser.GetLearningUnitIFrame(contentSource); if (iFrameLink != null) // if iframe found { Uri contentURL = new Uri(folder.Url, iFrameLink); string linkType = HTMLParser.GetLinkType(contentURL); string linkName = HTMLParser.GetPageTitle(contentSource); folder.AddFile(new BbContentItem(linkName, contentURL, folder, linkType)); } } nextLink = HTMLParser.GetNextLearningUnitContent(contentSource); if (nextLink == null) { nextURL = null; } else { nextURL = new Uri(folder.Url, nextLink.Attributes["href"].Value); } } }
// Event Handler for the content treeview. Changes information displayed private void contentTree_AfterSelect(object sender, TreeViewEventArgs e) { ClearInfoBox(); // Change what's displayed in the Information Box depending on type of node selected // File selected if (contentTree.SelectedNode.Tag.GetType() == typeof(BbContentItem)) { BbContentItem file = contentTree.SelectedNode.Tag as BbContentItem; infoLabel1.Text = "Name"; infoText1.Text = file.Name; infoLabel2.Text = "Filename"; infoText2.Text = file.Filename; infoLabel3.Text = "Link Type"; infoText3.Text = file.LinkType; infoLabel4.Text = "URL"; infoTextLink.Text = file.Url.AbsoluteUri; } // Folder selected else if (contentTree.SelectedNode.Tag.GetType() == typeof(BbContentDirectory)) { BbContentDirectory folder = contentTree.SelectedNode.Tag as BbContentDirectory; infoLabel1.Text = "Name"; infoText1.Text = folder.Name; infoLabel2.Text = "Files"; infoText2.Text = folder.CountAllFiles().ToString(); infoLabel3.Text = "Subfolders"; infoText3.Text = folder.SubFolders.Count.ToString(); infoLabel4.Text = "URL"; infoTextLink.Text = folder.Url.AbsoluteUri; } // Module selected else if (contentTree.SelectedNode.Tag.GetType() == typeof(BbModule)) { BbModule module = contentTree.SelectedNode.Tag as BbModule; infoLabel1.Text = "Name"; infoText1.Text = module.Name; infoLabel2.Text = "Files"; infoText2.Text = module.Content.CountAllFiles().ToString(); infoLabel3.Text = "Subfolders"; infoText3.Text = module.Content.SubFolders.Count.ToString(); infoLabel4.Text = "URL"; infoTextLink.Text = module.Url.AbsoluteUri; } }
// Adds given folder to the content TreeView. Adds folder's node to parent node. private void PopulateTreeFolder(TreeNode parent, BbContentDirectory folder) { TreeNode folderNode = new TreeNode(folder.Name); folderNode.Tag = folder; folderNode.ImageIndex = 2; folderNode.SelectedImageIndex = 2; foreach (BbContentDirectory subFolder in folder.SubFolders) { PopulateTreeFolder(folderNode, subFolder); } foreach (BbContentItem file in folder.Files) { AddTreeFile(folderNode, file); } parent.Nodes.Add(folderNode); }
// Used recursively to populate all subfolders of a module public void PopulateContentDirectory(BbContentDirectory folder) { string pageSource = http.DownloadString(folder.Url.AbsoluteUri); List <HtmlNode> contentLinks = HTMLParser.GetContentLinks(pageSource); if (contentLinks == null) { return; } foreach (HtmlNode link in contentLinks) { //Console.WriteLine("Adding " + folder.Name + ": " + link.InnerText); populateProgress.AppendStatus("."); Uri linkURL = new Uri(folder.Url, link.Attributes["href"].Value); if (HTMLParser.IsSubFolder(link)) // content is a subfolder { BbContentDirectory subFolder = new BbContentDirectory(link.InnerText, linkURL, folder); if (!folder.SubFolders.Contains(subFolder)) { folder.AddSubFolder(subFolder); } PopulateContentDirectory(subFolder); } else if (HTMLParser.IsLearningUnit(link)) //content is a learning unit { BbContentDirectory subFolder = new BbContentDirectory(link.InnerText, linkURL, folder); if (!folder.SubFolders.Contains(subFolder)) { folder.AddSubFolder(subFolder); } PopulateLearningUnit(subFolder); } else // content is a file { string linkType = HTMLParser.GetLinkType(linkURL); BbContentItem newFile = new BbContentItem(link.InnerText, linkURL, folder, linkType); if (!folder.Files.Contains(newFile)) { folder.AddFile(newFile); } } } }
public static void ViewDirectory(BbContentDirectory folder) { Console.WriteLine(); Console.WriteLine("VIEWING DIRECTORY: " + folder.Name); Console.WriteLine("------------------------------------------------------------------"); for (int i = 0; i < folder.SubFolders.Count; i++) { Console.WriteLine(i + 1 + ". Directory: " + folder.SubFolders[i].Name); } for (int i = 0; i < folder.Files.Count; i++) { Console.WriteLine(i + 1 + folder.SubFolders.Count + ". File: " + folder.Files[i].Name); } Console.WriteLine("0. Back"); Console.WriteLine("-1. Quit"); Console.Write(">> "); int choice = GetViewChoice(folder); if (choice == -1) { return; } else if (choice == 0) { if (folder.ParentFolder != null) { ViewDirectory(folder.ParentFolder); } else { ViewContent(); } } else if (choice <= folder.SubFolders.Count) { ViewDirectory(folder.SubFolders[choice - 1]); } else { ViewFile(folder.Files[choice - folder.SubFolders.Count - 1]); } }
// Add a sub-folder to this directory public void AddSubFolder(BbContentDirectory s) { subFolders.Add(s); }
// Downloads all files in folders. Used recursively for each subfolder found. public void DownloadFolder(BbContentDirectory folder, string directory) { foreach(BbContentItem file in folder.Files) { DownloadFile(file, directory); } foreach(BbContentDirectory subFolder in folder.SubFolders) { DownloadFolder(subFolder, directory + BbUtils.CleanDirectory(subFolder.Name) + "\\"); //Add subfolder name to directory } }
// Used recursively to populate all subfolders of a module public void PopulateContentDirectory(BbContentDirectory folder) { string pageSource = http.DownloadString(folder.Url.AbsoluteUri); List<HtmlNode> contentLinks = HTMLParser.GetContentLinks(pageSource); if (contentLinks == null) return; foreach (HtmlNode link in contentLinks) { //Console.WriteLine("Adding " + folder.Name + ": " + link.InnerText); populateProgress.AppendStatus("."); Uri linkURL = new Uri(folder.Url, link.Attributes["href"].Value); if (HTMLParser.IsSubFolder(link)) // content is a subfolder { BbContentDirectory subFolder = new BbContentDirectory(link.InnerText, linkURL, folder); if (!folder.SubFolders.Contains(subFolder)) { folder.AddSubFolder(subFolder); } PopulateContentDirectory(subFolder); } else if (HTMLParser.IsLearningUnit(link)) //content is a learning unit { BbContentDirectory subFolder = new BbContentDirectory(link.InnerText, linkURL, folder); if (!folder.SubFolders.Contains(subFolder)) { folder.AddSubFolder(subFolder); } PopulateLearningUnit(subFolder); } else // content is a file { string linkType = HTMLParser.GetLinkType(linkURL); BbContentItem newFile = new BbContentItem(link.InnerText, linkURL, folder, linkType); if (!folder.Files.Contains(newFile)) { folder.AddFile(newFile); } } } }
// Populates content in a learning unit, which is like a folder but with a tree-like navigation // and content is generally displayed in iframes to the right. public void PopulateLearningUnit(BbContentDirectory folder) { string pageSource = http.DownloadString(folder.Url.AbsoluteUri); // Get the link to the next content item in a learning unit by following the next arrow link // until there are no more left. Uri nextURL = folder.Url; HtmlNode nextLink; while (nextURL != null) { populateProgress.AppendStatus("."); string contentSource = http.DownloadString(nextURL); List<HtmlNode> contentLinks = HTMLParser.GetLearningUnitContent(contentSource); // for each content link found, add a file. Usually only one foreach (HtmlNode link in contentLinks) { Uri contentURL = new Uri(folder.Url, link.Attributes["href"].Value); string linkType = HTMLParser.GetLinkType(contentURL); string linkName = HTMLParser.GetLinkText(link); if (linkName == "DefaultText") { linkName = HTMLParser.GetPageTitle(contentSource); } folder.AddFile(new BbContentItem(linkName, contentURL, folder, linkType)); } if (contentLinks.Count == 0) // if no content links found, look for content source in iFrame { string iFrameLink = HTMLParser.GetLearningUnitIFrame(contentSource); if (iFrameLink != null) // if iframe found { Uri contentURL = new Uri(folder.Url, iFrameLink); string linkType = HTMLParser.GetLinkType(contentURL); string linkName = HTMLParser.GetPageTitle(contentSource); folder.AddFile(new BbContentItem(linkName, contentURL, folder, linkType)); } } nextLink = HTMLParser.GetNextLearningUnitContent(contentSource); if (nextLink == null) { nextURL = null; } else { nextURL = new Uri(folder.Url, nextLink.Attributes["href"].Value); } } }
public static void ViewDirectory(BbContentDirectory folder) { Console.WriteLine(); Console.WriteLine("VIEWING DIRECTORY: " + folder.Name); Console.WriteLine("------------------------------------------------------------------"); for (int i = 0; i < folder.SubFolders.Count; i++) { Console.WriteLine(i + 1 + ". Directory: " + folder.SubFolders[i].Name); } for (int i = 0; i < folder.Files.Count; i++) { Console.WriteLine(i + 1 + folder.SubFolders.Count + ". File: " + folder.Files[i].Name); } Console.WriteLine("0. Back"); Console.WriteLine("-1. Quit"); Console.Write(">> "); int choice = GetViewChoice(folder); if (choice == -1) { return; } else if (choice == 0) { if (folder.ParentFolder != null) { ViewDirectory(folder.ParentFolder); } else { ViewContent(); } } else if (choice <= folder.SubFolders.Count) { ViewDirectory(folder.SubFolders[choice-1]); } else { ViewFile(folder.Files[choice - folder.SubFolders.Count - 1]); } }
public static int GetViewChoice(BbContentDirectory folder) { bool parsed; int choice; parsed = Int32.TryParse(Console.ReadLine(), out choice); while (!parsed || choice < -1 || choice > folder.SubFolders.Count + folder.Files.Count) { Console.WriteLine("Invalid option. Enter the number of your choice"); parsed = Int32.TryParse(Console.ReadLine(), out choice); } return choice; }
// Return true if other BbContentDirectory has the same url public bool Equals(BbContentDirectory other) { if (other == null) { return false; } return this.url.Equals(other.Url); }
// Creates the main content directory. Url is the address of the main directory on Blackboard. public void InitContentDirectory(Uri url) { content = new BbContentDirectory(name + " Content", url, null); initialized = true; }