From 281f0984bb525bf727adae5e5d9c082a57a27205 Mon Sep 17 00:00:00 2001 From: Philippe Loctaux Date: Wed, 17 May 2023 22:25:08 +0200 Subject: [PATCH] added deno script to generate wallpapers metadata from images --- .gitignore | 4 ++- .vscode/settings.json | 7 +++++ package.json | 11 +++---- public/wallpapers/.gitkeep | 0 utils/wallpapers.ts | 60 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 public/wallpapers/.gitkeep create mode 100644 utils/wallpapers.ts diff --git a/.gitignore b/.gitignore index 95dbc0e..b20b3b2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# wallpapers +/public/wallpapers.json + # build output dist/ # generated types @@ -21,5 +24,4 @@ pnpm-debug.log* .DS_Store # ide -.vscode/ .idea/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..5785dd5 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,7 @@ +{ + "deno.enable": true, + "deno.unstable": true, + "deno.enablePaths":[ + "./utils/" + ] +} \ No newline at end of file diff --git a/package.json b/package.json index 9e20c21..8da3a00 100644 --- a/package.json +++ b/package.json @@ -3,11 +3,12 @@ "type": "module", "version": "0.0.1", "scripts": { - "dev": "astro dev", - "start": "astro dev", - "build": "astro build", - "preview": "astro preview", - "astro": "astro" + "dev": "npm run wallpapers && astro dev", + "start": "npm run wallpapers && astro dev", + "build": "npm run wallpapers && astro build", + "preview": "npm run wallpapers && astro preview", + "astro": "astro", + "wallpapers": "deno run --allow-read --allow-net --allow-write utils/wallpapers.ts" }, "dependencies": { "@astrojs/tailwind": "^3.1.2", diff --git a/public/wallpapers/.gitkeep b/public/wallpapers/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/utils/wallpapers.ts b/utils/wallpapers.ts new file mode 100644 index 0000000..f6cf748 --- /dev/null +++ b/utils/wallpapers.ts @@ -0,0 +1,60 @@ + +import exifr from "npm:exifr@^7.1.3"; +import { writeJson } from "https://deno.land/x/jsonfile@1.0.0/mod.ts"; + +const publicPath = "/public"; +const wallpapersPath = "/wallpapers"; +const imagesPath = `${Deno.cwd()}${publicPath}${wallpapersPath}`; + +interface MyWallpaper { + file: string; + location: string; + date: string; +} + +const wallpapers: MyWallpaper[] = []; + +// For each file in the wallpapers directory +for await (const dirEntry of Deno.readDir(imagesPath)) { + + if (!dirEntry.isFile || dirEntry.name === ".gitkeep") { + continue; + } + + // Open file + const path = `${imagesPath}/${dirEntry.name}`; + const bytes = await Deno.readFile(path); + + // Parse exif + const exif = await exifr.parse(bytes); + + // Timezones are too hard + const timestamp = Date.parse(exif.DateTimeOriginal); + const dateObject = new Date(timestamp); + const date = dateObject.toISOString().split("T")[0]; + + // Http request to get location + const response = await fetch( + `https://nominatim.openstreetmap.org/reverse?format=json&lat=${exif.latitude}&lon=${exif.longitude}&zoom=5`, + { headers: { "User-Agent": "https://philippeloctaux.com" } } + ); + const data = await response.json(); + const location = data?.display_name; + + // Final filename + const file = `${wallpapersPath}/${dirEntry.name}`; + + console.log(`Processed ${file}`); + + wallpapers.push({ + file, + date, + location, + }); + +} + +// write to /public/wallpapers.json +const jsonPath = `${Deno.cwd()}${publicPath}/wallpapers.json`; +await writeJson(jsonPath, wallpapers); +console.log(`Wrote to ${jsonPath}`);