start mlb parsing

This commit is contained in:
death916 2026-03-12 05:12:25 -07:00
parent 89c79c1410
commit 67307043d6
2 changed files with 64 additions and 16 deletions

View file

@ -2,11 +2,8 @@ mod sports;
use chrono::DateTime;
use chrono::Local;
use iced::Border;
use iced::Center;
use iced::Element;
use iced::Fill;
use iced::Shadow;
use iced::widget::{column, container, image, pane_grid, row, text};
use sports::Game;
pub fn main() -> iced::Result {
@ -103,13 +100,15 @@ impl State {
PaneType::Weather => {
let weather_img = image::Handle::from_bytes(state.weather.clone());
let time = Local::now().format("%H:%M:%S").to_string();
column![
text(time).size(20).center(),
text("Weather").size(50).center(),
image(weather_img).width(Fill).height(Fill).expand(true),
text(state.location.clone()).size(30).center(),
]
.padding(0)
container(
column![
text(time).size(20).center(),
text("Weather").size(50).center(),
image(weather_img).width(Fill),
text(state.location.clone()).size(30).center(),
]
.padding(0),
)
.into()
}
};
@ -123,6 +122,8 @@ impl State {
impl Default for State {
fn default() -> Self {
sports::update_mlb();
let text = ureq::get("https://v2.wttr.in/Sacramento.png?u0")
.header("User-Agent", "deathclock-app/1.0")
.call()
@ -150,5 +151,7 @@ impl Default for State {
panes
},
}
}
}

View file

@ -41,12 +41,57 @@ impl Game {
}
}
pub fn sports() -> Vec<Game> {
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),
]
pub fn update_mlb() {
let date = chrono::Local::now().format("%Y-%m-%d").to_string();
let mlb_url = format!(
"https://statsapi.mlb.com/api/v1/schedule?sportId=1&date={}",
date
);
let mlb_games = ureq::get(&mlb_url)
.header("User-Agent", "deathclock-app/1.0")
.call()
.unwrap()
.into_body()
.read_to_vec()
.unwrap();
let mlb_json: serde_json::Value = serde_json::from_slice(&mlb_games).unwrap();
let games = mlb_json["dates"][0]["games"].as_array().unwrap();
let mut mlb_games_vec = Vec::new();
for game in games {
let home_team = game["teams"]["away"]["team"]["name"].as_str().unwrap();
println!("Home Team: {}", home_team);
let away_team = game["teams"]["home"]["team"]["name"].as_str().unwrap();
println!("Away Team: {}", away_team);
let home_score = game["teams"]["away"]["score"].as_str().unwrap_or_else(|| "0");
let away_score = game["teams"]["home"]["score"].as_str().unwrap_or_else(|| "0");
let period = game["status"]["period"]
.as_str()
.unwrap_or_default()
.parse::<u8>()
.unwrap_or_default();
let mut mlb_game_struct = Game::new(
Sport::MLB,
home_team,
away_team,
home_score,
away_score,
period,
);
mlb_game_struct.update(&home_score, &away_score, period);
mlb_games_vec.push(mlb_game_struct);
println!("Home Team: {}", home_team);
println!("Away Team: {}", away_team);
println!("Home Score: {}", home_score);
println!("Away Score: {}", away_score);
println!("Period: {}", period);
}
// mlb_games_vec
}
pub fn update_nba() -> Vec<Game> {