Created
August 21, 2021 18:32
-
-
Save martonlederer/2c781cbe2eb064702deadfe47a887e94 to your computer and use it in GitHub Desktop.
Simple Arweave Blog Tutorial
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Arweave = require("arweave"); | |
const fs = require("fs"); | |
const path = require("path") | |
const argql = require("ar-gql") | |
const fetch = require("node-fetch"); | |
const client = new Arweave({ | |
host: "arweave.net", | |
port: 443, | |
protocol: "https", | |
}); | |
// feltöltjük a blogot | |
async function upload(name, author, content) { | |
const key = JSON.parse(fs.readFileSync(path.join(__dirname, "./arweave.json"))); | |
const transaction = await client.createTransaction({ | |
data: content, | |
}, key); | |
transaction.addTag("Content-Type", "text/plain"); | |
transaction.addTag("App-Name", "Zsomblog"); | |
transaction.addTag("Blog-Name", name); | |
transaction.addTag("Blog-Author", author); | |
await client.transactions.sign(transaction, key); | |
let uploader = await client.transactions.getUploader(transaction); | |
while (!uploader.isComplete) { | |
await uploader.uploadChunk(); | |
console.log(`${uploader.pctComplete}% complete, ${uploader.uploadedChunks}/${uploader.totalChunks}`); | |
} | |
console.log(transaction.id) | |
} | |
//upload("Teszt blog", "Marton Lederer", new TextDecoder().decode(fs.readFileSync(path.join(__dirname, "./blogszoveg.txt")))) | |
async function getBlogs() { | |
const blogsQuery = await argql.run(` | |
query { | |
transactions (tags: { | |
name: "App-Name", | |
values: "Zsomblog" | |
}) { | |
edges { | |
node { | |
id, | |
tags { | |
name | |
value | |
} | |
} | |
} | |
} | |
} | |
`); | |
const blogs = blogsQuery.data.transactions.edges.map((blog) => ({ | |
id: blog.node.id, | |
name: blog.node.tags.find((tag) => tag.name === "Blog-Name").value, | |
author: blog.node.tags.find((tag) => tag.name === "Blog-Author").value, | |
})) | |
console.log(blogs) | |
} | |
// getBlogs() | |
async function getBlog(id) { | |
const blogData = await fetch(`https://arweave.net/${id}`); | |
const blogQuery = await argql.run(` | |
query { | |
transaction (id: "${id}") { | |
tags { | |
name | |
value | |
} | |
} | |
} | |
`); | |
console.log({ | |
name: blogQuery.data.transaction.tags.find((tag) => tag.name === "Blog-Name").value, | |
author: blogQuery.data.transaction.tags.find((tag) => tag.name === "Blog-Author").value, | |
content: await blogData.text() | |
}) | |
} | |
// getBlog("jgf3YoPdr15EZXHJcrWo93YCx0lt1doJ_nkz4_FnUkM") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment