start sports updates

This commit is contained in:
death916 2026-03-11 05:46:51 -07:00
parent 1001d60b25
commit 464f109bc0
3 changed files with 62 additions and 32 deletions

2
rust/Cargo.lock generated
View file

@ -3496,6 +3496,8 @@ dependencies = [
"chrono",
"iced",
"reqwest",
"serde",
"serde_json",
"ureq",
]

View file

@ -7,4 +7,6 @@ edition = "2024"
chrono = "0.4.44"
iced = { version = "0.14.0", features = ["image"] }
reqwest = "0.13.2"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
ureq = "3.2.0"

View file

@ -101,7 +101,10 @@ impl Default for State {
news: Vec::new(),
weather: text,
location: "Sacramento".to_string(),
scores: sports().scores,
scores: {
sports_updates::update_nba();
sports()
},
panes: {
let (mut panes, nba) = pane_grid::State::new(Pane::NbaPane);
let (weather, _) = panes
@ -124,16 +127,18 @@ struct Game {
team2: String,
score1: String,
score2: String,
period: u8,
}
impl Game {
fn new(sport: Sport, team1: &str, team2: &str, score1: &str, score2: &str) -> Self {
fn new(sport: Sport, team1: &str, team2: &str, score1: &str, score2: &str, period: u8) -> Self {
Game {
sport,
team1: team1.to_string(),
team2: team2.to_string(),
score1: score1.to_string(),
score2: score2.to_string(),
period,
}
}
@ -143,36 +148,57 @@ impl Game {
}
}
fn sports() -> State {
fn sports() -> Vec<Game> {
println!("Sports!");
let mut state = State {
current_time: chrono::Utc::now(),
next_alarm: Some(chrono::Utc::now() + chrono::Duration::hours(1)),
news: vec!["Breaking news!".to_string()],
weather: Vec::new(),
location: "Sacramento".to_string(),
scores: Vec::new(),
panes: pane_grid::State::new(Pane::Main).0,
};
state
.scores
.push(Game::new(Sport::NBA, "Lakers", "Warriors", "100", "95"));
state
.scores
.push(Game::new(Sport::NBA, "Celtics", "Nets", "110", "105"));
state
.scores
.push(Game::new(Sport::MLB, "Red Sox", "Yankees", "100", "95"));
println!("{:?}", state.current_time);
println!("---------------");
for game in &state.scores {
println!("+----------------------+");
println!("| Sport: {:?}", game.sport);
println!("| {} vs {}", game.team1, game.team2);
println!("| {} - {}", game.score1, game.score2);
println!("+----------------------+");
}
state
vec![
Game::new(Sport::NBA, "Lakers", "Warriors", "100", "95", 3),
Game::new(Sport::NBA, "Celtics", "Nets", "110", "105", 2),
Game::new(Sport::MLB, "Red Sox", "Yankees", "100", "95", 1),
]
}
mod sports_updates {
use super::Game;
use super::Sport;
pub fn update_nba() {
let nba_games = ureq::get(
"https://cdn.nba.com/static/json/liveData/scoreboard/todaysScoreboard_00.json",
)
.header("User-Agent", "deathclock-app/1.0")
.call()
.unwrap()
.into_body()
.read_to_vec()
.unwrap();
let json: serde_json::Value = serde_json::from_slice(&nba_games).unwrap();
let games = json["scoreboard"]["games"].as_array().unwrap();
for game in games {
let game_id = game["gameId"].as_str().unwrap();
let home_team = game["homeTeam"]["teamName"].as_str().unwrap();
let away_team = game["awayTeam"]["teamName"].as_str().unwrap();
let home_score = game["homeTeam"]["score"].as_u64().unwrap().to_string();
let away_score = game["awayTeam"]["score"].as_u64().unwrap().to_string();
let period = game["period"].as_u64().unwrap() as u8;
let mut game = Game::new(
Sport::NBA,
home_team,
away_team,
&home_score,
&away_score,
period,
);
game.update(&home_score, &away_score);
println!("Game ID: {}", game_id);
println!("Home Team: {}", home_team);
println!("Away Team: {}", away_team);
println!("Home Score: {}", home_score);
println!("Away Score: {}", away_score);
println!("Period: {}", period);
}
}
}