Skip to content

Instantly share code, notes, and snippets.

View mariocesar's full-sized avatar

Mario-César mariocesar

View GitHub Profile
@mariocesar
mariocesar / get_s3_regions.py
Last active February 24, 2022 15:28
Get all available regions supported by boto library for an AWS service, like S3
from botocore.loaders import create_loader
from botocore.regions import EndpointResolver
endpoints = create_loader().load_data('endpoints')
partitions = [part['partition'] for part in endpoints['partitions']]
for partition in partitions:
s3_regions = EndpointResolver(endpoints).get_available_endpoints("s3", partition)
aws_partition = next(
@mariocesar
mariocesar / notion.md
Last active February 21, 2022 17:56
Notion JS API Use examples

Some common variables for all functions

const NOTION_API_KEY = process.env.NOTION_API_KEY;

const headers = {
  Accept: "application/json",
  "Notion-Version": "2021-08-16",
  "Content-Type": "application/json",
@mariocesar
mariocesar / checkiday.sh
Last active February 23, 2022 22:16
What day is it? Get the rss of checkiday.com and output what is celebrated today.
curl -s https://www.checkiday.com/rss.php \
| grep -o -E "<title><\!\[CDATA\[[^<]+</title>" \
| sed -E "s/.+CDATA\[(.+)\]\].+/\\1/" \
| head -n1
# Curling is Cool Day
@mariocesar
mariocesar / utils.py
Created June 29, 2021 22:28
Sanitize html defining allowed tags and attributes. Using BeautifulSoup
from bs4 import BeautifulSoup, Comment
from typing import Dict, List, Optional, Tuple
AllowedTagsType = Dict[str, Optional[List[str]]]
# Example:
default_allow_tags = {
"p": None,
"b": None, # Allow no property
@mariocesar
mariocesar / README.md
Created May 6, 2021 15:59
Javascript "Vanilla" helpers for different useful cases.

Add a target=_blank and rel=nooopener for all links to outside your page. Useful for security and accesibility

<script>
  Array.prototype.slice.call(document.querySelectorAll("a[href^=http]")).map((el)=> {
    el.setAttribute("target", "_blank");
    el.setAttribute("title", el.href);
    el.setAttribute("rel", "noopener");
  });
@mariocesar
mariocesar / README.md
Last active April 30, 2021 02:46
Useful shell function to load .env files in the terminal session

Load environment variables using .env files

You can add the following functions for your shell, loadenv will search for .env file if found will export all to the shell session with unloadenv will unset the variables so you can start again, reloadenv will reread the .env file.

function loadenv {
    test -f .env || echo "No .env file in the working directory"
    oldenv=$(env|sort)
    export $(grep -v '^#' .env | xargs -d '\n')
 diff &lt;(echo "$oldenv") &lt;(env | sort)
@mariocesar
mariocesar / README.md
Created March 30, 2021 15:58
Annoying things to remember about Node.js, npm, yarn, etc and the whole ecosystem

Most packages still fail due peer dependency version resolve. Packages like expo-cli. To be able to install it make sure you have the legacy mode for resolving versions is enabled

npm config --global set legacy-peer-deps true
@mariocesar
mariocesar / README.md
Last active February 16, 2021 17:02
Psycopg2 utils / PostgreSQL with Python

Create a connection using a database url from Heroku or Digital Ocean App platform

from urllib.parse import urlparse

import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT

def create_connection():
 url = urlparse(os.environ["DATABASE_URL"])
@mariocesar
mariocesar / password_hasher.py
Created December 14, 2020 22:45
Some notes for twitch.tv/melkeydev to make a simple/secure password hasher
import base64
import hashlib
import secrets
from typing import NoReturn
def pbkdf2(password: str, salt: str, iterations: int, dklen: int=0, digest=None):
"""Return the hash of password using pbkdf2."""
if digest is None:
digest = hashlib.sha256
@mariocesar
mariocesar / Makefile
Last active November 25, 2020 21:41
Build all icons and favicon needed for a webpage, using a svg input file
INKSCAPE_BIN ?= flatpak run org.inkscape.Inkscape
iconsizes = 16 32 48 57 60 72 76 96 120 144 152 180 192 256 300
iconnames = $(addsuffix .png, $(addprefix icons/icon-, $(iconsizes)))
all: icons icons/favicon.ico $(iconnames)
@echo = Done =
icons:
mkdir -p icons