Skip to content

Instantly share code, notes, and snippets.

Avatar
⌨️
Coding

Bibhuti Poudyal bibhuticoder

⌨️
Coding
View GitHub Profile
@bibhuticoder
bibhuticoder / wikipedia.js
Created Aug 18, 2021
Eye friendly wikipedia
View wikipedia.js
// load font
var wfScript = document.createElement('script');
wfScript.src = 'https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js';
document.head.appendChild(wfScript);
// apply font
setTimeout(() => {
WebFont.load({
google: {
families: ['Roboto']
View deployNodeApp.yml
name: deploy-to-dev-droplet
on:
push:
branches:
- dev
jobs:
deploy-backend:
runs-on: ubuntu-latest
steps:
- name: Deploy backend
View deployWebApp.yml
# name of github action workflow
name: deploy-to-dev-droplet
on:
push:
branches:
- dev # this script will run when pushed to "dev" branch
jobs:
deploy-frontend:
runs-on: ubuntu-latest #using latest ubuntu to build application
steps:
View cron2.js
import cron from 'node-cron';
import instaCacheCron from "./crons/instaCache.cron";
// run immediately after server starts
instaCacheCron();
// update instaPhotos Cache every 3 hours
cron.schedule('0 0 */3 * * *', async () => {
// this method fetches updated Insta images and saves to DB.
await instaCacheCron();
View fetchMedia.js
try {
let instaAccessToken = "XXXXXX"; // get from DB
let resp = await axios.get(`https://graph.instagram.com/me/media?fields=media_type,permalink,media_url&access_token=${instaAccessToken}`);
resp = resp.data;
let instaPhotos = resp.data.filter(d => d.media_type === "IMAGE").map(d => d.media_url);
// Got insta photos
} catch (e) {
console.log(e.response.data.error);
}
View instaRefresh.cron.js
try {
let oldAccessToken = "XXXXX"; // get from DB
let resp = await axios.get(`https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token=${oldAccessToken}`)
if (resp.data.access_token) {
let newAccessToken = resp.data.access_token;
// save newAccessToken to DB
}
} catch (e) {
console.log("Error=====", e.response.data);
}
View cron1.js
import cron from 'node-cron';
import instaRefreshCron from "./crons/instaRefresh.cron";
// run immediately after server starts
instaRefreshCron();
// refresh instaAccessToken eg: weekly(every Sat)
cron.schedule('* * * * * 7', async () => {
await instaRefreshCron();
});
View exchangeToken.js
try {
let resp = await axios.get(`https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret=${process.env.INSTA_APP_SECRET}&access_token=${accessToken}`)
accessToken = resp.data.access_token;
// save accessToken to Database
} catch (e) {
console.log("Error=====", e.data);
}
View processPost.js
// data from frontend
let code = req.body.code;
let redirectUri = req.body.redirectUri;
let accessToken = null;
try {
// send form based request to Instagram API
let result = await request.post({
url: 'https://api.instagram.com/oauth/access_token',
View postToServer.js
axios.post("https://your-server-url/init-insta", {
code: 'code-query-param-from-url',
redirectUrl: 'redirect-url' // needs to be registered at fb developer console
})
.then(({ data }) => {
// handle success case
})