From 0727f6092729047bdfe54171d29618bca183252c Mon Sep 17 00:00:00 2001 From: Ashik K Date: Sat, 11 Mar 2023 11:54:59 +0100 Subject: [PATCH] Add C++ code and tooling for exif data genaration --- genallexif | 5 +++ parser.cc | 100 +++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 98 insertions(+), 7 deletions(-) create mode 100755 genallexif diff --git a/genallexif b/genallexif new file mode 100755 index 0000000..de9ad55 --- /dev/null +++ b/genallexif @@ -0,0 +1,5 @@ +for f in "$1"/*.jpeg + do + exiftool -j "`echo $f`" > "`echo $f`.exif.json" + done +exit \ No newline at end of file diff --git a/parser.cc b/parser.cc index b5dbd61..a6ce624 100644 --- a/parser.cc +++ b/parser.cc @@ -7,17 +7,23 @@ #include #include +#define debug_level 0 std::string yearstr(int year) { return year==-1? "Unknown": std::to_string(year); } -int main() +int main(int argc, char **argv) { + bool download_mode =false; + if (argc == 2) { + std::string arg1 = argv[1]; + download_mode = arg1=="--download"; + } char filename[64]; snprintf(filename, sizeof filename, "out.csv"); std::ofstream out_file1; out_file1.open(filename); - out_file1<<"id, Caption/title, production start year, end year, Description, Source, image_filename, collection name, museum name, linceses\n"; + out_file1<<"id, Caption/title, production start year, end year, Description, Source, image_filename, subjects, date published, collection name, museum name, exif_model, exif_iso, exif_focallength, exif_exposuretime, exif_aperture, exif_datetimeoriginal, liceses\n"; for (auto i = 0; i<4; i++) { char in_file_i[64]; snprintf(in_file_i, sizeof in_file_i, "data_%d.json", i); @@ -54,7 +60,6 @@ int main() const rapidjson::Value& response = doc["response"]; if (response.HasMember("docs") && response["docs"].IsArray()) { - std::cout<<"yes, response is an array"< 0) std::cout<0) std::cout<<"Found CC by license, fetched the image for "<< mediaid<<"\n"; + } } + if (is_cc_license) { + char exif_file[128]; + snprintf(exif_file, sizeof exif_file, "%s-%s.jpeg.exif.json", article_id.c_str(), mediaid.c_str()); + // Open the file + FILE* exiffp = fopen(exif_file, "rb"); + if (!exiffp) { + std::cerr << "Error: unable to open file" << std::string(exif_file) + << std::endl; + return -1; + } + + + char exifreadBuffer[4096]; + rapidjson::FileReadStream exifis(exiffp, exifreadBuffer, sizeof(exifreadBuffer)); + rapidjson::Document exifdoc; + exifdoc.ParseStream(exifis); + + // Check if the document is valid + if (exifdoc.HasParseError()) { + std::cerr << "Error: failed to parse JSON document exif data" + << std::endl; + } + fclose(exiffp); + + std::string exif_model, exif_iso, exif_focallength, exif_exposuretime, exif_aperture, exif_fnumber, exif_datetimeoriginal; + if (exifdoc.IsArray()) { + if (exifdoc[0].IsObject()) { + if (exifdoc[0].GetObject().HasMember("Model")) { + exif_model = exifdoc[0].GetObject()["Model"].GetString(); + } + if (exifdoc[0].GetObject().HasMember("ISO")) { + exif_iso = std::to_string(exifdoc[0].GetObject()["ISO"].GetInt()); + } + if (exifdoc[0].GetObject().HasMember("FocalLength")) { + exif_focallength = exifdoc[0].GetObject()["FocalLength"].GetString(); + } + if (exifdoc[0].GetObject().HasMember("ExposureTime")) { + if (exifdoc[0].GetObject()["ExposureTime"].IsString()) + exif_exposuretime = exifdoc[0].GetObject()["ExposureTime"].GetString(); + } + if (exifdoc[0].GetObject().HasMember("ApertureValue")) { + exif_aperture = std::to_string(exifdoc[0].GetObject()["ApertureValue"].GetDouble()); + } + if (exifdoc[0].GetObject().HasMember("DateTimeOriginal")) { + exif_datetimeoriginal = exifdoc[0].GetObject()["DateTimeOriginal"].GetString(); + } + } + } + + out_file1<< /* article["artifact.defaultMediaIdentifier"].GetString()<< ", "<< article["artifact.defaultPictureIndex"].GetInt()<< @@ -156,11 +234,19 @@ int main() ", "<< title << ", "<< yearstr(yearb) << ", "<< yearstr(yeare) << - ", "<< descfilename << + ", "<< description << ", "<< imglink << ", "<< article_id+"-"+ mediaid +".jpeg" << + ", "<< subjects << + ", "<< publishdate << ", "<<"Länge leve Kosta! exhibition" << ", "<<"Kulturparken Småland / Smålands museum" << + ", "<< exif_model << + ", "<< exif_iso << + ", "<< exif_focallength << + ", "<< exif_exposuretime << + ", "<< exif_aperture << + ", "<< exif_datetimeoriginal<< ", "<< license << std::endl; }