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