photosite/dist/post.js

106 lines
3.4 KiB
JavaScript

"use strict";
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 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.post();
}
async post() {
await this.bsky();
}
bskyDescription(photo) {
return `${photo.description}
${photo.format}${photo.filmstock !== 'Unknown' ? ' - ' + photo.filmstock : ''}`;
}
async bsky() {
const photo = await this.db.getBsky();
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",
},
*/
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})`);
console.dir(photo);
}
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