Compare commits
2 Commits
5dfdff17c1
...
9c2d9c1fb7
| Author | SHA1 | Date | |
|---|---|---|---|
| 9c2d9c1fb7 | |||
| 1462401787 |
@@ -1,23 +1,9 @@
|
|||||||
use dir_spec::Dir;
|
|
||||||
use sqlite;
|
use sqlite;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
const DATA_FOLDER: &str = "scryfall";
|
use super::utils::get_local_cache_folder;
|
||||||
const SQLITE_FILENAME: &str = "scryfall_db.sqlite3";
|
use super::utils::{create_cache_folder, get_local_data_folder, SQLITE_FILENAME};
|
||||||
|
|
||||||
fn get_local_data_folder() -> PathBuf {
|
|
||||||
let cache_folder = Dir::data_home();
|
|
||||||
match cache_folder {
|
|
||||||
None => {
|
|
||||||
panic!("Can't find a cache folder - really don't know what the problem is sorry");
|
|
||||||
}
|
|
||||||
Some(mut f) => {
|
|
||||||
f.push(DATA_FOLDER);
|
|
||||||
f
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_local_data_sqlite_file() -> PathBuf {
|
fn get_local_data_sqlite_file() -> PathBuf {
|
||||||
let mut folder = get_local_data_folder();
|
let mut folder = get_local_data_folder();
|
||||||
@@ -25,27 +11,6 @@ fn get_local_data_sqlite_file() -> PathBuf {
|
|||||||
folder
|
folder
|
||||||
}
|
}
|
||||||
|
|
||||||
// NOTE: this should be idempotent - creating a dir always is... right?
|
|
||||||
pub fn create_cache_folder() {
|
|
||||||
let f = get_local_data_folder();
|
|
||||||
let ret = fs::create_dir(&f);
|
|
||||||
match ret {
|
|
||||||
Ok(_) => (),
|
|
||||||
Err(e) => {
|
|
||||||
if e.raw_os_error().unwrap() == 17 {
|
|
||||||
// This is folder already exists - which is fine for us
|
|
||||||
// TODO probably should use e.kind() for better readability
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
panic!(
|
|
||||||
"Couldn't create folder within your cache folder: {}. Error is {}",
|
|
||||||
f.display(),
|
|
||||||
e
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn create_db_sql() -> String {
|
fn create_db_sql() -> String {
|
||||||
"
|
"
|
||||||
CREATE TABLE cards (
|
CREATE TABLE cards (
|
||||||
@@ -66,6 +31,8 @@ pub fn init_db() -> bool {
|
|||||||
create_cache_folder();
|
create_cache_folder();
|
||||||
let sqlite_file = get_local_data_sqlite_file();
|
let sqlite_file = get_local_data_sqlite_file();
|
||||||
println!("sqlite file location: {}", sqlite_file.display());
|
println!("sqlite file location: {}", sqlite_file.display());
|
||||||
|
// TESTING
|
||||||
|
println!("cache folder: {}", get_local_cache_folder().display());
|
||||||
let _res = fs::remove_file(&sqlite_file);
|
let _res = fs::remove_file(&sqlite_file);
|
||||||
// TODO actually check result for whether it was a permissions thing or something
|
// TODO actually check result for whether it was a permissions thing or something
|
||||||
let connection = sqlite::open(sqlite_file).unwrap();
|
let connection = sqlite::open(sqlite_file).unwrap();
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ use ureq;
|
|||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
struct ScryfallBulkData {
|
struct ScryfallBulkData {
|
||||||
pub id: Uuid,
|
pub id: Uuid,
|
||||||
pub uri: String,
|
pub uri: String,
|
||||||
@@ -20,7 +20,7 @@ struct ScryfallBulkData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug, Clone)]
|
||||||
struct ScryfallBulk {
|
struct ScryfallBulk {
|
||||||
pub object: String,
|
pub object: String,
|
||||||
pub has_more: bool,
|
pub has_more: bool,
|
||||||
@@ -56,11 +56,17 @@ pub fn download_latest(
|
|||||||
|
|
||||||
let mut download_uri = String::new();
|
let mut download_uri = String::new();
|
||||||
for scryfall_bulk in bulk_body.data {
|
for scryfall_bulk in bulk_body.data {
|
||||||
if serde_json::to_string(&stype).unwrap() == scryfall_bulk.stype {
|
if serde_json::to_string(&stype)
|
||||||
|
.unwrap()
|
||||||
|
.contains(&scryfall_bulk.stype)
|
||||||
|
{
|
||||||
download_uri = scryfall_bulk.download_uri;
|
download_uri = scryfall_bulk.download_uri;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert!(!download_uri.is_empty());
|
assert!(!download_uri.is_empty());
|
||||||
|
return Ok(());
|
||||||
|
// Just while testing - don't need to download 150MB every time...
|
||||||
let cards_response = ureq::get(download_uri)
|
let cards_response = ureq::get(download_uri)
|
||||||
.header("User-Agent", "Arthur's Card Finger Testing v0.1")
|
.header("User-Agent", "Arthur's Card Finger Testing v0.1")
|
||||||
.header("Accept", "application/json")
|
.header("Accept", "application/json")
|
||||||
|
|||||||
@@ -6,3 +6,5 @@ pub use crate::deser::ScryfallCard;
|
|||||||
|
|
||||||
mod db;
|
mod db;
|
||||||
pub use db::init_db;
|
pub use db::init_db;
|
||||||
|
|
||||||
|
mod utils;
|
||||||
|
|||||||
80
scryfall_deser/src/utils.rs
Normal file
80
scryfall_deser/src/utils.rs
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
use dir_spec::Dir;
|
||||||
|
use std::fs;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub const LOCAL_FOLDER: &str = "scryfall";
|
||||||
|
pub const SQLITE_FILENAME: &str = "scryfall_db.sqlite3";
|
||||||
|
|
||||||
|
pub fn get_local_data_folder() -> PathBuf {
|
||||||
|
let data_folder = Dir::data_home();
|
||||||
|
match data_folder {
|
||||||
|
None => {
|
||||||
|
panic!("Can't find a data folder - really don't know what the problem is sorry");
|
||||||
|
}
|
||||||
|
Some(mut f) => {
|
||||||
|
f.push(LOCAL_FOLDER);
|
||||||
|
f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_local_cache_folder() -> PathBuf {
|
||||||
|
let cache_folder = Dir::cache_home();
|
||||||
|
match cache_folder {
|
||||||
|
None => {
|
||||||
|
panic!("Can't find a cache folder - really don't know what the problem is sorry");
|
||||||
|
}
|
||||||
|
Some(mut f) => {
|
||||||
|
f.push(LOCAL_FOLDER);
|
||||||
|
f
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_local_data_sqlite_file() -> PathBuf {
|
||||||
|
let mut folder = get_local_data_folder();
|
||||||
|
folder.push(SQLITE_FILENAME);
|
||||||
|
folder
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: this should be idempotent - creating a dir always is... right?
|
||||||
|
pub fn create_data_folder() {
|
||||||
|
let f = get_local_data_folder();
|
||||||
|
let ret = fs::create_dir(&f);
|
||||||
|
match ret {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(e) => {
|
||||||
|
if e.raw_os_error().unwrap() == 17 {
|
||||||
|
// 17 = this is folder already exists - which is fine for us
|
||||||
|
// TODO probably should use e.kind() for better readability
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
panic!(
|
||||||
|
"Couldn't create folder within your cache folder: {}. Error is {}",
|
||||||
|
f.display(),
|
||||||
|
e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE: this should be idempotent - creating a dir always is... right?
|
||||||
|
pub fn create_cache_folder() {
|
||||||
|
let f = get_local_cache_folder();
|
||||||
|
let ret = fs::create_dir(&f);
|
||||||
|
match ret {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(e) => {
|
||||||
|
if e.raw_os_error().unwrap() == 17 {
|
||||||
|
// 17 = this is folder already exists - which is fine for us
|
||||||
|
// TODO probably should use e.kind() for better readability
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
panic!(
|
||||||
|
"Couldn't create folder within your cache folder: {}. Error is {}",
|
||||||
|
f.display(),
|
||||||
|
e
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user