Initial commit
This commit is contained in:
parent
025f2dac1a
commit
a6bc39ca98
1
.dockerignore
Normal file
1
.dockerignore
Normal file
@ -0,0 +1 @@
|
|||||||
|
target
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
.vscode
|
2116
Cargo.lock
generated
Normal file
2116
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
[package]
|
||||||
|
name = "wordlebot"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
chrono = "0.4.26"
|
||||||
|
reqwest = { version ="0.11.18", features = ["json"]}
|
||||||
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
serde = { version = "1.0.104", features = ["derive"] }
|
||||||
|
serde_json = "1.0.48"
|
||||||
|
lemmy_api_common = "0.18.0"
|
11
Dockerfile
Normal file
11
Dockerfile
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
FROM rust:1-bookworm as builder
|
||||||
|
WORKDIR /usr/src/wordlebot
|
||||||
|
COPY . .
|
||||||
|
RUN cargo install --path .
|
||||||
|
|
||||||
|
FROM debian:bookworm-slim
|
||||||
|
RUN apt-get update \
|
||||||
|
&& apt-get install -y extra-runtime-dependencies libssl3 ca-certificates \
|
||||||
|
&& rm -rf /var/lib/apt/lists/*
|
||||||
|
COPY --from=builder /usr/local/cargo/bin/wordlebot /usr/local/bin/wordlebot
|
||||||
|
CMD ["wordlebot"]
|
99
src/helper.rs
Normal file
99
src/helper.rs
Normal file
@ -0,0 +1,99 @@
|
|||||||
|
use lemmy_api_common::{
|
||||||
|
community::GetCommunity,
|
||||||
|
lemmy_db_schema::newtypes::CommunityId,
|
||||||
|
person::{Login, LoginResponse},
|
||||||
|
sensitive::Sensitive,
|
||||||
|
};
|
||||||
|
use reqwest::Client;
|
||||||
|
use serde_json::Value;
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
static API_VERSION: i32 = 3;
|
||||||
|
|
||||||
|
pub fn lemmy_password() -> Result<Sensitive<String>, std::env::VarError> {
|
||||||
|
Ok(Sensitive::new(std::env::var("LEMMY_PASSWORD")?))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lemmy_user() -> Sensitive<String> {
|
||||||
|
Sensitive::new(std::env::var("LEMMY_USER").unwrap_or_else(|_| "wordlebot".to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lemmy_server() -> String {
|
||||||
|
std::env::var("LEMMY_SERVER").unwrap_or_else(|_| "https://enterprise.lemmy.ml".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lemmy_community() -> String {
|
||||||
|
std::env::var("LEMMY_COMMUNITY").unwrap_or_else(|_| "wordle".to_string())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn api_url(suffix: &str) -> String {
|
||||||
|
format!("{}/api/v{}/{}", lemmy_server(), API_VERSION, suffix)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum WordleError {
|
||||||
|
ParseFailed,
|
||||||
|
LoginFailed,
|
||||||
|
NoNewPostId,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::error::Error for WordleError {}
|
||||||
|
|
||||||
|
impl fmt::Display for WordleError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write!(f, "Oh no, something bad went down")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_community_id(
|
||||||
|
client: &Client,
|
||||||
|
community_name: String,
|
||||||
|
) -> Result<CommunityId, Box<dyn std::error::Error>> {
|
||||||
|
let params = GetCommunity {
|
||||||
|
name: Some(community_name),
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.get(api_url("community"))
|
||||||
|
.query(¶ms)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
let text = response.text().await?;
|
||||||
|
|
||||||
|
// Deserializing from GetCommunityResponse would fail with missing field `followers_url` in Community
|
||||||
|
let data = serde_json::from_str::<Value>(text.as_str())?;
|
||||||
|
|
||||||
|
if let Some(id) = data["community_view"]["community"]
|
||||||
|
.get("id")
|
||||||
|
.and_then(|v| v.as_i64())
|
||||||
|
{
|
||||||
|
return Ok(CommunityId(i32::try_from(id)?));
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(Box::new(WordleError::ParseFailed))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn lemmy_login(client: &Client) -> Result<Sensitive<String>, Box<dyn std::error::Error>> {
|
||||||
|
println!("Logging in");
|
||||||
|
let params = Login {
|
||||||
|
username_or_email: lemmy_user(),
|
||||||
|
password: lemmy_password()?,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.post(api_url("user/login"))
|
||||||
|
.json(¶ms)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
response.error_for_status_ref()?;
|
||||||
|
|
||||||
|
let data = response.json::<LoginResponse>().await?;
|
||||||
|
|
||||||
|
if let Some(jwt) = data.jwt {
|
||||||
|
return Ok(jwt);
|
||||||
|
}
|
||||||
|
|
||||||
|
Err(Box::new(WordleError::LoginFailed))
|
||||||
|
}
|
92
src/main.rs
Normal file
92
src/main.rs
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
use chrono::{Datelike, Month, Utc};
|
||||||
|
use lemmy_api_common::post::CreatePost;
|
||||||
|
use reqwest::{Client, Url};
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serde_json::Value;
|
||||||
|
|
||||||
|
mod helper;
|
||||||
|
use crate::helper::{get_community_id, lemmy_community, lemmy_login, WordleError};
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize, Default)]
|
||||||
|
struct WordleData {
|
||||||
|
days_since_launch: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn get_current_nyt_wordle_data() -> Result<WordleData, Box<dyn std::error::Error>> {
|
||||||
|
println!("Fetching NYT wordle data");
|
||||||
|
let now = Utc::now();
|
||||||
|
let date_string = format!("{}-{:02}-{:02}", now.year(), now.month(), now.day());
|
||||||
|
let response =
|
||||||
|
reqwest::get(format! {"https://www.nytimes.com/svc/wordle/v2/{}.json", date_string})
|
||||||
|
.await?;
|
||||||
|
// Did it work?
|
||||||
|
response.error_for_status_ref()?;
|
||||||
|
let text = response.text().await?;
|
||||||
|
let data: WordleData = serde_json::from_str(&text)?;
|
||||||
|
println!("{:?}", data);
|
||||||
|
Ok(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn post_to_lemmy(data: &WordleData) -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("Posting to lemmy: {:?}", data);
|
||||||
|
|
||||||
|
let client = Client::new();
|
||||||
|
|
||||||
|
let auth = lemmy_login(&client).await?;
|
||||||
|
|
||||||
|
// Get the community id
|
||||||
|
let community_id = get_community_id(&client, lemmy_community()).await?;
|
||||||
|
|
||||||
|
let now = Utc::now();
|
||||||
|
|
||||||
|
let month = Month::try_from(now.month() as u8)?;
|
||||||
|
let title = format!(
|
||||||
|
"Wordle #{} - {} {} {} {}",
|
||||||
|
data.days_since_launch,
|
||||||
|
now.weekday(),
|
||||||
|
now.day(),
|
||||||
|
month.name(),
|
||||||
|
now.year()
|
||||||
|
);
|
||||||
|
let url = Url::parse(
|
||||||
|
format!(
|
||||||
|
"https://www.nytimes.com/games/wordle/index.html#{}",
|
||||||
|
data.days_since_launch
|
||||||
|
)
|
||||||
|
.as_ref(),
|
||||||
|
)?;
|
||||||
|
println!("{}", title);
|
||||||
|
|
||||||
|
let params = CreatePost {
|
||||||
|
community_id,
|
||||||
|
name: title,
|
||||||
|
url: Some(url),
|
||||||
|
body: Some("Post your results and discuss your guesses. Have fun!\nNote: there might be spoilers in the thread.".to_owned()),
|
||||||
|
auth,
|
||||||
|
..Default::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
let response = client
|
||||||
|
.post(helper::api_url("post"))
|
||||||
|
.json(¶ms)
|
||||||
|
.send()
|
||||||
|
.await?;
|
||||||
|
let data = response.json::<Value>().await?;
|
||||||
|
|
||||||
|
// Again, deserializing using the api struct did not work due to missing fields
|
||||||
|
if let Some(new_id) = data["post_view"]["post"].get("id") {
|
||||||
|
println!("New post id: {}", new_id);
|
||||||
|
} else {
|
||||||
|
return Err(Box::new(WordleError::NoNewPostId));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
println!("A wordle a day keeps the doctor away!");
|
||||||
|
let data = get_current_nyt_wordle_data().await?;
|
||||||
|
post_to_lemmy(&data).await?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user