2022-10-06 12:54:25 +00:00
|
|
|
//! Code for creating a new snapshot.
|
|
|
|
|
|
|
|
use {
|
|
|
|
color_eyre::Result,
|
|
|
|
sea_orm::{prelude::*, ActiveValue::*, TransactionTrait},
|
|
|
|
tildes_parser::{Group, GroupList},
|
|
|
|
tracing::{debug, info},
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::{
|
2022-10-07 13:27:43 +00:00
|
|
|
group_data::{GroupDataActiveModel, GroupDataEntity},
|
|
|
|
snapshots::{SnapshotActiveModel, SnapshotModel},
|
2022-10-09 21:34:39 +00:00
|
|
|
utilities::{create_http_client, download_html, get_base_url, today},
|
2022-10-06 12:54:25 +00:00
|
|
|
};
|
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
impl SnapshotModel {
|
|
|
|
/// Create a snapshot for today.
|
|
|
|
pub async fn create(db: &DatabaseConnection, force: bool) -> Result<()> {
|
2022-10-09 21:34:39 +00:00
|
|
|
let base_url = get_base_url();
|
2022-10-07 13:27:43 +00:00
|
|
|
let snapshot_date = today();
|
|
|
|
match (force, Self::get_by_date(db, snapshot_date).await?) {
|
|
|
|
(true, Some(existing)) => {
|
|
|
|
info!("Removing existing snapshot {:?}", existing);
|
|
|
|
existing.delete(db).await?;
|
|
|
|
}
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
(false, Some(existing)) => {
|
|
|
|
info!("Snapshot for today already exists");
|
|
|
|
info!("Use --force to override snapshot {:?}", existing);
|
|
|
|
return Ok(());
|
|
|
|
}
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
(_, None) => (),
|
|
|
|
};
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
let transaction = db.begin().await?;
|
|
|
|
let snapshot = SnapshotActiveModel {
|
|
|
|
date: Set(snapshot_date),
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.insert(&transaction)
|
|
|
|
.await?;
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
info!("Scraping data for snapshot {:?}", snapshot);
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
let http = create_http_client()?;
|
|
|
|
let group_list = GroupList::from_html(
|
2022-10-09 21:34:39 +00:00
|
|
|
&download_html(&http, format!("{}/groups", base_url)).await?,
|
2022-10-07 13:27:43 +00:00
|
|
|
)?;
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
let mut groups_to_insert = vec![];
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
for summary in group_list.summaries {
|
|
|
|
debug!(summary = ?summary);
|
|
|
|
let group = Group::from_html(
|
2022-10-09 21:34:39 +00:00
|
|
|
&download_html(&http, format!("{}/{}", base_url, summary.name)).await?,
|
2022-10-07 13:27:43 +00:00
|
|
|
)?;
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
debug!(group = ?group);
|
|
|
|
groups_to_insert.push(GroupDataActiveModel {
|
|
|
|
description: Set(group.description),
|
|
|
|
name: Set(group.name),
|
|
|
|
snapshot_id: Set(snapshot.id),
|
|
|
|
subscribers: Set(group.subscribers.into()),
|
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
}
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
info!("Inserting {} groups", groups_to_insert.len());
|
|
|
|
GroupDataEntity::insert_many(groups_to_insert)
|
|
|
|
.exec(&transaction)
|
|
|
|
.await?;
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
transaction.commit().await?;
|
2022-10-06 12:54:25 +00:00
|
|
|
|
2022-10-07 13:27:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-06 12:54:25 +00:00
|
|
|
}
|