Skip to content

Commit

Permalink
feat: add votes to get_page_details
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonVirgo committed Aug 2, 2024
1 parent 0efc64b commit 490b86f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 7 deletions.
1 change: 1 addition & 0 deletions src/routes/api/dashboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub fn init(cfg: &mut actix_web::web::ServiceConfig) {
cfg.service(player_edit::player_edit);

cfg.service(vote_data::vote_data);
cfg.service(vote_data::scrape_votes);

cfg.service(setup_data::setup_data);
cfg.service(setup_data::submit_setup_data);
Expand Down
32 changes: 27 additions & 5 deletions src/routes/api/dashboard/vote_data.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{components::buttons::{gen_button, ButtonType, FormSubmitButton}, AppState};
use actix_web::{get, web::{self, Data}, HttpResponse, Responder};
use crate::{components::buttons::{gen_button, ButtonType, FormSubmitButton}, scraping::scraper, AppState};
use actix_web::{get, post, web::{self, Data}, HttpResponse, Responder};
use maud::{html, Markup};

struct TableRow {
Expand All @@ -22,13 +22,14 @@ fn format_table_row(row: TableRow) -> Markup {
}

#[get("/votes/{thread_id}")]
async fn vote_data(_: Data<AppState>, _: web::Path<String>) -> impl Responder {
async fn vote_data(_: Data<AppState>, path: web::Path<String>) -> impl Responder {
let thread_id = path.into_inner();
HttpResponse::Ok().body(
html! {
div."w-full h-full flex flex-col p-4" {
div."w-full h-full flex flex-col p-4" id="vote-wrapper" {
h1."text-3xl text-white font-bold pb-2" { "Player Data" }
div."text-xl text-white pb-2" { "Enter the data for the players in the game" }
form."flex flex-col pb-2 mb-2" {
form."flex flex-col pb-2 mb-2" hx-post=(format!("/api/dashboard/votes/{}", thread_id)) hx-target="#vote-wrapper" hx-swap="outerHTML" {
(gen_button(ButtonType::FormSubmit(FormSubmitButton {
text: "Scrape Votes".to_string(),
})))
Expand Down Expand Up @@ -66,3 +67,24 @@ async fn vote_data(_: Data<AppState>, _: web::Path<String>) -> impl Responder {
.into_string(),
)
}

#[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 {
Some(page_data) => page_data,
None => {
println!("Failed to get page data");
return HttpResponse::Found().insert_header(("HX-Redirect", format!("/dashboard/{}?d=2", thread_id))).finish()
}
};

println!("{:?}", page_data);

HttpResponse::Found()
.insert_header(("HX-Redirect", format!("/dashboard/{}?d=2", thread_id))).finish()
}
63 changes: 61 additions & 2 deletions src/scraping/scraper.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chrono::NaiveDateTime;
use reqwest::Client;
use select::document::Document;
use select::predicate::Name;
use select::predicate::{Attr, Class, Name, Or, Predicate};

use crate::scraping::parser::get_search_params;

Expand All @@ -9,6 +10,14 @@ pub struct PageData {
// pub title: String,
// pub url: String,
pub thread_id: String,
pub votes: Vec<Vote>,
}

#[derive(Debug)]
pub struct Vote {
pub author: String,
pub target: String,
pub post_number: i32,
}

pub async fn get_page_details(url: String) -> Option<PageData> {
Expand Down Expand Up @@ -44,8 +53,58 @@ pub async fn get_page_details(url: String) -> Option<PageData> {
None => (),
};

let votes: Vec<Vote> = Vec::new();

document.find(Class("post")).for_each(|node| {
let votes: Vec<String> = node
.find(Or(Class("bbvote"), Name("div").and(Attr("style", ()))))
.map(|node| node.text())
.filter(|text| text.to_lowercase().starts_with("vote:"))
.collect();
if votes.len() > 0 {
let author: Option<String> =
match node.find(Class("username")).collect::<Vec<_>>().first() {
Some(node) => Some(node.text()),
_ => {
match node
.find(Class("username-coloured"))
.collect::<Vec<_>>()
.first()
{
Some(node) => Some(node.text()),
_ => None,
}
}
};

let post_number = match node
.find(Class("post-number-bolded"))
.collect::<Vec<_>>()
.first()
{
Some(node) => {
let remove_first_char = node.text().chars().skip(1).collect::<String>();
match remove_first_char.parse::<i32>() {
Ok(num) => Some(num),
_ => None,
}
}
_ => None,
};

match (author, post_number) {
(Some(author), Some(post_number)) => {
for vote in votes {
println!("{} voted {} in post {}", author, vote, post_number);
}
}
_ => (),
}
}
});

match thread_id {
Some(thread_id) => Some(PageData { thread_id }),
Some(thread_id) => Some(PageData { thread_id, votes }),
_ => None,
}
}

0 comments on commit 490b86f

Please sign in to comment.