Skip to content

Instantly share code, notes, and snippets.

View magician11's full-sized avatar

A magician11

  • Golightly+
  • New Zealand
View GitHub Profile
@magician11
magician11 / get-youtube-subtitles.js
Created August 14, 2018 17:12
How to extract plain text subtitles from YouTube
const { getSubtitles } = require('youtube-captions-scraper');
const getYouTubeID = require('get-youtube-id');
const getYouTubeSubtitles = async youtubeUrl => {
try {
const videoID = getYouTubeID(youtubeUrl);
const subtitles = await getSubtitles({ videoID });
return subtitles.reduce(
(accumulator, currentSubtitle) =>
`${accumulator} ${currentSubtitle.text}`,
@magician11
magician11 / get-url-params.js
Created July 1, 2018 15:10
How to get all parameters from a URL as an associative array
export const getURLparams = () => {
const pl = /\+/g;
const search = /([^&=]+)=?([^&]*)/g;
const decode = s => decodeURIComponent(s.replace(pl, ' '));
const query = window.location.search.substring(1);
const urlParams = {};
let match;
while ((match = search.exec(query))) {
urlParams[decode(match[1])] = decode(match[2]);
@magician11
magician11 / freshbooks-webhooks.js
Last active June 14, 2018 18:05
Verify a FreshBooks webhook
const xml = require('xml');
module.exports = app => {
//processes the webhook for when a new invoice is created
app.post('/webhooks/new-invoice', async (req, res) => {
try {
// debugging
console.log(req.body);
const { name, verifier, object_id } = req.body;
@magician11
magician11 / install-certbot-ubuntu
Last active June 5, 2018 19:21
How to install certbot on Ubuntu for Nginx
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx
@magician11
magician11 / nodejs-cron.js
Last active May 1, 2018 17:50
How to get a Node.js function to run once on the last day of each month
const moment = require('moment');
const someAction = () => console.log('Actioning...');
const wait = ms => new Promise((resolve, reject) => setTimeout(resolve, ms));
const startCron = async () => {
while (true) {
if (
moment().date() ===
@magician11
magician11 / tweetnacl-test.js
Last active January 8, 2020 21:27
How to encrypt and decrypt a message with TweetNaCl.js
const tweetnacl = require('tweetnacl'); // https://github.com/dchest/tweetnacl-js
tweetnacl.util = require('tweetnacl-util'); // https://github.com/dchest/tweetnacl-util-js
// utility function to display the Uint8Array
const asciiArmored = arr => tweetnacl.util.encodeBase64(arr);
// generate the key to encrypt a message
const secretKey = tweetnacl.randomBytes(32);
console.log(`secret key: ${asciiArmored(secretKey)}`);
@magician11
magician11 / timezones.js
Created March 2, 2018 08:19
How to use Node.js to display the time now in a different timezone
const moment = require('moment-timezone'); // https://momentjs.com/timezone/
const timestamp = new Date();
console.log(
moment(timestamp)
.tz('America/Vancouver') // find your zone here https://momentjs.com/timezone/docs/#/data-loading/getting-zone-names/
.format('dddd, MMMM Do YYYY, h:mm:ss a (UTC Z)')
);
@magician11
magician11 / crontab
Last active April 30, 2018 20:25
How to automatically restart a Node.js script (with forever) on a server reboot
NODE_ENV=production
PATH=/root/.nvm/versions/node/v8.11.1/bin:$PATH
@reboot cd /var/server/script-directory && /root/.nvm/versions/node/v8.11.1/bin/forever start myscript.js
@magician11
magician11 / my-domain.com.conf
Last active January 19, 2018 19:42
How to add pre and post hooks for Certbot automatic renewals running Nginx
# renew_before_expiry = 30 days
version = 0.19.0
archive_dir = /etc/letsencrypt/archive/my-domain.com
cert = /etc/letsencrypt/live/my-domain.com/cert.pem
privkey = /etc/letsencrypt/live/my-domain.com/privkey.pem
chain = /etc/letsencrypt/live/my-domain.com/chain.pem
fullchain = /etc/letsencrypt/live/my-domain.com/fullchain.pem
# Options used in the renewal process
[renewalparams]
@magician11
magician11 / default
Last active January 19, 2018 20:10
Reverse proxy with Nginx to Node.js server with HTTP to HTTPS redirect
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name my-server.com www.my-server.com;
return 301 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl http2 default_server;