MastodonBot/src/ImageUploader.ts
2023-01-18 23:07:41 +01:00

75 lines
No EOL
2.9 KiB
TypeScript

import {ChangeSetData} from "./OsmCha";
import MastodonPoster from "./Mastodon";
import OsmUserInfo from "./OsmUserInfo";
import ImgurAttribution from "./ImgurAttribution";
import Utils from "./Utils";
import Config from "./Config";
export default class ImageUploader {
private readonly _imageQueue: { image: string; changeset: ChangeSetData }[];
private _poster: MastodonPoster;
private _authors: string[] = []
private readonly _globalConfig: Config
constructor(imageQueue: {image: string, changeset: ChangeSetData}[], poster: MastodonPoster, config: Config) {
this._imageQueue = imageQueue;
this._poster = poster;
this._globalConfig = config
}
public getCurrentAuthors(){
return [...this._authors]
}
public async attemptToUpload(targetcount: number): Promise<string[]>{
const mediaIds = []
while(mediaIds.length < targetcount && this._imageQueue.length >0){
const first = this._imageQueue[0]
try {
const id = await this.uploadFirstImage()
mediaIds.push(id)
}catch (e) {
console.error("Could not upload image! ", first.image, e)
console.log("Trying again")
try {
const id = await this.uploadFirstImage()
mediaIds.push(id)
}catch (e) {
console.error("Retry could not upload image! ", first.image, e)
}
}
}
return mediaIds
}
private async uploadFirstImage(): Promise<string>{
const image = this._imageQueue.shift()
const cs = image.changeset.properties
let authorName = cs.user
try {
const authorInfo = new OsmUserInfo(Number(cs.uid), this._globalConfig)
authorName = (await authorInfo.GetMastodonUsername(this._poster)) ?? cs.user
} catch (e) {
console.log("Could not fetch more info about contributor", authorName, cs.uid, "due to", e)
}
if (this._globalConfig.mastodonAuth.dryrun) {
console.log("Not uploading/downloading image:" + image.image + " dryrun")
this._authors.push(authorName)
return "dummy_id"
}
console.log("Fetching attribution for", image.image)
const attribution = await ImgurAttribution.DownloadAttribution(image.image)
const id = image.image.substring(image.image.lastIndexOf("/") + 1)
const path = this._globalConfig.cacheDir + "/image_" + id
await Utils.DownloadBlob(image.image, path)
const mediaId = await this._poster.uploadImage(path, "Image taken by " + authorName + ", available under " + attribution.license + ". It is made with the thematic map " + image.changeset.properties.theme + " in changeset https://openstreetmap.org/changeset/" + image.changeset.id)
this._authors.push(authorName)
return mediaId
}
}