mirror of
https://github.com/djibux/BoilR.git
synced 2026-09-01 05:53:41 +02:00
Implement cached serach
This commit is contained in:
+2
-1
@@ -1,2 +1,3 @@
|
|||||||
/target
|
/target
|
||||||
auth_key.txt
|
auth_key.txt
|
||||||
|
cache.json
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
use std::{collections::HashMap, fs::File, io::Write, path::Path};
|
||||||
|
|
||||||
|
type SearchMap = HashMap<u32, usize>;
|
||||||
|
|
||||||
|
pub struct CachedSearch<'a> {
|
||||||
|
search_map: SearchMap,
|
||||||
|
client: &'a steamgriddb_api::Client,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> CachedSearch<'a> {
|
||||||
|
pub fn new(client: &steamgriddb_api::Client) -> CachedSearch {
|
||||||
|
CachedSearch {
|
||||||
|
search_map: get_search_map(),
|
||||||
|
client,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn save(&self) {
|
||||||
|
save_search_map(&self.search_map);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn search(
|
||||||
|
&mut self,
|
||||||
|
app_id: u32,
|
||||||
|
query: &str,
|
||||||
|
) -> Result<Option<usize>, Box<dyn std::error::Error>> {
|
||||||
|
|
||||||
|
let cached_result = self.search_map.get(&app_id);
|
||||||
|
if let Some(result) = cached_result {
|
||||||
|
return Ok(Some(*result));
|
||||||
|
}
|
||||||
|
|
||||||
|
let search = self.client.search(query).await?;
|
||||||
|
if search.is_empty() {
|
||||||
|
return Ok(None);
|
||||||
|
}
|
||||||
|
let first_item = &search[0];
|
||||||
|
let assumed_id = first_item.id;
|
||||||
|
self.search_map.insert(app_id, assumed_id);
|
||||||
|
|
||||||
|
Ok(Some(assumed_id))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_search_map() -> SearchMap {
|
||||||
|
let path = Path::new("cache.json");
|
||||||
|
if path.exists() {
|
||||||
|
let string = std::fs::read_to_string(path).unwrap();
|
||||||
|
let search_map =
|
||||||
|
serde_json::from_str::<SearchMap>(&string).expect("Failed to parse cache.json");
|
||||||
|
search_map
|
||||||
|
} else {
|
||||||
|
SearchMap::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_search_map(search_map: &SearchMap) {
|
||||||
|
let string = serde_json::to_string(search_map).unwrap();
|
||||||
|
let mut file = File::create("cache.json").unwrap();
|
||||||
|
file.write_all(string.as_bytes()).unwrap();
|
||||||
|
}
|
||||||
+16
-20
@@ -6,19 +6,24 @@ use std::{
|
|||||||
io::Write,
|
io::Write,
|
||||||
path::Path,
|
path::Path,
|
||||||
};
|
};
|
||||||
|
mod cached_search;
|
||||||
mod egs;
|
mod egs;
|
||||||
use egs::{get_egs_manifests, ManifestItem};
|
use egs::{get_egs_manifests, ManifestItem};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use steam_shortcuts_util::{
|
use steam_shortcuts_util::{
|
||||||
parse_shortcuts, shortcut::ShortcutOwned, shortcuts_to_bytes, Shortcut,
|
parse_shortcuts, shortcut::ShortcutOwned, shortcuts_to_bytes, Shortcut,
|
||||||
};
|
};
|
||||||
use steamgriddb_api::{Client, query_parameters::{GridQueryParameters, HeroQueryParameters}, search::SearchResult};
|
use steamgriddb_api::{search::SearchResult, Client};
|
||||||
|
|
||||||
|
use crate::cached_search::CachedSearch;
|
||||||
|
|
||||||
pub struct ShortcutInfo {
|
pub struct ShortcutInfo {
|
||||||
pub path: String,
|
pub path: String,
|
||||||
pub shortcuts: Vec<ShortcutOwned>,
|
pub shortcuts: Vec<ShortcutOwned>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo {
|
fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo {
|
||||||
let mut shortcuts = vec![];
|
let mut shortcuts = vec![];
|
||||||
let mut new_path = user.shortcut_path.clone();
|
let mut new_path = user.shortcut_path.clone();
|
||||||
@@ -55,8 +60,10 @@ fn get_shortcuts_for_user(user: &SteamUsersInfo) -> ShortcutInfo {
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
let auth_key = std::fs::read_to_string("auth_key.txt")?;
|
let auth_key = std::fs::read_to_string("auth_key.txt")?;
|
||||||
let client = steamgriddb_api::Client::new(auth_key);
|
let client = steamgriddb_api::Client::new(auth_key);
|
||||||
|
let mut search = CachedSearch::new(&client);
|
||||||
|
|
||||||
let egs_manifests = get_egs_manifests()?;
|
let egs_manifests = get_egs_manifests()?;
|
||||||
let egs_shortcuts: Vec<ShortcutOwned> =
|
let egs_shortcuts: Vec<ShortcutOwned> =
|
||||||
@@ -97,12 +104,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let mut search_results = HashMap::new();
|
let mut search_results = HashMap::new();
|
||||||
for s in shortcuts_to_search_for {
|
for s in shortcuts_to_search_for {
|
||||||
println!("Searching for {}", s.app_name);
|
println!("Searching for {}", s.app_name);
|
||||||
let search = search_for_shortcut(&client, s.app_name).await;
|
let search = search.search(s.app_id,s.app_name).await?;
|
||||||
if let Some(search) = search {
|
if let Some(search) = search {
|
||||||
search_results.insert(s.app_id, search);
|
search_results.insert(s.app_id, search);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let types = vec![ImageType::Logo, ImageType::Hero, ImageType::Grid];
|
let types = vec![ImageType::Logo, ImageType::Hero, ImageType::Grid];
|
||||||
for image_type in types {
|
for image_type in types {
|
||||||
let mut images_needed = shortcuts
|
let mut images_needed = shortcuts
|
||||||
@@ -112,27 +121,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
let image_ids: Vec<usize> = images_needed
|
let image_ids: Vec<usize> = images_needed
|
||||||
.clone()
|
.clone()
|
||||||
.filter_map(|s| search_results.get(&s.app_id))
|
.filter_map(|s| search_results.get(&s.app_id))
|
||||||
.map(|search| search.id)
|
.map(|search| *search)
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|
||||||
let animation_order= vec![
|
|
||||||
steamgriddb_api::query_parameters::AnimtionType::Animated,
|
|
||||||
steamgriddb_api::query_parameters::AnimtionType::Static,
|
|
||||||
];
|
|
||||||
let hero_parameters = HeroQueryParameters {
|
|
||||||
types: Some(&animation_order),
|
|
||||||
..HeroQueryParameters::default()
|
|
||||||
};
|
|
||||||
let grid_parameters = GridQueryParameters {
|
|
||||||
types: Some(&animation_order),
|
|
||||||
..GridQueryParameters::default()
|
|
||||||
};
|
|
||||||
let query_type = match image_type {
|
let query_type = match image_type {
|
||||||
ImageType::Hero => {
|
ImageType::Hero => steamgriddb_api::query_parameters::QueryType::Hero(None),
|
||||||
steamgriddb_api::query_parameters::QueryType::Hero(Some(hero_parameters))
|
ImageType::Grid => steamgriddb_api::query_parameters::QueryType::Grid(None),
|
||||||
}
|
|
||||||
ImageType::Grid => steamgriddb_api::query_parameters::QueryType::Grid(Some(grid_parameters)),
|
|
||||||
ImageType::Logo => steamgriddb_api::query_parameters::QueryType::Logo(None),
|
ImageType::Logo => steamgriddb_api::query_parameters::QueryType::Logo(None),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -165,6 +159,8 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
search.save();
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user