Run posts

This commit is contained in:
Matt McWilliams 2024-12-11 02:03:46 -05:00
parent 4ff8a61c21
commit c736d6ae7a
9 changed files with 285 additions and 2 deletions

View File

@ -13,4 +13,6 @@ ARTIST="Unknown"
GEOCODE_API_KEY=""
TITLE=""
H1=""
DEST="remote:/dir/"
DEST="remote:/dir/"
BSKY_USER=""
BSKY_PASSWORD=""

23
dist/db/index.js vendored
View File

@ -99,6 +99,29 @@ class DB {
}
return rows;
}
async getBsky() {
const query = `SELECT * FROM photos WHERE bsky = 0 ORDER BY score ASC, created DESC LIMIT 1;`;
let rows = [];
try {
rows = await this.all(query);
}
catch (err) {
this.log.error(`Error getting all photos`, err);
}
if (rows.length === 0) {
return null;
}
return rows[0];
}
async confirmBsky(id) {
const query = `UPDATE photos SET bsky = 1 WHERE id = ?;`;
try {
await this.run(query, [id]);
}
catch (err) {
throw err;
}
}
async cacheLocation(location, latlng) {
const query = `INSERT OR IGNORE INTO geocode (location, latitude, longitude) VALUES (?, ?, ?);`;
try {

View File

@ -1 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/db/index.ts"],"names":[],"mappings":";;;AAAA,yBAAuB;AAEvB,gCAAmC;AAEnC,qCAAmC;AACnC,gCAAmC;AA6BnC,MAAa,EAAE;IAId;QACC,MAAM,YAAY,GAAY,IAAA,eAAS,EAAC,IAAI,EAAE,cAAc,CAAC,CAAC;QAC9D,IAAI,CAAC,GAAG,GAAG,IAAA,eAAS,EAAC,IAAI,CAAC,CAAC;QAC3B,IAAI,CAAC,EAAE,GAAG,IAAI,kBAAQ,CAAC,YAAY,CAAC,CAAC;QACrC,IAAI,CAAC,EAAE,CAAC,GAAG,CAAE,4BAA4B,CAAC,CAAC;QAC3C,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,YAAY,EAAE,CAAC,CAAC;IACtC,CAAC;IAEO,KAAK,CAAC,GAAG,CAAE,KAAc,EAAE,OAAe,EAAE;QACnD,OAAO,IAAI,OAAO,CAAC,CAAC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C,GAAG,CAAC,KAAK,EAAE,CAAE,EAAE,CAAE,CAAC,CAAC;QAC/B,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,MAAM,GAAG,CAAC;QACX,CAAC;IACF,CAAC;IAEM,KAAK,CAAC,aAAa,CAAE,QAAiB,EAAE,MAAe;QAC7D,MAAM,KAAK,GAAY,iFAAiF,CAAC;QACzG,IAAI,CAAC;YACJ,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC;QACtE,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,QAAQ;QACT,CAAC;IACF,CAAC;IAEM,KAAK,CAAC,WAAW,CAAE,QAAiB;QAC1C,MAAM,KAAK,GAAa,qEAAqE,CAAC;QAC9F,IAAI,IAAI,GAAW,EAAE,CAAC;QACtB,IAAI,GAAG,GAAY,IAAI,CAAC;QACxB,IAAI,CAAC;YACJ,IAAI,GAAG,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;QAC1C,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,QAAQ;QACT,CAAC;QAED,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YACrB,GAAG,GAAG;gBACL,QAAQ,EAAG,IAAI,CAAC,CAAC,CAAC,CAAC,QAAQ;gBAC3B,SAAS,EAAG,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS;aAC7B,CAAA;QACF,CAAC;QAED,OAAO,GAAG,CAAC;IACZ,CAAC;CAED;AAxJD,gBAwJC;AAED,MAAM,CAAC,OAAO,GAAG,EAAE,EAAE,EAAE,CAAC"}

106
dist/post.js vendored Normal file
View File

@ -0,0 +1,106 @@
"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

1
dist/post.js.map vendored Normal file
View File

@ -0,0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ACP,KAAK,EAAE,uBAAuB;gBAC9B,MAAM,EAAE,CAAC;wBACP,GAAG,EAAE,KAAK,CAAC,WAAW;wBACtB,KAAK,EAAE,YAAY,CAAC,IAAI,CAAC,IAAI;qBAC7B,CAAC;aACF;SACD,CAAA;QAED,IAAI,CAAC;YACJ,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACpC,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,UAAU,KAAK,CAAC,WAAW,KAAK,KAAK,CAAC,IAAI,GAAG,CAAC,CAAC;YAC7D,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;QACpB,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,2BAA2B,EAAE,GAAG,CAAC,CAAC;YACjD,OAAO;QACR,CAAC;QAED,IAAI,CAAC;YACJ,MAAM,IAAI,CAAC,EAAE,CAAC,WAAW,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;QACrC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACd,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,gCAAgC,KAAK,CAAC,IAAI,aAAa,EAAE,GAAG,CAAC,CAAC;YAC7E,OAAO;QACR,CAAC;IAEF,CAAC;CACD;AAED,IAAI,IAAI,EAAE,CAAC"}

7
scripts/post.sh Normal file
View File

@ -0,0 +1,7 @@
#!/bin/bash
set -e
source .env
node dist/post

View File

@ -131,6 +131,30 @@ export class DB {
return rows;
}
public async getBsky () : Promise<Photo> {
const query : string = `SELECT * FROM photos WHERE bsky = 0 ORDER BY score ASC, created DESC LIMIT 1;`;
let rows : Photo[] = [];
try {
rows = await this.all(query);
} catch (err) {
this.log.error(`Error getting all photos`, err);
}
if (rows.length === 0) {
return null;
}
return rows[0];
}
public async confirmBsky (id : string) {
const query : string = `UPDATE photos SET bsky = 1 WHERE id = ?;`;
try {
await this.run(query, [ id ]);
} catch (err) {
throw err;
}
}
public async cacheLocation (location : string, latlng : LatLng) {
const query : string = `INSERT OR IGNORE INTO geocode (location, latitude, longitude) VALUES (?, ?, ?);`;
try {

119
src/post.ts Normal file
View File

@ -0,0 +1,119 @@
import 'dotenv/config';
import { createLog } from './log';
import type { Logger } from 'winston';
import { readFile } from 'fs/promises';
import { join } from 'path';
import { BskyAgent } from '@atproto/api';
import type CallOptions from '@atproto/api';
import { DB } from './db';
import type { Photo } from './db';
import { envString } from './env';
class Post {
private log : Logger = createLog('post');
private db : DB = new DB();
private bskyAgent : BskyAgent = new BskyAgent({ service: 'https://bsky.social' });
constructor () {
this.post();
}
private async post () {
await this.bsky();
}
private bskyDescription (photo : Photo) : string {
return `${photo.description}
${photo.format}${photo.filmstock !== 'Unknown' ? ' - ' + photo.filmstock : ''}`;
}
private async bsky () {
const photo : Photo = await this.db.getBsky();
const identifier : string = envString('BSKY_USER', null);
const password : string = envString('BSKY_PASSWORD', null);
let text : string;
let post : any;
let imagePath : string;
let imageData : Buffer;
let blobResponse : any;
let postData : any;
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 = join(envString('WWW', './www'), 'img', `${photo.id}_full.jpg`);
try {
imageData = await readFile(imagePath);
} catch (err) {
this.log.error(`Error reading image data ${imagePath}`, err);
}
try {
blobResponse = await this.bskyAgent.uploadBlob(imageData, { contentType: 'image/jpeg' } as any)
} 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();

View File

@ -10,6 +10,7 @@
"baseUrl" : "dist",
"outDir": "./dist/",
"rootDir" : "./src/",
"skipLibCheck": true,
"paths" : {
}
},