From 641a28ce68b56f3ea7cbff700783fd83594f772b Mon Sep 17 00:00:00 2001 From: Joey Eamigh <55670930+JoeyEamigh@users.noreply.github.com> Date: Tue, 7 Feb 2023 11:09:50 -0500 Subject: [PATCH] more usable --- .gitignore | 1 + Cargo.lock | 1 + Cargo.toml | 1 + Makefile | 17 +++++++++++++++++ build.sh | 2 -- src/main.rs | 46 ++++++++++++++++++++++++++++++++++++---------- 6 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 Makefile delete mode 100644 build.sh diff --git a/.gitignore b/.gitignore index ea8c4bf..4dc0091 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +/bin diff --git a/Cargo.lock b/Cargo.lock index 5b69870..9d9c1e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -282,6 +282,7 @@ dependencies = [ name = "globalenter" version = "0.1.0" dependencies = [ + "bytes", "chrono", "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index 34bac6a..103f996 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,3 +17,4 @@ webbrowser = "0.8.7" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" chrono = "0.4.23" +bytes = "1.4.0" diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8799e05 --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +NAME = "globalenter" + +build b: + @echo "building x86_64 Linux" + RUSTFLAGS=-Awarnings cargo build --release --target x86_64-unknown-linux-gnu -q --frozen + @echo "building x86_64 Windows" + RUSTFLAGS=-Awarnings cargo build --release --target x86_64-pc-windows-gnu -q --frozen + mkdir -p bin + cp target/x86_64-unknown-linux-gnu/release/$(NAME) bin/$(NAME) + chmod +x bin/$(NAME) + cp target/x86_64-pc-windows-gnu/release/$(NAME).exe bin/$(NAME).exe + chmod +x bin/$(NAME).exe + @echo "done" + +clean c: + cargo clean + rm -rf bin \ No newline at end of file diff --git a/build.sh b/build.sh deleted file mode 100644 index 191bc70..0000000 --- a/build.sh +++ /dev/null @@ -1,2 +0,0 @@ -cargo build --target x86_64-pc-windows-gnu --release -cargo build --release diff --git a/src/main.rs b/src/main.rs index 2992652..0bfa62f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ -use std::process::exit; +use tokio::io::{self, AsyncReadExt}; -use chrono::{DateTime, Datelike, Local, Timelike}; +use bytes::BytesMut; +use chrono::{DateTime, Datelike, Timelike}; use reqwest; use serde::{Deserialize, Serialize}; use tokio::time::error::Error; @@ -18,19 +19,39 @@ struct Appointment { } const TIME: u64 = 3000; -// const LOCATION_ID: u32 = 14321; -const LOCATION_ID: u32 = 5023; +const CHARLOTTE: u32 = 14321; +// const TESTING: u32 = 5023; #[tokio::main] async fn main() { + let mut location_id: u32 = CHARLOTTE; + let mut buf = [0, 0, 0, 0]; + + println!("Enter a location id to override the default (Charlotte, NC): "); + let loc_input = io::stdin().read(&mut buf).await; + + if loc_input.unwrap() > 1 { + location_id = String::from_utf8(buf.to_vec()) + .unwrap() + .trim() + .parse() + .unwrap(); + } + eprintln!("Using default location (Charlotte, NC): {}", location_id); + + println!("Looking for appointments at location id: {}", location_id); loop { - look_for_appointments().await; + look_for_appointments(location_id).await; + println!( + "No appointments found, trying again in {} seconds", + TIME / 1000 + ); sleep(Duration::from_millis(TIME)).await; } } -async fn look_for_appointments() { - let data = get_data().await.unwrap(); +async fn look_for_appointments(location_id: u32) { + let data = get_data(location_id).await.unwrap(); for appointment in data { let time = appointment.startTimestamp + ":00-05:00"; @@ -45,14 +66,19 @@ async fn look_for_appointments() { at.minute(), ); open_browser(webbrowser::Browser::Default, "https://ttp.cbp.dhs.gov/credential/v1/login?app=GOES-prod&lang=en&state=en:IonIgXB4I9qt02S6cgKydMVA9HXHC1vq").unwrap(); - exit(0) + + println!("Press any key to continue..."); + io::stdin() + .read_buf(&mut BytesMut::with_capacity(10)) + .await + .unwrap(); } } -async fn get_data() -> Result, Error> { +async fn get_data(location_id: u32) -> Result, Error> { let res = reqwest::get(format!( "https://ttp.cbp.dhs.gov/schedulerapi/slots?orderBy=soonest&limit=3&locationId={}&minimum=1", - LOCATION_ID + location_id )) .await; let body: Vec = res.unwrap().json().await.unwrap();