CIS 700 Downloader
David Matuszek, Summer II, 2010
Given a URL, download all web pages (.htm and .html), and images (.gif, .jpg, and .png) from that site. Recreate the site directory structure on your computer. Do not download files of any other type, or any offsite files.
Here's a program structure that I think will work fairly well. Feel free to deviate from it, as long as you are using Actors in a reasonably appropriate fashion.
DownloadManager actorjava.util.concurrent.LinkedBlockingQueue) of URLs to be processed.Downloaders, and give them access to the queue of URLs to be processed.Parser. while(true) loop to receive messages from the Parser.
Parser should have the form (URLthatWasParsed, List[newURLs]).For each URL gotten (from the queue of URLs to be processed):
Parser. For each page received (from the Downloader):
List of appropriate URLs to explore. DownloadManager.HTML is what web pages are written in.
HTML contains elements; each element consists of a tag with zero or more attributes, enclosed in angle brackets. Tags and attributes are not case sensitive. Attributes may occur in any order. An attribute consists of a name, an equals sign, and a value. The value is supposed to be in quotes, either single or double, but often isn't. When a value isn't in quotes, it is terminated by a blank or by the closing angle bracket.
Example: <TABLE summary="" class='data' Border = 1 cellSpacing="0">
where
"table" is the tag, and "summary", "class", "border", and "cellspacing" are the names of the attributes.
The relevant tags and attributes are:
a, usually denoting a web page, with a possible href attribute.
a tag has no href attribute, it should be ignored.a tag has an href attribute, the value of the href attribute is some kind of URL.img, denoting an image, with a "required" src attribute
src attribute is some kind of URL.There are several kinds of URLs:
http:, it is (probably) an offsite link, and should be ignored.# character, it is a link to the middle of some page, and should be ignored.? character, it includes parameters (e.g. for searching), and should be ignored... or /), it is a link to somewhere on the same site, but not in this subdirectory; ignore it.name1/.../nameNIf you need to know more, visit w3schools.
You may make the following simplifying (though incorrect) assumptions:
a tag or one img tag.These assumptions are intended to minimize the amount of string manipulation required, especially for students who don't know regular expressions. Feel free to do a better job handling HTML elements, but you will not get any extra points for doing so.
You should read and understand About /robots.txt before trying out your program. Following that, you should look at the robots.txt file for the university. If you don't obey the rules there, you may get Penn IP addresses banned, which in turn may get you into trouble with the university.
I will not require your program to read the robots.txt file, but you should read it and not break the rules.
A relatively safe place to try out your robot is any one of my old course pages at Penn. You can find a list of these at HouseOfChaos.com. Any one of these courses is probably a good size to use for testing (except CIS700, which uses a wiki instead), but you probably don't want to download material from all 32 courses.
| Reading from a URL | val url = new java.net.URL(from) |
| Writing to a file | val file = new java.io.File(to) |
| Choosing a directory | val chooser = new FileChooser() |
| Creating a directory | val newDirectory = new File(fullPathName) |
| Regular expression to match a link to an HTML page | val HtmlPattern = |
| Regular expression to match a link to an image | val ImgPattern = |
| Using the regular expression to get the tag and URL. | string match { |
The program should work as specified.
For up to 15 points extra credit, give the user the ability to stop all the actors (by sending them an appropriate message) and quit the program cleanly, if the user types q or Q.
No mechanical deductions or bonuses for vars, etc. However, we reserve the right to give bonuses or deductions according to our subjective estimates of the quality of your code, especially the extent to which it embraces or violates "the Scala way." Please note that, in this context, "subjective" means we liked it or didn't like it, and as such is not open to arguments about points.