For a final bit of code to roast, here is the bot that posted the daily threads:
Bot code
use std::process::exit;
use chrono::{Datelike, Timelike};
use lemmy_client::lemmy_api_common::community::GetCommunity;
use lemmy_client::lemmy_api_common::person::Login;
use lemmy_client::lemmy_api_common::post::CreatePost;
use lemmy_client::{ClientOptions, LemmyClient};
use rand::seq::IndexedRandom;
use regex::Regex;
static TARGET_COMMUNITY: &str = "Advent_Of_Code@programming.dev";
async fn get_advent_emoji() -> String {
let choices = ['๐','๐','๐งฆ','๐ฏ','๐','โจ','๐งธ','โ','โ','โ','๐','๐
','๐คถ','๐ง','๐ฆ','๐ผ','๐ช','๐ฝ','๐ฅ','๐ท','๐','๐ฅง','๐ฌ','๐ซ','๐ถ','๐ถ','๐','๐','๐','๐'];
choices.choose(&mut rand::rng()).unwrap().to_string()
}
async fn get_advent_title(year: i32, day: u32) -> String {
let url = format!("https://adventofcode.com/%7B%7D/day/%7B%7D", year, day);
let resp = reqwest::get(url).await.unwrap();
let body = String::from_utf8(resp.bytes().await.unwrap().to_vec()).unwrap();
let re = Regex::new(r"---(.*)---").unwrap();
for (_, [line]) in re.captures_iter(&body).map(|c| c.extract()) {
return line.trim().to_string();
}
panic!("could not find advent title - {body}");
}
#[tokio::main]
async fn main() {
let time = chrono::Utc::now();
let year = time.year();
let day = time.day();
if time.hour() != 5 {
println!("Wrong time: {time}");
exit(0);
}
let title = get_advent_title(year, day).await;
println!("Title: {:#?}", title);
post_to_lemmy(year, day, title).await;
}
async fn post_to_lemmy(year: i32, day:u32, challenge_title: String) {
let emoji = get_advent_emoji().await;
let post_title = format!("{emoji} - {year} DAY {day} SOLUTIONS - {emoji}");
let post_body = format!("# {challenge_title}
## Megathread guidelines
- Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
- You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL
## FAQ
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465");
let mut client = LemmyClient::new(ClientOptions {
domain: "programming.dev".to_string(),
secure: true,
});
let auth = client.login(Login {
username_or_email: "HARDCODED".to_string().into(),
password: "HARDCODED".to_string().into(),
totp_2fa_token: None,
}).await.unwrap();
client.headers_mut().insert("Authorization".to_string(),
format!("Bearer {}", auth.jwt.unwrap().to_string()));
let community = client.get_community(GetCommunity {
id: None,
name: Some(TARGET_COMMUNITY.to_string()),
}).await.unwrap();
let community_id = community.community_view.community.id;
client.create_post(CreatePost {
name: post_title,
community_id,
url: None,
body: Some(post_body),
alt_text: None,
honeypot: None,
nsfw: None,
language_id: None,
custom_thumbnail: None,
}).await.unwrap();
}