revert to septa api for rt tracking
This commit is contained in:
parent
f407992035
commit
a335f14b14
18 changed files with 535 additions and 355 deletions
|
|
@ -3,5 +3,8 @@ gtfs_zips:
|
|||
subzip: "google_rail.zip"
|
||||
- uri: "https://www3.septa.org/developer/gtfs_public.zip"
|
||||
subzip: "google_bus.zip"
|
||||
- uri: "https://www.njtransit.com/rail_data.zip"
|
||||
- uri: "https://www.njtransit.com/bus_data.zip"
|
||||
# - uri: "https://www.njtransit.com/rail_data.zip"
|
||||
# - uri: "https://www.njtransit.com/bus_data.zip"
|
||||
annotations:
|
||||
synthetic_routes:
|
||||
- id: 'NYC'
|
||||
|
|
|
|||
|
|
@ -15,14 +15,6 @@ async fn main() -> ::anyhow::Result<()> {
|
|||
let env = Env::new().filter_or("RUST_LOG", "data_loader=info");
|
||||
Builder::from_env(env).init();
|
||||
|
||||
let mut file = File::open("config.yaml")?;
|
||||
let mut file_contents = String::new();
|
||||
file.read_to_string(&mut file_contents);
|
||||
|
||||
let config_file = serde_yaml::from_str::<Config>(file_contents.as_str())?;
|
||||
|
||||
let svc = GtfsPullService::new(config_file);
|
||||
svc.start();
|
||||
|
||||
loop{
|
||||
thread::sleep(Duration::from_secs(120));
|
||||
|
|
@ -41,88 +33,3 @@ async fn main() -> ::anyhow::Result<()> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug,Clone)]
|
||||
struct GtfsSource {
|
||||
pub uri: String,
|
||||
pub subzip: Option<String>
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||
struct Config {
|
||||
pub gtfs_zips: Vec<GtfsSource>
|
||||
}
|
||||
|
||||
struct GtfsFile {
|
||||
pub source: GtfsSource,
|
||||
pub hash: Option<String>
|
||||
}
|
||||
|
||||
struct GtfsPullServiceState {
|
||||
pub gtfs_files: Vec<GtfsFile>,
|
||||
pub tmp_dir: PathBuf
|
||||
}
|
||||
|
||||
pub struct GtfsPullService {
|
||||
state: Arc<Mutex<GtfsPullServiceState>>
|
||||
}
|
||||
|
||||
impl GtfsPullService {
|
||||
const UPDATE_SECONDS: u64 = 3600*24;
|
||||
|
||||
pub fn new(config: Config) -> Self {
|
||||
Self {
|
||||
state: Arc::new(Mutex::new(
|
||||
GtfsPullServiceState {
|
||||
gtfs_files: config.gtfs_zips.iter().map(|f| { GtfsFile { source: f.clone(), hash: None} }).collect(),
|
||||
tmp_dir: env::temp_dir()
|
||||
}
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn start(&self) {
|
||||
let cloned_state = Arc::clone(&self.state);
|
||||
thread::spawn(move || {
|
||||
loop {
|
||||
let recloned_state = Arc::clone(&cloned_state);
|
||||
let res = Self::update_gtfs_data(recloned_state);
|
||||
|
||||
match res {
|
||||
Err(err) => {
|
||||
error!("{}", err);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
thread::sleep(Duration::from_secs(Self::UPDATE_SECONDS));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn update_gtfs_data(state: Arc<Mutex<GtfsPullServiceState>>) -> anyhow::Result<()> {
|
||||
let l_state = state.lock().unwrap();
|
||||
|
||||
for gtfs_file in l_state.gtfs_files.iter() {
|
||||
let gtfs = if let Some(subzip) = gtfs_file.source.subzip.clone() {
|
||||
info!("Reading GTFS file at {} (subzip {})", gtfs_file.source.uri, subzip);
|
||||
let res = reqwest::blocking::get(gtfs_file.source.uri.clone())?;
|
||||
let outer_archive = res.bytes()?;
|
||||
let mut archive = ZipArchive::new(Cursor::new(outer_archive))?;
|
||||
archive.extract(l_state.tmp_dir.clone())?;
|
||||
|
||||
let mut file_path = l_state.tmp_dir.clone();
|
||||
file_path.push(subzip.clone());
|
||||
|
||||
gtfs_structures::Gtfs::new(file_path.to_str().unwrap())?
|
||||
} else {
|
||||
info!("Reading GTFS file at {}", gtfs_file.source.uri);
|
||||
gtfs_structures::Gtfs::new(gtfs_file.source.uri.as_str())?
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue