use reqwest::Error;
#[derive(Debug, Clone)]
pub struct TwoCH<'a> {
prefix: String,
board: Option<&'a str>,
thread: Option<u32>,
}
impl<'a> TwoCH<'a> {
pub fn default() -> TwoCH<'a> {
TwoCH {
prefix: String::from("https://2ch.hk/"),
board: None,
thread: None,
}
}
pub fn board(mut self, board: Option<&'a str>) -> TwoCH<'a> {
self.board = board;
self
}
pub fn thread(mut self, thread: Option<u32>) -> TwoCH<'a> {
self.thread = thread;
self
}
pub fn res(&self) -> Result<(), Box<Error>> {
let url = format!(
"{}{}/res/{}.json",
self.prefix,
self.board.unwrap(),
self.thread.unwrap()
);
self.get(url);
Ok(())
}
pub fn thread_list(&self, thread: Option<u32>) -> Result<(), Box<Error>> {
let url = format!(
"{}{}/{}.json",
self.prefix,
self.board.unwrap(),
thread.unwrap()
);
self.get(url);
Ok(())
}
pub fn catalog(&self) -> Result<(), Box<Error>> {
self.get_catalog("catalog");
Ok(())
}
pub fn catalog_num(&self) -> Result<(), Box<Error>> {
self.get_catalog("catalog_num");
Ok(())
}
pub fn boards_all(&self) -> Result<(), Box<Error>> {
let url = format!("{}makaba/mobile.fcgi?task=get_boards", self.prefix,);
self.get(url);
Ok(())
}
pub fn posts_by_board(&self, num: u32) -> Result<(), Box<Error>> {
let url = format!(
"{}makaba/mobile.fcgi?task=get_thread&board={}&thread={}&num={}",
self.prefix,
self.board.unwrap(),
self.thread.unwrap(),
num
);
self.get(url);
Ok(())
}
pub fn posts_by_thread(&self, post: u32) -> Result<(), Box<Error>> {
let url = format!(
"{}makaba/mobile.fcgi?task=get_thread&board={}&thread={}&post={}",
self.board.unwrap(),
self.thread.unwrap(),
self.prefix,
post
);
self.get(url);
Ok(())
}
pub fn post_by_thread(&self, post: u32) -> Result<(), Box<Error>> {
let url = format!(
"{}makaba/mobile.fcgi?task=get_post&board={}&post={}",
self.prefix,
self.board.unwrap(),
post
);
self.get(url);
Ok(())
}
fn get_catalog(&self, access: &'a str) -> Result<(), Box<Error>> {
let url = format!("{}{}/{}.json", self.prefix, self.board.unwrap(), access);
self.get(url);
Ok(())
}
fn get(&self, url: String) -> Result<(), Box<Error>> {
let link = url.as_str();
let body = reqwest::get(link)?.text()?;
println!("{}", body);
Ok(())
}
}