Created
August 29, 2019 05:41
-
-
Save xentec/3bbb4d05abd62e15108ccb039476cb57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use reqwest; | |
use scraper::{Html, Selector}; | |
use structopt::{self, StructOpt}; | |
#[derive(StructOpt)] | |
#[structopt(name = "w0bm-sav0r")] | |
struct Opt | |
{ | |
#[structopt()] | |
username: String, | |
} | |
fn main() | |
{ | |
let opt = Opt::from_args(); | |
let client = reqwest::Client::builder() | |
.timeout(std::time::Duration::from_secs(10)) | |
.gzip(true) | |
.build() | |
.expect("failed to build http client"); | |
let mut fav_count = 0usize; | |
let mut page_num = 1usize; | |
let sel_fav_id = Selector::parse("tbody > tr[data-thumb]").expect("failed to generate HTML selector"); | |
loop { | |
let mut req = client.get(&format!("https://w0bm.com/user/{}/favs/index", opt.username)) | |
.query(&[("page", page_num)]); | |
if let Ok(key) = std::env::var("W0BM_SESSION") { | |
req = req.header(reqwest::header::COOKIE, format!("w0bm_session={}", key)); | |
} | |
let page = req.send() | |
.and_then(|res| res.error_for_status()) | |
.and_then(|mut res| res.text()) | |
.expect("failed to fetch webpage: {}"); | |
eprintln!("# page: {}", page_num); | |
let mut fav_count_page = 0usize; | |
Html::parse_document(&page) | |
.select(&sel_fav_id) | |
.filter_map(|elem| elem.value().attr("data-thumb")) | |
.for_each(|id| { | |
fav_count_page += 1; | |
println!("https://w0bm.com/b/{}.webm", id) | |
}); | |
if fav_count_page == 0 { | |
break; // we're behind last page | |
} | |
fav_count += fav_count_page; | |
page_num += 1; | |
} | |
if fav_count == 0 { | |
eprintln!("Failed to list favorites. Does {} have any? Is W0BM_SESSION environement variable set?", opt.username); | |
} else { | |
eprintln!("# Fetched {} fav links", fav_count); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment