replace imgs with nostr.build urls
This commit is contained in:
@@ -4,6 +4,7 @@ import { extract } from '@extractus/feed-extractor'
|
||||
import NDK, { NDKEvent, NDKPrivateKeySigner } from '@nostr-dev-kit/ndk'
|
||||
import bech32 from 'bech32-buffer'
|
||||
import moment from 'moment'
|
||||
import FormData from 'form-data'
|
||||
|
||||
const toHexString = nsec => {
|
||||
let buffer = bech32.decode(nsec).data
|
||||
@@ -14,14 +15,72 @@ const toHexString = nsec => {
|
||||
}, '');
|
||||
}
|
||||
|
||||
const htmlToAscii = html => {
|
||||
let ascii = html
|
||||
ascii = ascii.replaceAll("<p>", "")
|
||||
ascii = ascii.replaceAll("</p>", "")
|
||||
ascii = ascii.replaceAll("<br>", "")
|
||||
ascii = ascii.replaceAll("<img src=\"", "")
|
||||
ascii = ascii.replaceAll("\" style=\"max-width:250px;\" />", "")
|
||||
return ascii
|
||||
const imgUrls = html => {
|
||||
const re = /<img[^>]+src="?([^"\s]+)"?[^>]*\/>/g
|
||||
const urls = []
|
||||
let m
|
||||
|
||||
while (m = re.exec(html)) {
|
||||
urls.push(m[1])
|
||||
}
|
||||
|
||||
return urls
|
||||
}
|
||||
|
||||
const putImgPlaceholders = html => {
|
||||
const re = /<img[^>]+src="?([^"\s]+)"?[^>]*\/>/g
|
||||
const placeholders = []
|
||||
let replaced = html
|
||||
const matches = html.matchAll(re)
|
||||
let img = 0
|
||||
|
||||
for(let match of matches) {
|
||||
replaced = replaced.replace(match[0], `%%${img}%%`)
|
||||
placeholders.push(`%%${img}%%`)
|
||||
img++
|
||||
}
|
||||
|
||||
return {
|
||||
replaced,
|
||||
placeholders
|
||||
}
|
||||
}
|
||||
|
||||
const stripHtml = html => {
|
||||
return html.replaceAll(/<[^>]*>/g, "")
|
||||
}
|
||||
|
||||
const replaceImgsWithNostrBuildUrls = async (storeUrl, html, npub) => {
|
||||
const urls = imgUrls(html)
|
||||
let { replaced, placeholders } = putImgPlaceholders(html)
|
||||
|
||||
let count = 0
|
||||
for (let placeholder of placeholders) {
|
||||
const nbUrl = await uploadImgStore(storeUrl, urls[count], npub)
|
||||
if (nbUrl) {
|
||||
replaced = replaced.replace(placeholder, nbUrl)
|
||||
} else {
|
||||
replaced = replaced.replace(placeholder, '')
|
||||
}
|
||||
count++
|
||||
}
|
||||
return replaced
|
||||
}
|
||||
|
||||
const uploadImgStore = async (hostUrl, imgUrl, npub) => {
|
||||
const response = await fetch(hostUrl, {
|
||||
method: 'POST',
|
||||
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
||||
body: new URLSearchParams({
|
||||
"url": imgUrl,
|
||||
"npub": npub
|
||||
}) })
|
||||
const json = await response.json()
|
||||
|
||||
if (json.status !== "success") {
|
||||
return false
|
||||
}
|
||||
return json.data[0].url
|
||||
}
|
||||
|
||||
export const handler = async (event) => {
|
||||
@@ -83,7 +142,13 @@ export const handler = async (event) => {
|
||||
explicitRelayUrls: [
|
||||
"wss://nostr.mom",
|
||||
"wss://nostr.snort.social",
|
||||
"wss://nostr.lol"
|
||||
"wss://nostr.lol",
|
||||
"wss://nostr.oxtr.dev",
|
||||
"wss://relay.nostr.bg",
|
||||
"wss://nostr-pub.wellorder.net",
|
||||
"wss://relay.damus.io",
|
||||
"wss://nostr.milou.lol",
|
||||
"wss://nostr.bitcoiner.social"
|
||||
]
|
||||
})
|
||||
|
||||
@@ -101,18 +166,29 @@ export const handler = async (event) => {
|
||||
return response
|
||||
})
|
||||
|
||||
let filtered = rss.item.filter(item => moment(item.published).isAfter(since))
|
||||
let filtered = rss.item.filter(item => moment(item.pubDate).isAfter(since))
|
||||
response.totalItems = filtered.length
|
||||
|
||||
for (let item of filtered) {
|
||||
const user = await ndk.signer.user()
|
||||
let content = ''
|
||||
let replaced = await replaceImgsWithNostrBuildUrls(
|
||||
'https://nostr.build/api/v2/upload/url',
|
||||
item.description,
|
||||
user.npub)
|
||||
if (replaced) {
|
||||
content = stripHtml(replaced)
|
||||
}
|
||||
|
||||
let ndkEvent = new NDKEvent(ndk, {
|
||||
kind: 1,
|
||||
pubkey: ndk.signer.user().hexpubkey,
|
||||
content: htmlToAscii(item.description),
|
||||
pubkey: user.hexpubkey,
|
||||
content,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
})
|
||||
|
||||
if (dryrun) {
|
||||
console.dir(content)
|
||||
response.successes++
|
||||
continue
|
||||
}
|
||||
@@ -122,7 +198,6 @@ export const handler = async (event) => {
|
||||
const published = await ndkEvent.publish()
|
||||
if (published) {
|
||||
response.successes++
|
||||
|
||||
const ssmPutCommand = new PutParameterCommand({
|
||||
Name: lastRunTimeParam,
|
||||
Value: new Date().toISOString(),
|
||||
@@ -130,12 +205,10 @@ export const handler = async (event) => {
|
||||
Overwrite: true
|
||||
})
|
||||
await ssmClient.send(ssmPutCommand)
|
||||
.catch(err => {
|
||||
console.dir(err)
|
||||
response.errors.push(err)
|
||||
return response
|
||||
})
|
||||
continue
|
||||
}
|
||||
console.dir(`failed to publish note`)
|
||||
response.errors.push(`failed to publish note`)
|
||||
} catch(e) {
|
||||
console.dir(e)
|
||||
response.errors.push(e)
|
||||
|
||||
Reference in New Issue
Block a user