photosite/dist/post.js

124 lines
4.2 KiB
JavaScript
Raw Normal View History

2024-12-11 07:03:46 +00:00
"use strict";
2024-12-21 05:13:39 +00:00
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
2024-12-11 07:03:46 +00:00
Object.defineProperty(exports, "__esModule", { value: true });
require("dotenv/config");
const log_1 = require("./log");
const promises_1 = require("fs/promises");
const path_1 = require("path");
const api_1 = require("@atproto/api");
2024-12-21 05:13:39 +00:00
const argparse_1 = require("argparse");
const moment_1 = __importDefault(require("moment"));
2024-12-11 07:03:46 +00:00
const db_1 = require("./db");
const env_1 = require("./env");
class Post {
constructor() {
this.log = (0, log_1.createLog)('post');
this.db = new db_1.DB();
this.bskyAgent = new api_1.BskyAgent({ service: 'https://bsky.social' });
2024-12-21 05:13:39 +00:00
this.random = false;
const parser = new argparse_1.ArgumentParser({
description: 'Generate script'
});
parser.add_argument('-r', '--random', { action: 'store_true', default: false, help: 'Choose one at random' });
const args = parser.parse_args();
this.random = args.random;
2024-12-11 07:03:46 +00:00
this.post();
}
async post() {
await this.bsky();
}
bskyDescription(photo) {
2024-12-21 05:13:39 +00:00
let str = `${photo.description}
2024-12-11 07:03:46 +00:00
${photo.format}${photo.filmstock !== 'Unknown' ? ' - ' + photo.filmstock : ''}`;
2024-12-21 05:13:39 +00:00
const created = moment_1.default.unix(photo.created / 1000);
const now = (0, moment_1.default)();
if (now.format('YYYY') !== created.format('YYYY')) {
str += '\n' + created.format('MMMM YYYY');
}
return str;
2024-12-11 07:03:46 +00:00
}
async bsky() {
2024-12-21 05:13:39 +00:00
const photo = await this.db.getBsky(this.random);
2024-12-11 07:03:46 +00:00
const identifier = (0, env_1.envString)('BSKY_USER', null);
const password = (0, env_1.envString)('BSKY_PASSWORD', null);
let text;
let post;
let imagePath;
let imageData;
let blobResponse;
let postData;
if (identifier === null || password === null) {
this.log.error(`Cannot post without Bluesky username or password`);
return;
}
if (photo === null) {
this.log.warn(`Nothing to post`);
return;
}
try {
await this.bskyAgent.login({ identifier, password });
this.log.info(`Logged into Bluesky`);
}
catch (err) {
this.log.error(`Error logging into Bluesky`, err);
return;
}
text = this.bskyDescription(photo);
/*
embed: {
images: [
{
image: testUpload.data.blob,
alt: "",
},
],
$type: "app.bsky.embed.images",
},
*/
2024-12-21 05:13:39 +00:00
this.log.info(text);
2024-12-11 07:03:46 +00:00
imagePath = (0, path_1.join)((0, env_1.envString)('WWW', './www'), 'img', `${photo.id}_full.jpg`);
try {
imageData = await (0, promises_1.readFile)(imagePath);
}
catch (err) {
this.log.error(`Error reading image data ${imagePath}`, err);
}
try {
blobResponse = await this.bskyAgent.uploadBlob(imageData, { contentType: 'image/jpeg' });
}
catch (err) {
this.log.error(`Error uploading blob to Bluesky`, err);
return;
}
postData = {
text,
embed: {
$type: 'app.bsky.embed.images',
images: [{
alt: photo.description,
image: blobResponse.data.blob
}]
}
};
try {
await this.bskyAgent.post(postData);
this.log.info(`Posted ${photo.description} (${photo.name})`);
}
catch (err) {
this.log.error(`Error making Bluesky post`, err);
return;
}
try {
await this.db.confirmBsky(photo.id);
}
catch (err) {
this.log.error(`Could not confirm that photo ${photo.name} was posted`, err);
return;
}
}
}
new Post();
//# sourceMappingURL=post.js.map