Skip to content

Instantly share code, notes, and snippets.

@dmdboi
dmdboi / IG-Image-Downloader
Created April 18, 2020 23:52
This code snippet downloads a single image from an Instagram URL using Puppeteer and Cheerio
const download = require('image-downloader')
const puppeteer = require('puppeteer')
const $ = require('cheerio');
const saveImage = (urlVar, callback) => {
puppeteer
.launch()
.then(function(browser) {
return browser.newPage();
})
@dmdboi
dmdboi / mysql_backup.js
Created September 11, 2020 22:52
A node.js script to backup a MySQL database using MySQLDump. Can be added to a node-cron timer to automate backups.
var fs = require('fs');
var spawn = require('child_process').spawn;
var wstream = fs.createWriteStream('dumpfilename.sql'); //Name of SQL dump file
var mysqldump = spawn('mysqldump', [
'-u',
'DB_USER',
'-p DB_PASSWORD',
'DB_NAME'
]);
@dmdboi
dmdboi / discord_oauth.js
Created September 20, 2020 13:08
Two functions to authenticate a Discord User and get User's data
var axios = require('axios');
var qs = require('qs');
exports.getAccessToken = (code) => {
var data = qs.stringify({
'client_id': '',
'client_secret': '',
'grant_type': 'authorization_code',
'code': code,
'redirect_uri': '',
@dmdboi
dmdboi / getRepositories.js
Last active December 1, 2020 09:02
A Netlify function to return your public GitHub repositories.
const axios = require("axios");
exports.handler = async event => {
try {
let username = "" // Your Github Username
let repos = await axios({
method: "GET",
url: `https://api.github.com/users/${username}/repos`
}).then(res => {
@dmdboi
dmdboi / MongoDB-wrapper.js
Last active January 8, 2021 03:40
A simple MongoDB wrapper for an Express server.
const { mongo } = require("./mongo");
const db = mongo.db;
// collection - The name of the collection to perform operation on
// body - req.body or an object with data to insert/update in a table
// query - Filter to find document in the table i.e { username: "DMDBOI" }
const database = {
create: async function (collection, body) {
let document = await db
@dmdboi
dmdboi / cawfee.js
Created February 22, 2021 04:38
Use this script to show a drinks stats on your website from Cawfee
//If you're reading this, say hi on Twitter @dmdboi
async function getData(url) {
// Default options are marked with *
const response = await fetch(url, {
method: "GET", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, *cors, same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
headers: {
"Content-Type": "application/json"
},
@dmdboi
dmdboi / discord-webhook.js
Last active October 8, 2023 11:09
An example gist to send Embed messages via Discord Webhooks
const axios = require("axios")
//An array of Discord Embeds.
let embeds = [
{
title: "Discord Webhook Example",
color: 5174599,
footer: {
text: `📅 ${date}`,
},
@dmdboi
dmdboi / scraper.js
Last active April 1, 2021 21:58
Downloads the JSON data for a given number of XKCD comics
const axios = require('axios')
const fs = require('fs');
const start = async (number) => {
for (let num = 0; num < 10; num++) {
let posts = []
for (let index = 0; index < number; index++) {
let comic = (num * number) + index + 1
console.log(comic)
@dmdboi
dmdboi / notion-to-markdown.js
Last active October 11, 2022 14:46
Save Notion articles as Nuxt/Content-ready markdown files.
// This script reads all pages in a Notion DB and saves them as Markdown files ready for Nuxt/Content.
// You will need a Notion Integration key to make the Client() work and the ID of the database yo want to save.
// Run this from the root dir of your Nuxt project.
const fs = require('fs-extra');
const { Client } = require("@notionhq/client");
const { NotionToMarkdown } = require("notion-to-md");
const notionAuth = ""
@dmdboi
dmdboi / Logger.ts
Created September 15, 2023 09:31
Adonis Request Logger
import type { HttpContextContract } from "@ioc:Adonis/Core/HttpContext";
import { default as Log } from "@ioc:Adonis/Core/Logger";
export default class Logger {
public async handle(ctx: HttpContextContract, next: () => Promise<void>) {
Log.info(`[ ${ctx.request.method()} ] ${ctx.request.url()}`);
await next();
}
}