start working on bar pinger and traewelling client

This commit is contained in:
Schrottkatze 2024-09-07 18:31:04 +02:00
parent 867514362a
commit 718fe00b46
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
13 changed files with 1062 additions and 48 deletions

1
.gitignore vendored
View file

@ -2,3 +2,4 @@ result
.direnv/ .direnv/
programs/*/target programs/*/target
target target
*openapi.json

927
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,5 +1,5 @@
[workspace] [workspace]
resolver = "2" resolver = "2"
members = [ "programs/bar-ws-monitor", members = [ "programs/bar-pinger", "programs/bar-ws-monitor",
"programs/jrnl" "programs/jrnl"
] , "programs/traveldings"]

View file

@ -87,6 +87,8 @@
])) ]))
rs-toolchain rs-toolchain
pkgs.haskell-language-server pkgs.haskell-language-server
pkgs.pkg-config
pkgs.openssl
]; ];
}; };
nixosConfigurations = { nixosConfigurations = {

View file

@ -1,7 +1,3 @@
.topBar {
margin-bottom: 2px;
}
label { label {
font: 14pt "FiraCode Nerd Font"; font: 14pt "FiraCode Nerd Font";
} }

View file

@ -0,0 +1,6 @@
[package]
name = "bar-pinger"
version = "0.1.0"
edition = "2021"
[dependencies]

View file

@ -0,0 +1,5 @@
// const ADDRS: [&str] = ["katzen.cafe", "fucktorio.s10e.de", "9.9.9.9"];
fn main() {
// let pingers = ADDRS.iter.map(|addr| Command::new("ping").args([addr]));
}

View file

@ -0,0 +1,13 @@
[package]
name = "traveldings"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0.209", features = ["derive"] }
serde_json = "1.0.128"
reqwest = {version = "0.12.7", default-features = false, features = ["rustls-tls", "charset", "http2"]}
tokio = { version = "1", features = ["full"] }
anyhow = "1"
chrono = { version = "0.4", features = ["serde"]}
clap = { version = "4.5", features = ["derive"]}

View file

@ -0,0 +1 @@
pub mod current_journey;

View file

@ -0,0 +1,9 @@
use crate::traewelling::TraewellingClient;
pub async fn get_current_journey() -> anyhow::Result<()> {
let client = TraewellingClient::new()?;
println!("active: {:#?}", client.get_active_checkin().await?);
Ok(())
}

View file

@ -0,0 +1,35 @@
use std::{default, fs};
use clap::{Parser, Subcommand};
use commands::current_journey::get_current_journey;
use reqwest::{
header::{self, HeaderMap},
ClientBuilder,
};
use traewelling::model::{Container, Status};
mod commands;
mod traewelling;
#[derive(Parser)]
struct Cli {
#[command(subcommand)]
subcommand: Subcommands,
}
#[derive(Subcommand, Clone)]
enum Subcommands {
/// Watch for a current journey and give out json info about it
Current,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Cli::parse();
match args.subcommand {
Subcommands::Current => get_current_journey().await?,
};
Ok(())
}

View file

@ -0,0 +1,56 @@
use std::{fmt, fs};
use model::{Container, Status};
use reqwest::{
header::{self, HeaderMap},
Client, ClientBuilder,
};
const KEY_PATH: &str = "/home/jade/Docs/traveldings-key";
const USER_AGENT: &str = "s10e/traveldings";
const TRAEWELLING_API_URL: &str = "https://traewelling.de/api/v1";
pub struct TraewellingClient {
client: Client,
}
impl TraewellingClient {
pub fn new() -> anyhow::Result<Self> {
let mut headers = HeaderMap::new();
let token = fs::read_to_string(KEY_PATH)?;
let key = header::HeaderValue::from_str(&format!("Bearer {token}"))?;
println!("meow");
headers.insert("Authorization", key);
headers.insert(
header::ACCEPT,
header::HeaderValue::from_static("application/json"),
);
Ok(Self {
client: ClientBuilder::new()
.user_agent("s10e/traveldings")
.default_headers(headers)
.build()?,
})
}
pub async fn get_active_checkin(&self) -> anyhow::Result<Status> {
let txt = self
.client
.get(Self::fmt_url("user/statuses/active"))
.send()
.await?
.text()
.await?;
println!("{txt}");
let res: Container<Status> = serde_json::de::from_str(&txt)?;
Ok(res.data)
}
fn fmt_url(path: impl fmt::Display) -> String {
format!("{TRAEWELLING_API_URL}/{path}")
}
}
pub mod model;

View file

@ -0,0 +1,47 @@
use chrono::{DateTime, FixedOffset};
use serde::Deserialize;
#[derive(Deserialize, Debug)]
pub struct Container<D> {
pub data: D,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Status {
train: TransportResource,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TransportResource {
category: String,
line_name: String,
distance: u32,
duration: u32,
operator: OperatorResource,
origin: StopOverResource,
destination: StopOverResource,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct StopOverResource {
name: String,
ril_identifier: Option<String>,
arrival: Option<DateTime<FixedOffset>>,
arrival_planned: Option<DateTime<FixedOffset>>,
arrival_real: Option<DateTime<FixedOffset>>,
departure: Option<DateTime<FixedOffset>>,
departure_planned: Option<DateTime<FixedOffset>>,
departure_real: Option<DateTime<FixedOffset>>,
platform: Option<String>,
departure_platform_planned: Option<String>,
departure_platform_real: Option<String>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct OperatorResource {
name: String,
}