rework directory/query handling in aug-*

aug-e621: support moving/renaming files for non-deleted posts
master
Conduitry 3 months ago
parent dd05ab91b3
commit 95983cbffe
  1. 33
      _shared.js
  2. 21
      aug-e621.js

@ -70,19 +70,32 @@ async function *get_files(root) {
}
}
export const aug = async run => {
for (const query of process.argv.slice(2)) {
for await (const path of get_files(query)) {
const out = await run(query, path.slice(query.length + 1), type => new Promise(res => {
const hash = crypto.createHash(type);
fs.createReadStream(path).once('end', () => res(hash.digest())).pipe(hash);
}));
if (out) {
console.log(`${path} -> ${query}/${out.dest}`);
const aug_process = async (dir, query, run) => {
for await (const path of get_files(dir)) {
const out = await run(query, path.slice(dir.length + 1), type => new Promise(res => {
const hash = crypto.createHash(type);
fs.createReadStream(path).once('end', () => res(hash.digest())).pipe(hash);
}));
if (out) {
if (await fs.promises.access(`${dir}/${out.dest}`).then(() => true, () => false)) {
console.log(`${path} -> [deleted]`);
await fs.promises.unlink(path);
} else {
console.log(`${path} -> ${dir}/${out.dest}`);
const date_obj = new Date(out.date);
await fs.promises.utimes(path, date_obj, date_obj);
await fs.promises.rename(path, `${query}/${out.dest}`);
await fs.promises.rename(path, `${dir}/${out.dest}`);
}
}
}
};
export const aug = async run => {
if (process.argv.length === 2) {
await aug_process('.', null, run);
} else {
for (const arg of process.argv.slice(2)) {
await aug_process(arg, get_query(arg), run);
}
}
};

@ -2,23 +2,22 @@
import { aug, get } from './_shared.js';
const deleted_posts_lookup = {};
const get_deleted_posts = async query => {
const deleted_posts = [];
const get_all_posts = async query => {
const all_posts = [];
for (let page = null, posts; !posts || posts.length === 320; page = `b${posts[319]?.id}`) {
posts = (await (await get('https://e621.net/posts.json', { limit: 320, page, tags: `${query} status:deleted` })).json()).posts;
deleted_posts.push(...posts);
posts = (await (await get('https://e621.net/posts.json', { limit: 320, page, tags: `${query} status:any` })).json()).posts;
all_posts.push(...posts);
}
return deleted_posts;
return all_posts;
};
const all_posts_cache = Object.create(null);
aug(async (query, path, hash) => {
if (!/^[0-9]+-[0-9a-f]{32}\./.test(path)) {
const deleted_posts = await (deleted_posts_lookup[query] ??= get_deleted_posts(query));
const md5 = (await hash('md5')).toString('hex');
const post = deleted_posts.find(post => post.file.md5 === md5);
if (post) {
return { dest: `${post.id}-${post.file.md5}.${post.file.ext}`, date: post.created_at };
}
query ??= `md5:${md5}`;
const post = (await (all_posts_cache[query] ??= get_all_posts(query))).find(post => post.file.md5 === md5);
return post && { dest: `${post.id}-${post.file.md5}.${post.file.ext}`, date: post.created_at };
}
});

Loading…
Cancel
Save