Skip to content

Commit

Permalink
Handle multitype items (#118)
Browse files Browse the repository at this point in the history
* Created serde_config

* Setting up db in env file (#117)

* Updated version and added README to subproject

* Improved unwrap and test loging

* Allowed ItemError for advanced items

* Fixed Advanced items test

* Improved README

* Fixed icons

* Removed T.o.C

* Centered title

* Updated Changelog and version number

* Small change for CHANGELOG

* Removing debug

* Implemented suggested change

* Removed unnecessary braces

* Updated .gitattributes

* make Json transparent in the error

* Improved README

---------

Co-authored-by: Pepe Márquez Romero <pepe.marquezromero@protonmail.com>
  • Loading branch information
0xCAB0 and pxp9 authored Nov 3, 2024
1 parent e9b2995 commit 42793b6
Show file tree
Hide file tree
Showing 19 changed files with 544 additions and 230 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copy this file as your .env
POSTGRES_DB=vinted-rs
POSTGRES_PASSWORD=postgres
POSTGRES_USER=postgres
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
static/*.html linguist-generated

# Mark all HTML files in scrapping/vinted-db-feeder/data/raw/ as generated
scrapping/vinted-db-feeder/data/raw/*.html linguist-generated
scrapping/vinted-db-feeder/data/raw/**/*.html linguist-generated
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ Cargo.lock
/**/results/
docker/query.sh

src/tests/output
src/tests/output
.env
19 changes: 13 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
# Changelog

# 0.9.2 (WIP)
# 0.10.0 (WIP)

## Fixed
[#115](https://github.com/ThalosES/vinted-rs/pull/115) : Rexported Redis crate
- Re-exported Redis crate [#115](https://github.com/ThalosES/vinted-rs/pull/115)

- Added support for fields that may come as boolean or integer [#118](https://github.com/ThalosES/vinted-rs/pull/118)

## Improved

- Removed hardcoded DB strings and introduced a `.env` file required for feature `Advanced Items` [#117](https://github.com/ThalosES/vinted-rs/pull/117)

# 0.9.1 (2024-09-24) [#111](https://github.com/TuTarea/vinted-rs/pull/111/)
⚠️Not using `develop` branch any more. Leaving main as "canary"/develop channel and using releases to declare _stable_ and _experimental_ versions

⚠️Not using [`develop`](https://github.com/ThalosES/vinted-rs/tree/develop) branch any more. Leaving main as "canary"/develop channel and using releases to declare _stable_ and _experimental_ versions

## Fixed
#108 : Updated Redis to 0.27.2
#105 : Updated TypeBuilder to 0.20
#109 : Solved vulnerable dependency for Db Feeder
- Updated Redis to 0.27.2 [#108](https://github.com/ThalosES/vinted-rs/pull/108)
- Updated TypeBuilder to 0.20 [#105](https://github.com/ThalosES/vinted-rs/pull/105)
- Solved vulnerable dependency for Db Feeder [#109](https://github.com/ThalosES/vinted-rs/pull/109)

# 0.9.0 (2024-07-28) [#97](https://github.com/TuTarea/vinted-rs/pull/97/)

Expand Down
13 changes: 9 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[workspace]
members = ["examples/filter_example", "."]

[package]
name = "vinted-rs"
version = "0.9.2"
version = "0.10.0"
edition = "2021"
repository = "https://github.com/TuTarea/vinted-rs"
authors = [
Expand Down Expand Up @@ -34,14 +37,16 @@ reqwest_cookie_store = "0.8"
typed-builder = "0.20"
fang = { version = "0.10.3", features = ["asynk"], default-features = false }
redis-macros = { version = "0.4.2", optional = true }
redis = { version = "0.27.2", optional = true, features = ["tokio-comp", "aio"] }
redis = { version = "0.27.5", optional = true, features = [
"tokio-comp",
"aio",
] }
serde_json = { version = "1.0.91" }
log = "0.4.20"
lazy_static = "1.4.0"
dotenvy = "0.15"
[dev-dependencies]
env_logger = "0.11.5"
redis-macros = { version = "0.4.2" }
redis = { version = "0.27.2" }


[dependencies.bb8-postgres]
Expand Down
15 changes: 10 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
include .env

db:
docker run --rm -d --name postgres -p 5432:5432 \
-e POSTGRES_DB=vinted-rs \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=postgres \
-e POSTGRES_DB=$(POSTGRES_DB) \
-e POSTGRES_USER=$(POSTGRES_USER) \
-e POSTGRES_PASSWORD=$(POSTGRES_PASSWORD) \
postgres:latest


diesel:
DATABASE_URL=postgres://postgres:postgres@localhost/vinted-rs diesel migration run
DATABASE_URL=postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@localhost/$(POSTGRES_DB) diesel migration run
stop:
docker kill postgres

clippy:
cargo clippy --all-features
cargo clippy --all-features

env:

125 changes: 73 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,96 +1,117 @@
# Vinted-rs: A Vinted API wrapper
<div align="center">

# Vinted-rs: A Vinted API wrapper in Rust

[![github]](https://github.com/TuTarea/vinted-rs/)&ensp;[![crates-io]](https://crates.io/crates/vinted-rs)&ensp;[![docs-rs]](https://docs.rs/vinted-rs/latest/vinted_rs/)

[github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
[crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
[docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs

## Table of Contents

- [Vinted-rs: A Vinted API wrapper](#vinted-rs-a-vinted-api-wrapper)
- [Table of Contents](#table-of-contents)
- [Installation](#installation)
- [DB setup](#db-setup)
- [Create a migration](#create-a-migration)
- [Run a Docker container with PostgreSQL](#run-a-docker-container-with-postgresql)
- [Run migrations](#run-migrations)
- [Stop DB](#stop-db)
- [Running Tests](#running-tests)
</div>

## Installation

Via `cargo` you can add the library to your project's `Cargo.toml`

```toml
[dependencies]
vinted-rs = "0.9.1"
vinted-rs = { version = "0.10.0",
#features = ["advanced_filters", "redis"]
}
```

## DB setup
## Features

| Feature | Description | Example |
| ------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------- |
| [Advanced Filters](#advanced-filters) | Uses the data pulled by the [scrapping module](./scrapping/vinted-db-feeder/), which is stored in the diesel [migrations](./migrations/) folder. | [](./examples/filter_example/) |
| [Redis](#redis) | Allows recovered results to be cached using a Redis instance ||

### Advanced filters

> This feature requires [setting up a Postgres Database](#database-set-up) <code><img width="3%" src="https://raw.githubusercontent.com/yurijserrano/Github-Profile-Readme-Logos/refs/heads/master/databases/postgresql.svg"></code>
Uses the data pulled by the [scrapping module](./scrapping/vinted-db-feeder/), which is stored in the diesel [migrations](./migrations/) folder.

#### Environment set-up

1. Copy the `.env.example`

```sh
cp .env.example .env
```

2. Modify the variables to your liking

#### Database set-up
Advanced filtering features must require this setup before running.

- First start installing diesel-cli (in order to run the migrations in PostgreSQL database)
1. ⚠️ `diesel-cli` installation may fail if you do not have `libpq` library installed. To install `libpq`, just install PostgreSQL package on your machine.

⚠️**Very important:** diesel-cli installation may fail if you do not have `libpq` library installed.
- In `Arch` based is only necessary to install this package.

To install `libpq`, just install PostgreSQL package on your machine.
```bash
sudo pacman -S postgresql-libs
```

In `Arch` based is only necessary to install this package.
- In `Debian` based distributions is only necessary to install this package.

```bash
sudo pacman -S postgresql-libs
```
```bash
sudo apt install libpq-dev
```

In `Debian` based distributions is only necessary to install this package.
2. Install `diesel-cli` in order to run the migrations in PostgreSQL database

```bash
cargo install diesel_cli --features=postgres --no-default-features
```

```bash
sudo apt install libpq-dev
```
**Available interactions** (See [Makefile](./Makefile))

```bash
cargo install diesel_cli --features=postgres --no-default-features
```
1. Create a migration

### Create a migration
```bash
mkdir -p migrations #
diesel migration generate my_migration
```

```bash
mkdir migrations
```
Program after that `up.sql` and `down.sql` scripts.

```bash
diesel migration generate my_migration
```
2. Run a Docker container with PostgreSQL

Program after that `up.sql` and `down.sql` scripts.
- See in [Makefile](https://github.com/ThalosES/vinted-rs/blob/main/Makefile)

### Run a Docker container with PostgreSQL
```bash
make db
```

- See in [Makefile](https://github.com/TuTarea/vinted-rs/blob/main/Makefile)
3. Run migrations

```bash
make db
```
```bash
make diesel
```

### Run migrations
4. Stop DB

```bash
make diesel
```
```bash
make stop
```

#### Testing set-up

### Stop DB
> This step requires completing the [DB setup](#database-set-up)

```bash
make stop
cargo test
```

## Running Tests
### Redis

⚠️**Very important:** Before running tests is important to do the [DB setup](#db-setup)
This feature allows recovered results to be cached using a Redis instance. <code><img width="4%" src="https://raw.githubusercontent.com/yurijserrano/Github-Profile-Readme-Logos/refs/heads/master/databases/redis.svg"></code>

Then run the tests
A development instance can be created using:

```bash
cargo test
```
make cache
```
2 changes: 2 additions & 0 deletions examples/filter_example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.venv
result/
12 changes: 9 additions & 3 deletions examples/filter_example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
[package]
name = "filter_example"
version = "0.1.0"
version = "0.10.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
vinted-rs = { path = "../../", features = ["advanced_filters"]}
bb8-postgres = {version = "0.8", features = ["with-serde_json-1" , "with-uuid-1" , "with-chrono-0_4"]}
vinted-rs = { path = "../../", features = ["advanced_filters"] }
bb8-postgres = { version = "0.8", features = [
"with-serde_json-1",
"with-uuid-1",
"with-chrono-0_4",
] }
tokio = { version = "1", features = ["full"] }
dotenvy = { version = "0.15.7" }
lazy_static = { version = "1.4.0" }
20 changes: 20 additions & 0 deletions examples/filter_example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Advanced filter example project

## Rust set-up

Refer to the [main README *Install* section](../../README.md#installation) and its subsections

## Python set-up

1. Create a Virtual Environment

```bash
python -m venv .venv
```

2. Install the requirements

```bash
source .venv/bin/activate #linux
pip install -r requirements.txt
```
31 changes: 24 additions & 7 deletions examples/filter_example/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
use bb8_postgres::tokio_postgres::NoTls;

use lazy_static::lazy_static;
use std::env;
use vinted_rs::{db::DbController, queries::Host, Filter, VintedWrapper};

lazy_static! {
pub static ref POSTGRES_DB: String =
std::env::var("POSTGRES_DB").unwrap_or(String::from("vinted-rs"));
pub static ref POSTGRES_USER: String =
std::env::var("POSTGRES_USER").unwrap_or(String::from("postgres"));
pub static ref POSTGRES_PASSWORD: String =
std::env::var("POSTGRES_PASSWORD").unwrap_or(String::from("postgres"));
}

#[tokio::main]
async fn main() {
let args: Vec<String> = env::args().collect();

let _ = dotenvy::dotenv(); //Load at runtime

if args.len() < 2 {
println!("Please provide the host as a command-line parameter.");
return;
Expand All @@ -15,9 +26,19 @@ async fn main() {
let host_arg = args[1].as_str();
let host: Host = host_arg.into();

let db = DbController::new("postgres://postgres:postgres@localhost/vinted-rs", 5, NoTls)
let vinted = VintedWrapper::new_with_host(host);
println!("Host: {}", vinted.get_host());

let db_uri = &format!(
"postgres://{}:{}@localhost/{}?sslmode=disable",
POSTGRES_USER.clone(),
POSTGRES_PASSWORD.clone(),
POSTGRES_DB.clone()
);

let db = DbController::new(&db_uri, 5, NoTls)
.await
.unwrap();
.expect("Broken connection to Database, please set it up correctly");

let adidas = db.get_brand_by_name(&"Adidas").await.unwrap();
let nike = db.get_brand_by_name(&"Nike").await.unwrap();
Expand All @@ -30,10 +51,6 @@ async fn main() {
.price_to(Some(20.0))
.build();

let vinted = VintedWrapper::new_with_host(host);

println!("Host: {}", vinted.get_host());

let items = vinted
.get_items(&filter, 10, None, None, None)
.await
Expand Down
Loading

0 comments on commit 42793b6

Please sign in to comment.