Added download of book metadata
This commit is contained in:
parent
fd6fb1fda3
commit
2abb44408a
2
.gitignore
vendored
2
.gitignore
vendored
|
|
@ -1 +1,3 @@
|
||||||
/target
|
/target
|
||||||
|
/src-tauri
|
||||||
|
/dist
|
||||||
|
|
|
||||||
1626
Cargo.lock
generated
1626
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -5,3 +5,7 @@ edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
serial = "0.4.0"
|
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"] }
|
||||||
|
|
|
||||||
80
src/main.rs
80
src/main.rs
|
|
@ -1,17 +1,50 @@
|
||||||
extern crate serial;
|
use reqwest::Client;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serial::prelude::*;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::str::from_utf8;
|
use std::str::from_utf8;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use serial::prelude::*;
|
struct BookData {
|
||||||
|
title: String,
|
||||||
fn main() {
|
authors: Vec<String>,
|
||||||
let mut port = serial::open("/dev/ttyACM0").unwrap();
|
thumbnail: String,
|
||||||
interact(&mut port).unwrap();
|
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| {
|
port.reconfigure(&|settings| {
|
||||||
settings.set_baud_rate(serial::Baud9600)?;
|
settings.set_baud_rate(serial::Baud9600)?;
|
||||||
settings.set_char_size(serial::Bits8);
|
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))?;
|
port.set_timeout(Duration::from_millis(1000000000))?;
|
||||||
loop {
|
loop {
|
||||||
let mut buf: &mut [u8; 14] = &mut [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
|
let 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).unwrap();
|
||||||
port.read_exact(buf);
|
let barcode_id = from_utf8(buf).unwrap();
|
||||||
/* for i in buf.iter() {
|
println!("{}", &barcode_id);
|
||||||
let output = format!("{output} {}", i);
|
if barcode_id.starts_with("978") {
|
||||||
}*/
|
get_book_data(barcode_id.to_string()).await;
|
||||||
println!("{}", from_utf8(buf).unwrap());
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue