Skip to content

Instantly share code, notes, and snippets.

@tmcw
Created November 5, 2017 20:51
Show Gist options
  • Save tmcw/274b187334a24e2fcc309d3387a8efe7 to your computer and use it in GitHub Desktop.
Save tmcw/274b187334a24e2fcc309d3387a8efe7 to your computer and use it in GitHub Desktop.
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate reqwest;
use std::env;
use std::fs::File;
use std::io::prelude::*;
use regex::*;
use reqwest::Client;
use reqwest::Error;
fn get_redirected_url(url: &str) -> Option<String> {
Client::new()
.head(url)
.send()
.map(|resp| resp.url().as_str().to_string())
.ok()
}
fn main() {
let filename = "../tmcw.github.com/_posts/2017-07-01-recently.md";
let mut f = File::open(filename).expect("file not found");
let mut contents = String::new();
f.read_to_string(&mut contents).expect(
"something went wrong reading the file",
);
lazy_static! {
static ref AMZN_RE: Regex = Regex::new(r"https?://amzn.to/([0-9A-Za-z]+)").unwrap();
static ref ISBN1: Regex = Regex::new(
r"https://www.amazon.com/(?:[A-Za-z\-]+)/dp/(\d{10})/").unwrap();
static ref ISBN2: Regex = Regex::new(
r"https://www.amazon.com/gp/product/(\d{10})/").unwrap();
}
let captures: Vec<_> = AMZN_RE.captures_iter(&contents).collect();
for cap in captures {
get_redirected_url(cap.get(0).unwrap().as_str()).and_then(|redirected_to| {
let c1 = ISBN1.captures(&redirected_to);
// let c2 = ISBN2.captures(&redirected_to);
c1
});
// .and_then(|capture| {
// let isbn = capture.get(1).unwrap().as_str();
// let worldcat_url = format!("http://www.worldcat.org/isbn/{}", isbn);
// get_redirected_url(&worldcat_url).map(|worldcat_permalink| {
// println!("worldcat permalink:\n{:?}", worldcat_permalink);
// // contents =
// // contents.replace(capture.get(0).unwrap().as_str(), &worldcat_permalink);
// })
// });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment