From a66805fb93a7a6ad698c12a16ba157b21fcb7f58 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 25 Dec 2022 21:05:31 +0100 Subject: export working --- oop2eindopdr/Pokedex.cpp | 16 +++++++--- oop2eindopdr/Pokedex.h | 10 +++--- oop2eindopdr/PokemonCard.cpp | 9 ++++++ oop2eindopdr/PokemonCard.h | 5 +++ oop2eindopdr/ZipExport.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++ oop2eindopdr/ZipExport.h | 21 +++++++++++++ oop2eindopdr/input.csv | 16 ---------- oop2eindopdr/main.cpp | 6 ++-- oop2eindopdr/readme.md | 6 ++-- 9 files changed, 132 insertions(+), 30 deletions(-) diff --git a/oop2eindopdr/Pokedex.cpp b/oop2eindopdr/Pokedex.cpp index c55469a..7cd2dc9 100644 --- a/oop2eindopdr/Pokedex.cpp +++ b/oop2eindopdr/Pokedex.cpp @@ -65,7 +65,7 @@ std::string Pokedex::lower(std::string input) { return out; } -std::vector Pokedex::search_cards_by_id_local(std::string query) { +std::vector Pokedex::search_cards_local(std::string query) { std::vector out(this->cards.size()); // https://cplusplus.com/reference/algorithm/copy_if/ @@ -78,7 +78,7 @@ std::vector Pokedex::search_cards_by_id_local(std::string query) { return out; } -std::vector Pokedex::search_cards_by_id_remote(std::string query) { +std::vector Pokedex::search_cards_remote(std::string query) { std::cout << "couldn't find card in cache, trying api..." << std::endl; std::vector api_cards = api->get_cards_by_query((std::string("name:\"") + query + "\" OR id:*" + query + "*").c_str()); std::vector out; @@ -99,10 +99,16 @@ std::vector Pokedex::search_cards_by_id_remote(std::string query) return out; } -std::vector Pokedex::search_cards_by_id(std::string query) { - std::vector out = search_cards_by_id_local(query); - if (out.size() == 0) out = search_cards_by_id_remote(query); +std::vector Pokedex::search_cards(std::string query) { + std::vector out = search_cards_local(query); + if (out.size() == 0) out = search_cards_remote(query); return out; } +PokemonCard* Pokedex::get_card_by_id(std::string id) { + for (PokemonCard* card : this->cards) + if (card->id == id) return card; + return nullptr; +} + diff --git a/oop2eindopdr/Pokedex.h b/oop2eindopdr/Pokedex.h index 5fc2fc9..ec4ac26 100644 --- a/oop2eindopdr/Pokedex.h +++ b/oop2eindopdr/Pokedex.h @@ -29,8 +29,8 @@ private: friend class PokemonCard; - virtual std::vector search_cards_by_id_local(std::string query); - virtual std::vector search_cards_by_id_remote(std::string query); + virtual std::vector search_cards_local(std::string query); + virtual std::vector search_cards_remote(std::string query); /** @brief convert std::string to lowercase */ std::string lower(std::string input); @@ -39,6 +39,8 @@ public: Pokedex(); virtual ~Pokedex(); - /** @brief search cards that contain `query` in id field */ - virtual std::vector search_cards_by_id(std::string query); + /** @brief search cards that contain `query` in id or name field */ + virtual std::vector search_cards(std::string query); + /** @brief get card with specific id */ + virtual PokemonCard* get_card_by_id(std::string id); }; diff --git a/oop2eindopdr/PokemonCard.cpp b/oop2eindopdr/PokemonCard.cpp index 9715fc3..95dc93c 100644 --- a/oop2eindopdr/PokemonCard.cpp +++ b/oop2eindopdr/PokemonCard.cpp @@ -107,3 +107,12 @@ void PokemonCard::raw_load_cache(const char* cache_path) { std::string PokemonCard::short_identifier() { return this->name + " (" + this->id + ")"; } + +std::string PokemonCard::image_location() { + return this->pokedex->cache->prefix_cache_path(prefix_cache_path("card.png").c_str()); +} + +std::string PokemonCard::image_location_hires() { + return this->pokedex->cache->prefix_cache_path(prefix_cache_path("card_hires.png").c_str()); +} + diff --git a/oop2eindopdr/PokemonCard.h b/oop2eindopdr/PokemonCard.h index 33a9d97..ea1a18f 100644 --- a/oop2eindopdr/PokemonCard.h +++ b/oop2eindopdr/PokemonCard.h @@ -53,6 +53,11 @@ public: /** @brief set cache */ virtual void set_pokedex(Pokedex* pokedex); + + /** @brief get (relative) path to card image */ + virtual std::string image_location(); + /** @brief get (relative) path to hires card image */ + virtual std::string image_location_hires(); }; struct PokemonCard::from_json : public PokemonCard { diff --git a/oop2eindopdr/ZipExport.cpp b/oop2eindopdr/ZipExport.cpp index e69de29..35c670f 100644 --- a/oop2eindopdr/ZipExport.cpp +++ b/oop2eindopdr/ZipExport.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#include "ZipExport.h" +#include "Pokedex.h" +#include "PokemonCard.h" + +ZipExport::ZipExport() { } +ZipExport::~ZipExport() { } + +ZipExport::ZipExport(Pokedex* pokedex, std::string input_csv, std::string output_zip) { + set_pokedex(pokedex); + import_csv(input_csv); + export_zip(output_zip); +} + +void ZipExport::set_pokedex(Pokedex* pokedex) { + this->pokedex = pokedex; +} + +void ZipExport::export_zip(std::string filename) { + this->zip_path = filename; + + std::vector zip_srcs; + zip_t* zip = zip_open(this->zip_path.c_str(), ZIP_CREATE | ZIP_EXCL, nullptr); + + std::string csv_content = "id,value\n"; + + for (std::string card_id : this->id_list) { + PokemonCard* card = this->pokedex->get_card_by_id(card_id); + if (card == nullptr) { + csv_content.append(card->id + "," + "???" + "\n"); + continue; + } + card->fetch_market_value(); + + csv_content.append(card->id + "," + std::to_string(card->value) + "\n"); + + zip_source* image = zip_source_file(zip, card->image_location_hires().c_str(), 0, 0); + zip_file_add(zip, std::string(card->id + ".png").c_str(), image, 0); + zip_srcs.push_back(image); + } + + zip_source* csv_file = zip_source_buffer(zip, csv_content.data(), csv_content.size(), 0); + zip_file_add(zip, "cards.csv", csv_file, 0); + + zip_close(zip); +} + +void ZipExport::import_csv(std::string filename) { + this->csv_path = filename; + + csv2::Reader, + csv2::quote_character<'"'>, + csv2::first_row_is_header, + csv2::trim_policy::trim_whitespace> csv; + + if (csv.mmap(this->csv_path)) { + for (const auto row: csv) { + for (const auto cell: row) { + std::string value; + cell.read_value(value); + this->id_list.push_back(value); + } + } + } else { + std::cout << "parsing csv failed!" << std::endl; + exit(EXIT_FAILURE); + } +} + diff --git a/oop2eindopdr/ZipExport.h b/oop2eindopdr/ZipExport.h index 45dcbb0..741ab4e 100644 --- a/oop2eindopdr/ZipExport.h +++ b/oop2eindopdr/ZipExport.h @@ -1,3 +1,24 @@ #pragma once +#include +#include +class Pokedex; + +class ZipExport { +private: + Pokedex* pokedex; + + std::string csv_path, zip_path; + std::vector id_list; + +public: + ZipExport(); + ZipExport(Pokedex* pokedex, std::string input_csv, std::string output_zip); + virtual ~ZipExport(); + + virtual void set_pokedex(Pokedex* pokedex); + + virtual void import_csv(std::string filename); + virtual void export_zip(std::string filename); +}; diff --git a/oop2eindopdr/input.csv b/oop2eindopdr/input.csv index 1b8f133..ffb1321 100644 --- a/oop2eindopdr/input.csv +++ b/oop2eindopdr/input.csv @@ -1,21 +1,5 @@ id swshp-SWSH001 swshp-SWSH002 -swshp-SWSH003 -swshp-SWSH004 -swshp-SWSH005 -swshp-SWSH006 -swshp-SWSH007 -swshp-SWSH008 -swshp-SWSH009 -swshp-SWSH010 -swshp-SWSH011 -swshp-SWSH012 -swshp-SWSH013 -swshp-SWSH014 -swshp-SWSH015 -swshp-SWSH016 -swshp-SWSH017 -swshp-SWSH018 swshp-SWSH019 swshp-SWSH020 diff --git a/oop2eindopdr/main.cpp b/oop2eindopdr/main.cpp index 4cd99ec..9a8c352 100644 --- a/oop2eindopdr/main.cpp +++ b/oop2eindopdr/main.cpp @@ -3,6 +3,7 @@ #include "Pokedex.h" #include "PokemonCard.h" +#include "ZipExport.h" using std::endl; using std::cout; @@ -19,7 +20,7 @@ int interactive_mode() { cout << "card id or name?: "; if (!std::getline(cin, search_query)) break; - std::vector cards = g_pokedex->search_cards_by_id(search_query); + std::vector cards = g_pokedex->search_cards(search_query); size_t card_count = cards.size(); if (card_count == 0) { @@ -44,7 +45,8 @@ int export_mode(int argc, char** argv) { if (!std::getline(cin, zip_file)) break; } - cout << "export mode! let's convert " << csv_file << " to " << zip_file << endl; + cout << "exporting cards in " << csv_file << " to " << zip_file << endl; + ZipExport zip_export(g_pokedex, csv_file, zip_file); return EXIT_SUCCESS; } diff --git a/oop2eindopdr/readme.md b/oop2eindopdr/readme.md index 35d4f1b..7c83614 100644 --- a/oop2eindopdr/readme.md +++ b/oop2eindopdr/readme.md @@ -8,13 +8,13 @@ combinatie readme en kladblok tijdens ontwikkeling ## gebruikte libraries - versies moeten nog gedocumenteerd worden -- er wordt er vanuit gegaan dat deze libraries als systeemheaders geïnstalleerd - zijn +- er wordt er vanuit gegaan dat deze libraries als + systeemheaders/systeemlibraries (globaal) geïnstalleerd zijn - [nlohmann/json](https://github.com/nlohmann/json) voor json (de)serializen - [cpr](https://github.com/libcpr/cpr) voor https requests - [libzip](https://libzip.org/) voor zip bestanden maken -- [csv2](https://github.com/p-ranav/csv2) voor csv bestanden (de)serializen +- [csv2](https://github.com/p-ranav/csv2) voor csv bestanden parsen ## functionaliteit (pseudocode) -- cgit v1.2.3