Code reorganized.

This commit is contained in:
Martin Brodbeck 2021-01-30 17:59:00 +01:00
parent 3f5d790d3c
commit a9ef4d2aef

View file

@ -1,6 +1,6 @@
mod pocketbook; mod pocketbook;
use rusqlite::{named_params, Connection, Result, NO_PARAMS}; use rusqlite::{named_params, Connection, Result, Transaction, NO_PARAMS};
use std::error::Error; use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
@ -111,17 +111,14 @@ struct BookEntry {
filepath: String, filepath: String,
} }
fn main() { fn fix_firstauthor(tx: &Transaction) -> i32 {
let mut authors_fixed = 0; let mut authors_fixed = 0;
let mut conn = Connection::open("/mnt/ext1/system/explorer-3/explorer-3.db").unwrap(); // Get book ids from entries where we have something like "firstname lastname" in author
let tx = conn.transaction().unwrap(); // but no "lastname, firstname" in fistauthor
{ // Get also book ids from the special case where we have multiple authors (separated by ", " in authors)
// Get book ids from entries where we have something like "firstname lastname" in author // but no ampersand ("&") in firstauthor
// but no "lastname, firstname" in fistauthor let mut stmt = tx.prepare(r"
// Get also book ids from the special case where we have multiple authors (separated by ", " in authors)
// but no ampersand ("&") in firstauthor
let mut stmt = tx.prepare(r"
SELECT files.book_id, folders.name, files.filename SELECT files.book_id, folders.name, files.filename
FROM files INNER JOIN folders FROM files INNER JOIN folders
ON files.folder_id = folders.id ON files.folder_id = folders.id
@ -134,44 +131,52 @@ fn main() {
AND files.storageid = 1 AND files.storageid = 1
;").unwrap(); ;").unwrap();
let mut rows = stmt.query(NO_PARAMS).unwrap(); let mut rows = stmt.query(NO_PARAMS).unwrap();
let mut bookentries = Vec::new(); let mut bookentries = Vec::new();
while let Some(row) = rows.next().unwrap() { while let Some(row) = rows.next().unwrap() {
let book_id: i32 = row.get(0).unwrap(); let book_id: i32 = row.get(0).unwrap();
let prefix: String = row.get(1).unwrap(); let prefix: String = row.get(1).unwrap();
let filename: String = row.get(2).unwrap(); let filename: String = row.get(2).unwrap();
let filepath = format!("{}/{}", prefix, filename); let filepath = format!("{}/{}", prefix, filename);
bookentries.push(BookEntry { bookentries.push(BookEntry {
id: book_id, id: book_id,
filepath, filepath,
}); });
} }
for entry in bookentries { for entry in bookentries {
let file = File::open(entry.filepath.as_str()); let file = File::open(entry.filepath.as_str());
let file = match file { let file = match file {
Err(_) => continue, Err(_) => continue,
Ok(file) => file, Ok(file) => file,
}; };
let mut archive = ZipArchive::new(BufReader::new(file)).unwrap(); let mut archive = ZipArchive::new(BufReader::new(file)).unwrap();
let container = archive.by_name("META-INF/container.xml").unwrap(); let container = archive.by_name("META-INF/container.xml").unwrap();
if let Some(opf_file) = get_root_file(container).unwrap() { if let Some(opf_file) = get_root_file(container).unwrap() {
let opf = archive.by_name(opf_file.as_str()).unwrap(); let opf = archive.by_name(opf_file.as_str()).unwrap();
if let Some(file_as) = get_attribute_file_as(opf) { if let Some(file_as) = get_attribute_file_as(opf) {
let mut stmt = tx let mut stmt = tx
.prepare("UPDATE books_impl SET firstauthor = :file_as WHERE id = :book_id") .prepare("UPDATE books_impl SET firstauthor = :file_as WHERE id = :book_id")
.unwrap(); .unwrap();
stmt.execute_named(named_params![":file_as": file_as, ":book_id": entry.id]) stmt.execute_named(named_params![":file_as": file_as, ":book_id": entry.id])
.unwrap(); .unwrap();
authors_fixed = authors_fixed + 1; authors_fixed = authors_fixed + 1;
}
} }
} }
} }
authors_fixed
}
fn main() {
let mut conn = Connection::open("/mnt/ext1/system/explorer-3/explorer-3.db").unwrap();
let tx = conn.transaction().unwrap();
let authors_fixed = fix_firstauthor(&tx);
tx.commit().unwrap(); tx.commit().unwrap();
if cfg!(target_arch = "arm") { if cfg!(target_arch = "arm") {