Skip to content

Commit

Permalink
Update with itemId and tweakers
Browse files Browse the repository at this point in the history
  • Loading branch information
lucemans committed Nov 26, 2024
1 parent d38c4ff commit f6f6ec9
Show file tree
Hide file tree
Showing 12 changed files with 523 additions and 39 deletions.
383 changes: 347 additions & 36 deletions engine/Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ poem-openapi = { version = "5", git = "https://github.com/poem-web/poem", branch
"redoc",
"static-files",
] }
regex = "1.11.1"
reqwest = "0.12.5"
scraper = "0.21.0"
serde = "1.0.204"
serde_json = "1.0.120"
serde_with = { version = "3.9.0", features = ["json", "chrono"] }
Expand All @@ -41,6 +43,7 @@ sqlx = { version = "0.7.4", features = [
"ipnetwork",
] }
terminal-banner = "0.4.1"
thiserror = "2.0.3"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
url = { version = "2.5.2", features = ["serde"] }
Expand Down
1 change: 1 addition & 0 deletions engine/src/ingress/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod product;
43 changes: 43 additions & 0 deletions engine/src/ingress/product/1855004.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"@type": "Product",
"@id": "https://tweakers.net/pricewatch/1855004/anker-737-power-bank-powercore-24k.html#Product-1855004",
"name": "Anker 737 Power Bank (PowerCore 24K)",
"@context": "https://schema.org",
"url": "https://tweakers.net/pricewatch/1855004/anker-737-power-bank-powercore-24k.html",
"brand": {
"@type": "Brand",
"name": "Anker",
"url": "https://tweakers.net/merk/2742/anker/"
},
"image": [
"https://tweakers.net/ext/i/2005317900.webp",
"https://tweakers.net/ext/i/2005565422.jpeg",
"https://tweakers.net/ext/i/2006644124.jpeg",
"https://tweakers.net/ext/i/2006644126.jpeg",
"https://tweakers.net/ext/i/2006644128.jpeg",
"https://tweakers.net/ext/i/2006644130.jpeg",
"https://tweakers.net/ext/i/2006644132.jpeg",
"https://tweakers.net/ext/i/2006644134.jpeg",
"https://tweakers.net/ext/i/2006644136.jpeg"
],
"gtin13": [
"0194644098728"
],
"mpn": [
"a1289",
"A1289011"
],
"description": "1x USB A, 2x USB type-C",
"aggregateRating": {
"@type": "AggregateRating",
"ratingValue": 3.5,
"ratingCount": 4
},
"offers": {
"@type": "AggregateOffer",
"lowPrice": 84,
"highPrice": 153,
"offerCount": 15,
"priceCurrency": "EUR"
}
}
1 change: 1 addition & 0 deletions engine/src/ingress/product/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod tweakers;
56 changes: 56 additions & 0 deletions engine/src/ingress/product/tweakers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use scraper::{Html, Selector};
use serde::{Deserialize, Serialize};
use thiserror::Error;

// "{\"@type\":\"Product\",\"@id\":\"https:\\/\\/tweakers.net\\/pricewatch\\/1855004\\/anker-737-power-bank-powercore-24k.html#Product-1855004\",\"name\":\"Anker 737 Power Bank (PowerCore 24K)\",\"@context\":\"https:\\/\\/schema.org\",\"url\":\"https:\\/\\/tweakers.net\\/pricewatch\\/1855004\\/anker-737-power-bank-powercore-24k.html\",\"brand\":{\"@type\":\"Brand\",\"name\":\"Anker\",\"url\":\"https:\\/\\/tweakers.net\\/merk\\/2742\\/anker\\/\"},\"image\":[\"https:\\/\\/tweakers.net\\/ext\\/i\\/2005317900.webp\",\"https:\\/\\/tweakers.net\\/ext\\/i\\/2005565422.jpeg\",\"https:\\/\\/tweakers.net\\/ext\\/i\\/2006644124.jpeg\",\"https:\\/\\/tweakers.net\\/ext\\/i\\/2006644126.jpeg\",\"https:\\/\\/tweakers.net\\/ext\\/i\\/2006644128.jpeg\",\"https:\\/\\/tweakers.net\\/ext\\/i\\/2006644130.jpeg\",\"https:\\/\\/tweakers.net\\/ext\\/i\\/2006644132.jpeg\",\"https:\\/\\/tweakers.net\\/ext\\/i\\/2006644134.jpeg\",\"https:\\/\\/tweakers.net\\/ext\\/i\\/2006644136.jpeg\"],\"gtin13\":[\"0194644098728\"],\"mpn\":[\"a1289\",\"A1289011\"],\"description\":\"1x USB A, 2x USB type-C\",\"aggregateRating\":{\"@type\":\"AggregateRating\",\"ratingValue\":3.5,\"ratingCount\":4},\"offers\":{\"@type\":\"AggregateOffer\",\"lowPrice\":84,\"highPrice\":153,\"offerCount\":15,\"priceCurrency\":\"EUR\"}}"

#[derive(Debug, Serialize, Deserialize)]
pub struct TweakerProduct {
pub id: String,
}

#[derive(Debug, Error)]
pub enum TweakerError {
#[error(transparent)]
Request(#[from] reqwest::Error),
}

// Fetch https://tweakers.net/pricewatch/1855004/anker-737-power-bank-powercore-24k/specificaties/
// and parse the application/ld+json
pub async fn get_by_tweaker_id(tweaker_id: String) -> Result<TweakerProduct, TweakerError> {

let response = reqwest::get(format!("https://tweakers.net/pricewatch/{}/specificaties/", tweaker_id)).await.unwrap();
let body = response.text().await.unwrap();

// println!("{}", body);
// // regex match for <script type="application/ld+json"> ... </script>
// let re = regex::Regex::new(r#"<script type="application/ld\+json">\s*?(.*?)\s*?</script>"#).unwrap();
// let captures = re.captures(&body).unwrap();

// for capture in captures.iter() {
// println!("{}", capture.unwrap().as_str());
// }

let document = Html::parse_document(&body);

// Define a selector for <script> tags with type="application/ld+json"
let selector = Selector::parse(r#"script[type="application/ld+json"]"#).unwrap();

// Extract the content of matching <script> tags
let ld_json_scripts: Vec<String> = document
.select(&selector)
.filter_map(|element| element.text().next().map(|text| text.to_string()))
.collect();

println!("{:?}", ld_json_scripts);

Ok(TweakerProduct {
id: "123".to_string(),
})
}

#[async_std::test]
async fn test_get_by_tweaker_id() {
let product = get_by_tweaker_id("1855004".to_string()).await.unwrap();
println!("{:?}", product);
}
1 change: 1 addition & 0 deletions engine/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod auth;
mod state;
mod models;
mod routes;
mod ingress;
mod database;

#[async_std::main]
Expand Down
56 changes: 55 additions & 1 deletion web/src/routeTree.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import { createFileRoute } from '@tanstack/react-router'

import { Route as rootRoute } from './routes/__root'
import { Route as SessionsImport } from './routes/sessions'
import { Route as ItemIdImport } from './routes/$itemId'
import { Route as ItemItemIdIndexImport } from './routes/item/$itemId/index'
import { Route as ItemItemIdEditImport } from './routes/item/$itemId/edit'

// Create Virtual Routes

Expand All @@ -38,11 +41,26 @@ const SessionsRoute = SessionsImport.update({
getParentRoute: () => rootRoute,
} as any)

const ItemIdRoute = ItemIdImport.update({
path: '/$itemId',
getParentRoute: () => rootRoute,
} as any)

const IndexLazyRoute = IndexLazyImport.update({
path: '/',
getParentRoute: () => rootRoute,
} as any).lazy(() => import('./routes/index.lazy').then((d) => d.Route))

const ItemItemIdIndexRoute = ItemItemIdIndexImport.update({
path: '/item/$itemId/',
getParentRoute: () => rootRoute,
} as any)

const ItemItemIdEditRoute = ItemItemIdEditImport.update({
path: '/item/$itemId/edit',
getParentRoute: () => rootRoute,
} as any)

// Populate the FileRoutesByPath interface

declare module '@tanstack/react-router' {
Expand All @@ -54,6 +72,13 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof IndexLazyImport
parentRoute: typeof rootRoute
}
'/$itemId': {
id: '/$itemId'
path: '/$itemId'
fullPath: '/$itemId'
preLoaderRoute: typeof ItemIdImport
parentRoute: typeof rootRoute
}
'/sessions': {
id: '/sessions'
path: '/sessions'
Expand All @@ -75,16 +100,33 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof CreateLazyImport
parentRoute: typeof rootRoute
}
'/item/$itemId/edit': {
id: '/item/$itemId/edit'
path: '/item/$itemId/edit'
fullPath: '/item/$itemId/edit'
preLoaderRoute: typeof ItemItemIdEditImport
parentRoute: typeof rootRoute
}
'/item/$itemId/': {
id: '/item/$itemId/'
path: '/item/$itemId'
fullPath: '/item/$itemId'
preLoaderRoute: typeof ItemItemIdIndexImport
parentRoute: typeof rootRoute
}
}
}

// Create and export the route tree

export const routeTree = rootRoute.addChildren({
IndexLazyRoute,
ItemIdRoute,
SessionsRoute,
AboutLazyRoute,
CreateLazyRoute,
ItemItemIdEditRoute,
ItemItemIdIndexRoute,
})

/* prettier-ignore-end */
Expand All @@ -96,14 +138,20 @@ export const routeTree = rootRoute.addChildren({
"filePath": "__root.tsx",
"children": [
"/",
"/$itemId",
"/sessions",
"/about",
"/create"
"/create",
"/item/$itemId/edit",
"/item/$itemId/"
]
},
"/": {
"filePath": "index.lazy.tsx"
},
"/$itemId": {
"filePath": "$itemId.tsx"
},
"/sessions": {
"filePath": "sessions.tsx"
},
Expand All @@ -112,6 +160,12 @@ export const routeTree = rootRoute.addChildren({
},
"/create": {
"filePath": "create.lazy.tsx"
},
"/item/$itemId/edit": {
"filePath": "item/$itemId/edit.tsx"
},
"/item/$itemId/": {
"filePath": "item/$itemId/index.tsx"
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions web/src/routes/$itemId.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createFileRoute } from '@tanstack/react-router';

export const Route = createFileRoute('/$itemId')({
component: () => <div>Hello /$itemId!</div>,
});
3 changes: 1 addition & 2 deletions web/src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { createRootRoute, Outlet } from '@tanstack/react-router';
import { TanStackRouterDevtools } from '@tanstack/router-devtools';

import { Navbar } from '../components/Navbar';

Expand All @@ -8,7 +7,7 @@ export const Route = createRootRoute({
<>
<Navbar />
<Outlet />
<TanStackRouterDevtools />
{/* <TanStackRouterDevtools /> */}
</>
),
});
5 changes: 5 additions & 0 deletions web/src/routes/item/$itemId/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createFileRoute } from '@tanstack/react-router';

export const Route = createFileRoute('/item/$itemId/edit')({
component: () => <div>Hello /item/$itemId/edit!</div>,
});
5 changes: 5 additions & 0 deletions web/src/routes/item/$itemId/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createFileRoute } from '@tanstack/react-router';

export const Route = createFileRoute('/item/$itemId/')({
component: () => <div>Hello /item/$itemId!</div>,
});

0 comments on commit f6f6ec9

Please sign in to comment.