feat: get users and comments

This commit is contained in:
Aravinth Manivannan 2023-08-11 12:33:21 +05:30
commit 6ab53c5b22
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
6 changed files with 1196 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/target

1107
Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

12
Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "hn"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
reqwest = { version = "0.11.18", features = ["json", "gzip"] }
serde = { version = "1.0.183", features = ["derive"] }
serde_json = "1.0.104"
tokio = { version = "1.30.0", features = ["rt", "rt-multi-thread", "macros"] }

38
src/comments.rs Normal file
View file

@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use reqwest::Client;
use serde::Serialize;
use serde::Deserialize;
use crate::user::User;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub enum ChildComment {
Comment(Box<Comment>),
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Comment {
by: String,
id: usize,
kids: Option<Vec<usize>>,
#[serde(rename(deserialize = "type"))]
item_type: String,
parent: Option<usize>,
time: usize,
}
pub async fn get_user_comments(c: &Client, u:&User) -> Vec<Comment> {
let mut comments = Vec::default();
for item in u.submitted.iter() {
let comment: Comment = c.get(&format!("https://hacker-news.firebaseio.com/v0/item/{item}.json")).send().await.unwrap().json().await.unwrap();
if comment.item_type == "comment" {
comments.push(comment);
}
}
comments
}

19
src/main.rs Normal file
View file

@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use reqwest::Client;
mod user;
mod comments;
use user::*;
use comments::*;
#[tokio::main]
async fn main() {
let c = Client::default();
let user = get_user(&c, "realaravinth").await;
let comments = get_user_comments(&c, &user).await;
println!("{:#?}", comments);
}

19
src/user.rs Normal file
View file

@ -0,0 +1,19 @@
// SPDX-FileCopyrightText: 2023 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later
use reqwest::Client;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct User {
pub created: u128,
pub id: String,
pub about: Option<String>,
pub karma: usize,
pub submitted: Vec<usize>,
}
pub async fn get_user(c: &Client, username: &str) -> User {
c.get(&format!("https://hacker-news.firebaseio.com/v0/user/{username}.json")).send().await.unwrap().json().await.unwrap()
}