LinkedIn: keep company Page names on disk for 30 days
Restarts and reconnects no longer spend LinkedIn's small daily lookup limit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
db56b69ce6
commit
613727321d
@ -1,5 +1,8 @@
|
|||||||
// LinkedIn: post to a person's profile or to a company Page they administer.
|
// LinkedIn: post to a person's profile or to a company Page they administer.
|
||||||
// Company Pages need the Community Management API product on the LinkedIn app (approval required).
|
// Company Pages need the Community Management API product on the LinkedIn app (approval required).
|
||||||
|
import fs from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
|
import { PROJECT_ROOT } from "../env.js";
|
||||||
import { request, bytesOf, isVideo, sizeOf } from "./http.js";
|
import { request, bytesOf, isVideo, sizeOf } from "./http.js";
|
||||||
|
|
||||||
const API = "https://api.linkedin.com";
|
const API = "https://api.linkedin.com";
|
||||||
@ -48,8 +51,11 @@ async function uploadVideo(token, owner, src) {
|
|||||||
|
|
||||||
// Company Page names. LinkedIn caps lookups per app and member per day, so names are cached for a day and
|
// Company Page names. LinkedIn caps lookups per app and member per day, so names are cached for a day and
|
||||||
// fetched in batches of 50, from organizationsLookup first and the organizations resource if that is capped.
|
// fetched in batches of 50, from organizationsLookup first and the organizations resource if that is capped.
|
||||||
const NAME_TTL = 24 * 3600 * 1000;
|
// Kept on disk so restarts don't spend the day's lookups again.
|
||||||
const nameCache = new Map();
|
const NAME_TTL = 30 * 24 * 3600 * 1000;
|
||||||
|
const NAME_FILE = path.join(PROJECT_ROOT, "data", "linkedin-pages.json");
|
||||||
|
const nameCache = new Map((() => { try { return Object.entries(JSON.parse(fs.readFileSync(NAME_FILE, "utf8"))); } catch { return []; } })());
|
||||||
|
const saveNames = () => { try { fs.writeFileSync(NAME_FILE, JSON.stringify(Object.fromEntries(nameCache))); } catch { /* cache only */ } };
|
||||||
async function pageNames(token, ids) {
|
async function pageNames(token, ids) {
|
||||||
const names = {};
|
const names = {};
|
||||||
const missing = [];
|
const missing = [];
|
||||||
@ -71,6 +77,7 @@ async function pageNames(token, ids) {
|
|||||||
} catch { /* capped or not allowed: try the next resource, else keep ids */ }
|
} catch { /* capped or not allowed: try the next resource, else keep ids */ }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (missing.length) saveNames();
|
||||||
return names;
|
return names;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user