mirror of
https://forge.katzen.cafe/schrottkatze/nix-configs.git
synced 2024-11-21 21:04:41 +01:00
start working on bar pinger and traewelling client
This commit is contained in:
parent
867514362a
commit
718fe00b46
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -2,3 +2,4 @@ result
|
||||||
.direnv/
|
.direnv/
|
||||||
programs/*/target
|
programs/*/target
|
||||||
target
|
target
|
||||||
|
*openapi.json
|
||||||
|
|
927
Cargo.lock
generated
927
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -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"]
|
||||||
|
|
|
@ -87,6 +87,8 @@
|
||||||
]))
|
]))
|
||||||
rs-toolchain
|
rs-toolchain
|
||||||
pkgs.haskell-language-server
|
pkgs.haskell-language-server
|
||||||
|
pkgs.pkg-config
|
||||||
|
pkgs.openssl
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
nixosConfigurations = {
|
nixosConfigurations = {
|
||||||
|
|
|
@ -1,7 +1,3 @@
|
||||||
.topBar {
|
|
||||||
margin-bottom: 2px;
|
|
||||||
}
|
|
||||||
|
|
||||||
label {
|
label {
|
||||||
font: 14pt "FiraCode Nerd Font";
|
font: 14pt "FiraCode Nerd Font";
|
||||||
}
|
}
|
||||||
|
|
6
programs/bar-pinger/Cargo.toml
Normal file
6
programs/bar-pinger/Cargo.toml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "bar-pinger"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
5
programs/bar-pinger/src/main.rs
Normal file
5
programs/bar-pinger/src/main.rs
Normal 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]));
|
||||||
|
}
|
13
programs/traveldings/Cargo.toml
Normal file
13
programs/traveldings/Cargo.toml
Normal 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"]}
|
1
programs/traveldings/src/commands.rs
Normal file
1
programs/traveldings/src/commands.rs
Normal file
|
@ -0,0 +1 @@
|
||||||
|
pub mod current_journey;
|
9
programs/traveldings/src/commands/current_journey.rs
Normal file
9
programs/traveldings/src/commands/current_journey.rs
Normal 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(())
|
||||||
|
}
|
35
programs/traveldings/src/main.rs
Normal file
35
programs/traveldings/src/main.rs
Normal 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(())
|
||||||
|
}
|
56
programs/traveldings/src/traewelling.rs
Normal file
56
programs/traveldings/src/traewelling.rs
Normal 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;
|
47
programs/traveldings/src/traewelling/model.rs
Normal file
47
programs/traveldings/src/traewelling/model.rs
Normal 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,
|
||||||
|
}
|
Loading…
Reference in a new issue