Last active
February 24, 2021 21:56
-
-
Save alex-taxiera/56499b0ab3e173d2e347cd84e69ee408 to your computer and use it in GitHub Desktop.
Create image posts with Tumblr API v2 (NPF)
This file contains hidden or 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
import { readFile } from 'fs/promises' | |
import crypto from 'crypto' | |
import OAuth from 'oauth-1.0a' | |
import fetch from 'node-fetch' | |
import { config } from 'dotenv' | |
config() | |
const CAPTION_SEPARATOR = ' - ' | |
const POST_STATE = 'draft' // creating post as a draft | |
const sleep = (time = 2000) => new Promise((resolve) => setTimeout(resolve, time)) | |
const token = { | |
key: process.env.TOKEN, | |
secret: process.env.TOKEN_SECRET, | |
} | |
const oauth = new OAuth({ | |
consumer: { | |
key: process.env.CONSUMER_KEY, | |
secret: process.env.CONSUMER_SECRET, | |
}, | |
signature_method: 'HMAC-SHA1', | |
hash_function: (baseString, key) => crypto | |
.createHmac('sha1', key) | |
.update(baseString) | |
.digest('base64'), | |
}) | |
async function main () { | |
// reading data from a file | |
console.log('reading file...') | |
/** | |
* @type {Array<PostData>} | |
*/ | |
const data = await readFile('files/tumblr-data.json').then((res) => JSON.parse(res.toString())) | |
for (let i = 0; i < data.length; i++) { | |
const item = data[i] | |
const postText = `${item.title}${CAPTION_SEPARATOR}${item.artist.name}` | |
console.log(`${POST_STATE}ing`, i + 1, postText) | |
const body = { | |
state: POST_STATE, | |
content: [ | |
...item.images.map((url) => ({ | |
type: 'image', | |
media: [{ url }] | |
})), | |
{ | |
type: 'text', | |
text: postText, | |
formatting: [ | |
{ | |
start: 0, | |
end: item.title.length, | |
type: 'link', | |
url: item.link, | |
}, | |
{ | |
start: item.title.length + CAPTION_SEPARATOR.length, | |
end: postText.length, | |
type: 'link', | |
url: item.artist.link, | |
} | |
] | |
} | |
] | |
} | |
const requestData = { | |
url: `https://api.tumblr.com/v2/blog/${process.env.BLOG_NAME}/posts`, | |
method: 'POST', | |
} | |
const { Authorization } = oauth.toHeader(oauth.authorize(requestData, token)) | |
await fetch(requestData.url, { | |
method: requestData.method, | |
body: JSON.stringify(body), | |
headers: { | |
Authorization, | |
'Content-Type': 'application/json' | |
} | |
}) | |
if (i < data.length - 1) { | |
console.log('sleeping between requests...') | |
await sleep() | |
} | |
} | |
console.log('done!') | |
} | |
main() | |
/** | |
* @typedef PostData | |
* @prop {string} title Title of work. | |
* @prop {string} link Permalink to original source. | |
* @prop {Array<string>} images Images to add to post. | |
* @prop {object} artist Artist credited for images. | |
* @prop {string} artist.name Name of artist. | |
* @prop {string} artist.link Permalink to artist page. | |
*/ |
This file contains hidden or 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
{ | |
"name": "tumblr-post-images", | |
"type": "module", | |
"dependencies": { | |
"dotenv": "^8.2.0", | |
"node-fetch": "^2.6.1", | |
"oauth-1.0a": "^2.2.6" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Posting to tumblr with limited dependencies. Refer to the Tumblr API documentation for retrieving your secrets and tokens.
Place your tokens with the proper keynames in a file called
.env
.