Skip to content

Instantly share code, notes, and snippets.

@mmstick
Created June 5, 2016 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmstick/0ee543ddb2f0a384c663c65890414d6d to your computer and use it in GitHub Desktop.
Save mmstick/0ee543ddb2f0a384c663c65890414d6d to your computer and use it in GitHub Desktop.
Printing a Numbered List of URLs
/// This example demonstrates how print a numbered list of URLs from the Ubuntu Kernel PPA,
/// which can be used to automate the installation of Ubuntu kernels.
extern crate hyper;
use hyper::Client;
use hyper::header::Connection;
extern crate select;
use select::document::Document;
use select::predicate::{Name};
use std::io::Read;
fn main() {
let input_url = "http://kernel.ubuntu.com/~kernel-ppa/mainline";
for url in parse_document(input_url, get_page(input_url).as_str()) {
println!(" {}. {}", url.id, url.url);
}
}
fn get_page(input_url: &str) -> String {
let client = Client::new();
let mut response = client.get(input_url)
.header(Connection::close()).send().unwrap();
let mut body = String::new();
response.read_to_string(&mut body).unwrap();
body
}
struct URL {
id: u16,
url: String,
}
fn parse_document(input_url: &str, document: &str) -> Vec<URL> {
let mut id = 1;
let mut output: Vec<URL> = Vec::new();
for node in Document::from(document).find(Name("a")).iter() {
if let Some(href) = node.attr("href") {
if href.starts_with('/') {
output.push(URL{id: id, url: String::from(input_url) + href});
} else if href.starts_with('#') {
output.push(URL{id: id, url: String::from(input_url) + "/"});
} else if href.starts_with('j') {
continue
} else if !href.starts_with("http") {
output.push(URL{id: id, url: String::from(input_url) + "/" + href});
} else {
output.push(URL{id: id, url: String::from(href)});
}
}
id += 1;
}
output
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment