Skip to content

Instantly share code, notes, and snippets.

@dwsmart
dwsmart / same_day_previous_year.js
Created October 10, 2024 08:16
Get the same day of the week in a previous year
// get same day of the week in the last x years ago for specific date.
// Default is 1 year ago, and default date is today, takes account of leap year
function getPreviousYearSameDay(dateToStart = false, years = 1) {
const date = dateToStart ? new Date(dateToStart) : new Date();
const lastYear = new Date(date.getFullYear() - years, date.getMonth(), date.getDate());
const dayDiff = date.getDay() - lastYear.getDay();
return new Date(lastYear.setDate(date.getDate() + dayDiff));
}
console.log(getPreviousYearSameDay('2024-10-10', 1).toISOString()); // 2023-10-11T23:00:00.000Z
def Jaccard_Similarity(doc1, doc2):
# List the unique words in a document
words_doc1 = set(doc1.lower().split())
words_doc2 = set(doc2.lower().split())
# Find the intersection of words list of doc1 & doc2
intersection = words_doc1.intersection(words_doc2)
# Find the union of words list of doc1 & doc2
@dwsmart
dwsmart / app script
Created January 17, 2023 17:09
get file size from url
# google sheets app script to get file size in bytes from a url
function getSizes(theurl) {
const response = UrlFetchApp.fetch(theurl);
const blob = response.getBlob()
const size = blob.getBytes().length
return size
}
@dwsmart
dwsmart / mastodonsitemap.js
Last active November 15, 2022 11:04
Node.js script to create a sitemap from mastodon API
// config
const sitemapFile = '/var/www/html/mastodon_sitemap.xml'; // path / name of the created sitemap
const mastodonServer = 'https://yourmastodon.domain'; // your mastodon server, no trailing slash
const numberofPosts = 10000; // number of posts to include in the sitemap
const pingGoogle = false; // true or false
const pingURL = 'enter_full_final_accessible_url'; // URL to ping Google with, if you're putting on differet domain, see cross-site sitemaps https://developers.google.com/search/docs/crawling-indexing/sitemaps/large-sitemaps#manage-sitemaps-for-multiple-sites
// end config
const fs = require('fs');
@dwsmart
dwsmart / nginx_redirect_mastodon_well-known.config
Created November 9, 2022 13:06
nginx conf for redirecting .well-known for mastodon
# before server {} block
# if you're not mapping $request_path already, you'll need to this next block
map $request_uri $request_path {
~(?<captured_path>[^?]*) $captured_path;
}
map $arg_resource $valid_mastodon {
# If you want any account at your domain to resolve to just one mastodon account, i.e bob@tamethebots.com, mary@tamethebots.com
/*
* a cloudflare worker to remove the script tag they inject into the head if you have a cloudflare app installed.
* WARNING: some apps may not work if you remove this script tag!!! But things like logflare that add no js of
* their own should be fine.
*/
addEventListener('fetch', event => {
const request = event.request
event.respondWith(handleRequest(request))
});
@dwsmart
dwsmart / decode-encode.js
Last active October 27, 2021 13:33
decode-encode.js
function fixedEncodeURI(str) {
const prepURL = new URL(str);
const origin = prepURL.origin;
let raw = str;
let decoded = decodeURI(str);
while (raw !== decoded) {
decoded = decodeURI(decoded);
raw = decodeURI(raw);
}
justPath = decoded.replace(origin, '');
@dwsmart
dwsmart / logflare_to_common_log_format.js
Last active November 14, 2023 10:26
Logflare to Common Log Format nodejs Script
// require libs
// run npm install @google-cloud/bigquery
const { BigQuery } = require('@google-cloud/bigquery');
const fs = require('fs');
// BigQuery Config - see https://cloud.google.com/docs/authentication/production#create_service_account
const options = {
keyFilename: '{path_to_key_file}',
projectId: '{project_id}',
};
function normEncoding(string) {
try {
return urlEncodeCaseCorrect(encodeURI(path).replace(/%25/g, '%'));
} catch (e) {
return string;
}
}
function urlEncodeCaseCorrect(string) {
return path.replace(/%[0-9a-fA-F]{2}/g, function(match) {
// update existing robots tag if one is there
const oldRobots = document.querySelector("[name='robots']");
if (oldRobots) {
oldRobots.content = "noindex";
} else {
// add one if none existed
const newRobots = document.createElement('meta');
newRobots.name = "robots";
newRobots.content = "noindex";
document.getElementsByTagName('head')[0].prepend(newRobots);