Skip to content

Instantly share code, notes, and snippets.

@daleharvey
Last active August 12, 2019 12:23
Show Gist options
  • Save daleharvey/56f5c7599b312d7595787715b7fa7093 to your computer and use it in GitHub Desktop.
Save daleharvey/56f5c7599b312d7595787715b7fa7093 to your computer and use it in GitHub Desktop.
"use strict";
const fs = require("fs");
const glob = require("fast-glob");
const piexif = require("piexifjs");
const TEST_RUN = false;
const DATE_TIME_ORIGINAL = piexif.ExifIFD.DateTimeOriginal;
const META_DATA_FOLDER = "72157710213394221_30bc46431d95_part1";
function toDataUrl(path) {
let file = fs.readFileSync(path);
return "data:image/jpeg;base64," + file.toString('base64');
}
function readJSON(path) {
let rawdata = fs.readFileSync(path);
return JSON.parse(rawdata);
}
async function run() {
let files = await glob("data-download-*/*.jpg");
for (let file of files) {
let base64 = toDataUrl(file);
let meta, hasDate = true;
try {
meta = piexif.load(base64);
hasDate = DATE_TIME_ORIGINAL in meta.Exif;
} catch (e) {
console.log(`${file} has broken metadata`);
}
if (!hasDate) {
console.log(`Processsing ${file}`);
let id = file.split("_").reverse()[1];
let flickrMeta = readJSON(`${META_DATA_FOLDER}/photo_${id}.json`);
let date = flickrMeta.date_taken.replace(/-/g, ":");
console.log(` * adding date: ${date}`);
meta.Exif[DATE_TIME_ORIGINAL] = flickrMeta.date_taken;
let newExif = piexif.dump(meta);
let newFileData = piexif.insert(newExif, base64)
.split(';base64,').pop();
if (TEST_RUN) {
file = file.replace("_o.jpg", "_o_replaced.jpg");
}
fs.writeFileSync(file, newFileData, {encoding: 'base64'});
}
}
}
run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment