Skip to content

Commit

Permalink
added some async to remove callback nesting hell
Browse files Browse the repository at this point in the history
  • Loading branch information
edsu committed Sep 18, 2020
1 parent 9f10f4d commit 39470b0
Showing 1 changed file with 47 additions and 54 deletions.
101 changes: 47 additions & 54 deletions anon.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ async function takeScreenshot(url) {

const filename = Date.now() + '.png'
const selector = 'table.diff.diff-contentalign-left'
const padding = 0

const rect = await page.evaluate(selector => {
const element = document.querySelector(selector)
Expand All @@ -133,77 +132,74 @@ async function takeScreenshot(url) {
await page.screenshot({
path: filename,
clip: {
x: rect.left - padding,
y: rect.top - padding,
width: rect.width + padding * 2,
height: rect.height + padding * 2
x: rect.left,
y: rect.top,
width: rect.width,
height: rect.height
}
})

await browser.close()
return filename
}

function sendStatus(account, status, edit) {
async function sendStatus(account, status, edit) {
console.log(status)

if (!argv.noop && (!account.throttle || !isRepeat(edit))) {

takeScreenshot(edit.url).then(function(screenshot) {
const screenshot = await takeScreenshot(edit.url)

// Mastodon
if (account.mastodon) {
const mastodon = new Mastodon(account.mastodon)
mastodon.post('media', {file: fs.createReadStream(screenshot)})
.then(function(response) {
if (!response.data.id) {
console.log('error uploading screenshot to mastodon')
return
}
mastodon.post('statuses', { 'status': status, media_ids: [response.data.id] }, function(err) {
if (err) {
console.log(err)
}
})
})
// Mastodon
if (account.mastodon) {
const mastodon = new Mastodon(account.mastodon)
const response = await mastodon.post('media', {file: fs.createReadStream(screenshot)})
if (!response.data.id) {
console.log('error uploading screenshot to mastodon')
return
}

// Twitter
if (account.access_token) {
const twitter = new Twit(account)
const b64content = fs.readFileSync(screenshot, {encoding: 'base64'})
await mastodon.post(
'statuses',
{'status': status, media_ids: [response.data.id]},
err => console.log(`mastodon post failed: ${err}`)
)
}

// upload the screenshot to twitter
twitter.post('media/upload', {media_data: b64content}, function(err, data, response) {
if (err) {
console.log(err)
return
}
// Twitter
if (account.access_token) {
const twitter = new Twit(account)
const b64content = fs.readFileSync(screenshot, {encoding: 'base64'})

// add alt text for the media, for use by screen readers
const mediaIdStr = data.media_id_string
const altText = "Screenshot of edit to " + edit.page
const metaParams = {media_id: mediaIdStr, alt_text: {text: altText}}
// upload the screenshot to twitter
twitter.post('media/upload', {media_data: b64content}, function(err, data, response) {
if (err) {
console.log(err)
return
}

twitter.post('media/metadata/create', metaParams, function(err, data, response) {
// add alt text for the media, for use by screen readers
const mediaIdStr = data.media_id_string
const altText = "Screenshot of edit to " + edit.page
const metaParams = {media_id: mediaIdStr, alt_text: {text: altText}}

twitter.post('media/metadata/create', metaParams, function(err, data, response) {
if (err) {
console.log('metadata upload for twitter screenshot alt text failed with error', err)
}
const params = {
'status': status,
'media_ids': [mediaIdStr]
}
twitter.post('statuses/update', params, function(err) {
if (err) {
console.log('metadata upload for twitter screenshot alt text failed with error', err)
}
const params = {
'status': status,
'media_ids': [mediaIdStr]
console.log(err)
}
twitter.post('statuses/update', params, function(err) {
if (err) {
console.log(err)
}
})
fs.unlinkSync(screenshot)
})
fs.unlinkSync(screenshot)
})
}

})
})
}
}
}

Expand Down Expand Up @@ -274,9 +270,6 @@ function main() {
})
}

async function getClip(page, selector, path, padding=16) {
}

if (require.main === module) {
main()
}
Expand Down

0 comments on commit 39470b0

Please sign in to comment.