@publidata/utils-socials
Version:
Collection of methods to handle social networks
125 lines (114 loc) • 3.53 kB
JavaScript
const axios = require("axios");
const IFRAMELY_API_ENDPOINT = "https://iframe.ly/api/oembed";
const registry = {};
/* Get twitter post from a specific account
* @param config { consumerKey, consumerSecret } both string
* @param id, account id you would like post from
* @param count, how much of them would you like
*/
const twitter = ({ consumerKey, consumerSecret }, account, count = 5) =>
new Promise((resolve, reject) => {
if (!consumerKey || !consumerSecret || !account) {
reject(new Error("Missing parameters"));
} else {
const accessToken = `${consumerKey}|${consumerSecret}`;
axios
.get(
`https://api.publidata.io/v1/socials/twitter/${account}?token=${accessToken}&size=${count}`
)
.then(({ data }) => {
resolve(
data.map(post => ({
...post,
type: "twitter",
model: "socials",
side: "content",
release_raw_date: post.created_at
}))
);
})
.catch(reject);
}
});
registry.twitter = twitter;
/* Get Facebook post from a specific account
* @param config { consumerKey, consumerSecret } both string
* @param account, account name you would like post from
* @param count, how much of them would you like
*/
const facebook = ({ consumerKey, consumerSecret }, account, count = 5) =>
new Promise((resolve, reject) => {
if (!consumerKey || !consumerSecret || !account) {
reject(new Error("Missing parameters"));
} else {
const accessToken = `${consumerKey}|${consumerSecret}`;
axios
.get(
`https://api.publidata.io/v1/socials/facebook/${account}?token=${accessToken}&size=${count}`
)
.then(({ data }) => {
resolve(
data.map(post => ({
...post,
type: "facebook",
model: "socials",
side: "content",
release_raw_date: post.created_time
}))
);
})
.catch(reject);
}
});
registry.facebook = facebook;
/* Get instagram post media from a specific account
* @param accessToken, from your account, (get yourself one with http://instagram.pixelunion.net)
* @param count, how much of them would you like
*/
const instagram = (accessToken, count = 5) =>
new Promise((resolve, reject) => {
if (!accessToken) {
reject(new Error("Missing parameters"));
} else {
axios
.get(
`https://api.publidata.io/v1/socials/instagram?token=${accessToken}&size=${count}`
)
.then(({ data }) => {
resolve(
data.map(insta => ({
...insta,
type: "instagram",
model: "socials",
side: "content",
release_raw_date: new Date(
Number(`${insta.created_time}000`)
).toString()
}))
);
})
.catch(reject);
}
});
registry.instagram = instagram;
/**
* Get iframely info
* @param {string} url - The url to get info from
* @returns {Promise}
*/
const getIframelyMetas = url =>
new Promise((resolve, reject) => {
axios
.get(IFRAMELY_API_ENDPOINT, {
params: {
url,
api_key: process.env.IFRAMELY_API_KEY
}
})
.then(result => {
const { data } = result;
resolve(data);
}).catch(reject);
});
registry.getIframelyMetas = getIframelyMetas;
module.exports = registry;