photosite/dist/post.js

124 lines
4.2 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
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");
const argparse_1 = require("argparse");
const moment_1 = __importDefault(require("moment"));
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' });
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;
this.post();
}
async post() {
await this.bsky();
}
bskyDescription(photo) {
let str = `${photo.description}
${photo.format}${photo.filmstock !== 'Unknown' ? ' - ' + photo.filmstock : ''}`;
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;
}
async bsky() {
const photo = await this.db.getBsky(this.random);
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",
},
*/
this.log.info(text);
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