Skip to content

Instantly share code, notes, and snippets.

@loiane
Forked from ErickWendel/gde-post-contribution.js
Created September 16, 2021 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loiane/83aff47c27a4ed04dcb7768264d728b3 to your computer and use it in GitHub Desktop.
Save loiane/83aff47c27a4ed04dcb7768264d728b3 to your computer and use it in GitHub Desktop.
Example of how to automate contribution submissions on GDE API
const axios = require('axios')
class GDEAPI {
constructor({ token }) {
this.token = token
}
async submitContributions(body) {
const headers = {
"accept": "application/json, text/plain, */*",
"accept-language": "en-US,en;q=0.9",
"authorization": `Bearer ${this.token}`,
"cache-control": "no-cache",
"content-type": "application/json",
"pragma": "no-cache",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"sec-gpc": "1"
}
return axios.post("https://api-gde.advocu.com/activities", body, {
headers,
});
}
baseObj(contribution, type) {
return {
category: 'Web Technologies',
type,
city: null,
country: null,
date: new Date(contribution.date).getTime(),
title: contribution.title,
description: contribution.description,
reach: contribution.reach,
link: contribution.link,
tags: contribution.tags,
publishable: true
}
}
prepareData(contribution) {
const type = {
'article': (contribution) => {
return {
...this.baseObj(contribution, 'written'),
subtype: 'article',
}
},
'video': (contribution) => {
return {
...this.baseObj(contribution, 'video'),
subtype: 'video',
}
},
'talk': (contribution) => {
return {
...this.baseObj(contribution, 'talk'),
subtype: null,
onlineActivity: true
}
},
}
return type[contribution.type.toLowerCase()](contribution)
}
}
// Your should get your token from Google Page (inpect element on google requests, on request header get Bearer token
// Your token will be a JWT token
// such as
const token = ''
const gdeApi = new GDEAPI({ token })
// post an article
const contribution = {
"category": "Web Technologies",
"type": "article",
"date": "2021-02-13",
"title": "Your amazing title",
"description": "Your amazing description",
"reach": "10000",
"link": "your.amazing.link.com",
"tags": ["Web Technologies"],
}
const body = gdeApi.prepareData(contribution)
const result = await gdeApi.submitContributions(body)
console.log(result)
/*
{ key: '09213avb' }
*/
// other contribution types
/*
const video = {
"category": "Web Technologies",
"type": "video",
"date": "2021-02-13",
"title": "Your amazing title",
"description": "Your amazing description",
"reach": "10000",
"link": "your.amazing.link.com",
"tags": ["Web Technologies"],
}
const talk = {
"category": "Web Technologies",
"type": "talk",
"date": "2021-02-13",
"title": "Your amazing title",
"description": "Your amazing description",
"reach": "10000",
"link": "your.amazing.link.com",
"tags": ["Web Technologies"],
}
const article = {
"category": "Web Technologies",
"type": "article",
"date": "2021-02-13",
"title": "Your amazing title",
"description": "Your amazing description",
"reach": "10000",
"link": "your.amazing.link.com",
"tags": ["Web Technologies"],
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment