Removed imgur support

+ Added my own basic image handling
+ Probably not perfect but works good enough in testing
+ clippy + fmt
This commit is contained in:
2023-06-04 14:10:01 -06:00
parent 14032692b4
commit 7847bd38b9
14 changed files with 339 additions and 257 deletions
-41
View File
@@ -1,41 +0,0 @@
use crate::error::Error;
use j_db::database::Database;
use j_db::model::JdbModel;
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct AlbumConfig {
id: Option<u64>,
pub name: String,
pub album_id: String,
}
impl JdbModel for AlbumConfig {
fn id(&self) -> Option<u64> {
self.id
}
fn set_id(&mut self, id: u64) {
self.id = Some(id)
}
fn tree() -> String {
"Album".to_string()
}
}
impl AlbumConfig {
pub fn new(name: &str, album_id: &str) -> AlbumConfig {
Self {
id: None,
name: name.to_string(),
album_id: album_id.to_string(),
}
}
pub fn get_album_by_name(db: &Database, name: &str) -> Result<Option<AlbumConfig>, Error> {
Ok(db
.filter(|_, album_config: &AlbumConfig| album_config.name.eq_ignore_ascii_case(name))?
.next())
}
}
-1
View File
@@ -1,4 +1,3 @@
pub mod album;
pub mod api_key;
pub mod birthday;
pub mod insult_compliment;
+19 -9
View File
@@ -1,15 +1,16 @@
use crate::album_manager::Album;
use crate::error::Error;
use crate::imgur::get_album_images;
use j_db::database::Database;
use j_db::model::JdbModel;
use rand::prelude::{IteratorRandom, SliceRandom};
use rand::thread_rng;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct MotivationConfig {
id: Option<u64>,
pub album: Vec<String>,
pub album: Vec<u64>,
pub action: Vec<String>,
pub goal: Vec<String>,
}
@@ -47,7 +48,10 @@ impl MotivationConfig {
pub fn add_album(db: &Database, album: &str) -> Result<(), Error> {
let mut motivation = Self::get_motivation_config(db)?;
motivation.album.push(album.to_string());
let album = Album::find_album_by_name_or_alias(db, album)
.unwrap()
.unwrap();
motivation.album.push(album.id().unwrap());
db.insert(motivation)?;
@@ -74,7 +78,7 @@ impl MotivationConfig {
pub async fn generate_motivation(
db: &Database,
imgur_client_id: &str,
base_path: &Path,
border_color: &str,
) -> Result<Motivation, Error> {
let motivation = Self::get_motivation_config(db)?;
@@ -82,18 +86,24 @@ impl MotivationConfig {
let mut images = Vec::new();
for album in motivation.album {
let mut album_images = get_album_images(imgur_client_id, &album).await?;
images.append(&mut album_images);
let album = db.get::<Album>(album);
if let Ok(album) = album {
let mut album_images = album.images.clone();
images.append(&mut album_images);
}
}
let image = images.choose(&mut thread_rng()).unwrap();
let image = images
.choose(&mut thread_rng())
.ok_or(Error::NoAlbumFound)?;
let action = motivation.action.iter().choose(&mut thread_rng()).unwrap();
let goal = motivation.goal.iter().choose(&mut thread_rng()).unwrap();
Ok(Motivation {
image_url: image.link.clone(),
image_path: image.full_path(base_path.to_path_buf()),
action: action.to_string(),
goal: goal.to_string(),
border_color: border_color.to_string(),
@@ -103,7 +113,7 @@ impl MotivationConfig {
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct Motivation {
pub image_url: String,
pub image_path: PathBuf,
pub action: String,
pub goal: String,
pub border_color: String,