Skip to content

Commit

Permalink
feat: total motion photos script
Browse files Browse the repository at this point in the history
  • Loading branch information
coltenkrauter committed Dec 17, 2024
1 parent 52d2bf5 commit 3143822
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "@krauters/immich-utils",
"description": " Scripts that wrangle the immich api to help you tame and manage your immich instance.",
"version": "0.0.0",
"version": "0.1.0",
"main": "dist/src/index.js",
"type": "module",
"scripts": {
"build": "tsc",
"dev": "npm run node -- ./src/index.ts",
"bulk-delete-assets-by-suffix": "npm run node -- ./src/bulk-delete-assets-by-suffix.ts",
"total-motion-photos": "npm run node -- ./src/total-motion-photos.ts",
"fix": "npm run lint -- --fix",
"lint": "npx eslint src/** test/**",
"prepare": "husky || true",
Expand Down Expand Up @@ -42,7 +43,7 @@
"@types/jest": "^29.5.14",
"husky": "^9.1.7",
"jest": "^29.7.0",
"nodemon": "^3.1.7",
"nodemon": "^3.1.9",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.7.2"
Expand All @@ -52,7 +53,7 @@
"scripts"
],
"dependencies": {
"@immich/sdk": "^1.122.2",
"@immich/sdk": "^1.123.0",
"@krauters/logger": "^1.4.3",
"@krauters/utils": "^1.3.0"
}
Expand Down
54 changes: 54 additions & 0 deletions src/total-motion-photos.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { init, searchAssets } from '@immich/sdk'

const apiKey = process.env.IMMICH_API_KEY
const baseUrl = process.env.IMMICH_BASE_URL

if (!apiKey || !baseUrl) {
throw new Error('Missing IMMICH_API_KEY or IMMICH_BASE_URL environment variables')
}

init({ apiKey, baseUrl })

async function* getMotionPhotoIds(): AsyncGenerator<string[]> {
let nextPage: string | null = '1'
let totalFound = 0

do {
try {
const response = await searchAssets({
metadataSearchDto: { isMotion: true, page: parseInt(nextPage) },
})

const assets = response.assets.items
if (assets && assets.length > 0) {
const motionPhotoIds = assets.map((asset: { id: string }) => asset.id)
totalFound += motionPhotoIds.length
console.log(`Running total of motion photos found [${totalFound}]`)
yield motionPhotoIds
}

nextPage = response.assets.nextPage
} catch (error) {
console.error(`Failed searching for motion photos with error [${error}]`)
break
}
} while (nextPage)
}

void (async () => {
console.log('Starting search for motion photos...')

const startTime = Date.now()
let totalMotionPhotos = 0

for await (const motionPhotoIds of getMotionPhotoIds()) {
console.log(`Found [${motionPhotoIds.length}] motion photos in this batch`)
totalMotionPhotos += motionPhotoIds.length
}

const elapsedTime = ((Date.now() - startTime) / 1000).toFixed(2)
console.log('--- Report ---')
console.log(`Total motion photos found: [${totalMotionPhotos}]`)
console.log(`Elapsed time: [${elapsedTime}] seconds`)
console.log('Search completed successfully')
})()

0 comments on commit 3143822

Please sign in to comment.