Added download of book metadata

This commit is contained in:
Robin Löhn 2025-09-24 18:07:05 +02:00
parent fd6fb1fda3
commit 2abb44408a
Signed by: robin
GPG key ID: 4F5CDA3F9635EB10
4 changed files with 1697 additions and 15 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
/target
/src-tauri
/dist

1626
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,3 +5,7 @@ edition = "2024"
[dependencies]
serial = "0.4.0"
reqwest = { version = "0.12.23", features = ["json"] }
serde_json = "1.0.145"
tokio = { version = "1.47.1", features = ["full"] }
serde = { version = "1.0.226", features = ["derive"] }

View file

@ -1,17 +1,50 @@
extern crate serial;
use reqwest::Client;
use serde::Deserialize;
use serial::prelude::*;
use std::io;
use std::str::from_utf8;
use std::time::Duration;
use serial::prelude::*;
fn main() {
let mut port = serial::open("/dev/ttyACM0").unwrap();
interact(&mut port).unwrap();
struct BookData {
title: String,
authors: Vec<String>,
thumbnail: String,
description: String,
}
#[allow(unused)]
#[derive(Debug, Deserialize)]
struct BookRoot {
items: Vec<Item>,
}
fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> {
#[allow(unused)]
#[derive(Debug, Deserialize)]
struct Item {
volumeinfo: VolumeInfo,
}
#[allow(unused)]
#[derive(Debug, Deserialize)]
struct VolumeInfo {
title: String,
authors: Option<Vec<String>>,
description: Option<String>,
imagelinks: Option<ImageLinks>,
}
#[allow(unused)]
#[derive(Debug, Deserialize)]
struct ImageLinks {
thumbnail: Option<String>,
}
#[tokio::main]
async fn main() {
let mut port = serial::open("/dev/ttyACM0").unwrap();
interact(&mut port).await.unwrap();
}
async fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> {
port.reconfigure(&|settings| {
settings.set_baud_rate(serial::Baud9600)?;
settings.set_char_size(serial::Bits8);
@ -23,12 +56,29 @@ fn interact<T: SerialPort>(port: &mut T) -> io::Result<()> {
port.set_timeout(Duration::from_millis(1000000000))?;
loop {
let mut buf: &mut [u8; 14] = &mut [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
let output = String::new();
port.read_exact(buf);
/* for i in buf.iter() {
let output = format!("{output} {}", i);
}*/
println!("{}", from_utf8(buf).unwrap());
let buf: &mut [u8; 14] = &mut [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
port.read_exact(buf).unwrap();
let barcode_id = from_utf8(buf).unwrap();
println!("{}", &barcode_id);
if barcode_id.starts_with("978") {
get_book_data(barcode_id.to_string()).await;
}
}
}
async fn get_book_data(isbn: String) {
let lookup_url = format!(
"https://www.googleapis.com/books/v1/volumes?q=isbn:{}",
isbn
);
let cleint = Client::new();
let book_data = cleint
.get(lookup_url)
.send()
.await
.unwrap()
.json::<BookRoot>()
.await
.unwrap();
println!("{:?}", book_data);
}