Skip to content

Commit

Permalink
feat: add forumURL struct/impl
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonVirgo committed Aug 3, 2024
1 parent 4aa255a commit 8e7dda8
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
8 changes: 3 additions & 5 deletions src/routes/api/dashboard/vote_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ components::buttons::{gen_button, ButtonType, FormSubmitButton}, models::votes::get_votes, scraping::scraper, utils::app_state::AppState};
use crate::{ components::buttons::{gen_button, ButtonType, FormSubmitButton}, models::votes::get_votes, scraping::scraper, utils::{app_state::AppState, url::ForumURL}};
use actix_web::{get, post, web::{self, Data}, HttpResponse, Responder};
use maud::{html, Markup};

Expand Down Expand Up @@ -88,11 +88,9 @@ async fn vote_data(state: Data<AppState>, path: web::Path<String>) -> impl Respo
#[post("/votes/{thread_id}")]
async fn scrape_votes(_: Data<AppState>, path: web::Path<String>) -> impl Responder {
let thread_id = path.into_inner();
let full_uri = format!("https://forum.mafiascum.net/viewtopic.php?t={}", thread_id);

println!("{}", full_uri);

let page_data = match scraper::get_page_details(full_uri).await {
let url = ForumURL::new(thread_id);
let page_data = match url.scrape().await {
Some(page_data) => page_data,
None => {
println!("Failed to get page data");
Expand Down
25 changes: 6 additions & 19 deletions src/routes/api/search_or_register_thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::scraping::{
scraper::get_page_details,
};
use crate::utils::app_state::AppState;
use crate::utils::url::ForumURL;
use actix_web::{
post,
web::{self, Data},
Expand All @@ -22,35 +23,21 @@ async fn search_or_register_thread(
form: web::Form<FormData>,
) -> impl Responder {
let query_search_params = get_search_params(&form.url);
let raw_url = match (query_search_params.get("t"), query_search_params.get("p")) {
(Some(thread_id), _) => get_url_from_type(
URLType::Thread(ThreadURL {
thread_id: thread_id.to_string(),
}),
PageType::Thread,
),
(None, Some(post_id)) => get_url_from_type(
URLType::Post(PostURL {
post_id: post_id.to_string(),
}),
PageType::Thread,
),
_ => None,
};

let url = match raw_url {
None => {
let url = match (query_search_params.get("t"), query_search_params.get("p")) {
(Some(thread_id), _) => ForumURL::new(thread_id.to_string()),
(None, Some(post_id)) => ForumURL::new_from_post(post_id.to_string()),
_ => {
return HttpResponse::BadRequest().body(
html! {
div."text-red-500" { "Invalid URL" }
}
.into_string(),
)
}
Some(url) => url,
};

let page_data = match get_page_details(url.clone()).await {
let page_data = match url.scrape().await {
Some(page) => page,
None => {
return HttpResponse::BadRequest().body(
Expand Down
1 change: 1 addition & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod app_state;
pub mod string_similarity;
pub mod url;
38 changes: 38 additions & 0 deletions src/utils/url.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use crate::scraping::scraper::{self, PageData};

pub struct ForumURL {
thread_id: String,
ppp: i32,
start: i32,
}

pub enum URLType {
Thread,
}

impl ForumURL {
pub fn new(thread_id: String) -> ForumURL {
ForumURL {
thread_id: thread_id,
ppp: 200,
start: 0,
}
}

pub fn new_from_post(post_id: String) -> ForumURL {
ForumURL::new("thread_url".to_string())
}

pub fn url(&self, url_type: URLType) -> String {
match url_type {
URLType::Thread => format!(
"https://forum.mafiascum.com/t/{}/?ppp={}&start={}",
self.thread_id, self.ppp, self.start
),
}
}

pub async fn scrape(&self) -> Option<PageData> {
scraper::get_page_details(self.url(URLType::Thread)).await
}
}

0 comments on commit 8e7dda8

Please sign in to comment.