Skip to content

Instantly share code, notes, and snippets.

View douglasmiranda's full-sized avatar
👽

Douglas Miranda douglasmiranda

👽
  • Earth, Brazil
View GitHub Profile
@douglasmiranda
douglasmiranda / fields.py
Created July 9, 2024 01:53
Django (postgres) ArrayField Multiple Choices as checkboxes.
# https://gist.github.com/danni/f55c4ce19598b2b345ef
from django import forms
from django.contrib.postgres.fields import ArrayField
class MultipleChoiceArrayField(ArrayField):
def formfield(self, **kwargs):
defaults = {
"form_class": forms.MultipleChoiceField,
"choices": self.base_field.choices,
@douglasmiranda
douglasmiranda / Dockerfile
Created July 6, 2024 23:02
Dockerfile Alpine Python Pillow
FROM python:3.12-alpine
# Python specific settings
# sends output directly to terminal
# https://docs.python.org/3/using/cmdline.html#cmdoption-u
ENV PYTHONUNBUFFERED=1
# Python packages build dependencies
RUN apk add --no-cache --virtual .build-dependencies \
# Essentials
@douglasmiranda
douglasmiranda / browserless.py
Created June 10, 2024 21:43
Testing browserless deployed on Google Cloud Run + Google API Gateway
import requests
# API GATEWAY URL
browserless_url = "https://..../config"
url = "https://example.com/"
# token: BROWSERLESS TOKEN
querystring = {"token":"XYZ", "timeout":"1200000"}
response = requests.get(
@douglasmiranda
douglasmiranda / browserless.md
Last active June 4, 2024 00:44
Browserless / Puppeteer (Protocol error (Runtime.callFunctionOn): Target closed) Timeout Issue

Error: Protocol error (Runtime.callFunctionOn): Target closed

Browserless v2.6.1 running on Docker

I don't know why this happens, but it doesn't matter if you set your TIMEOUT environment variable, like so:

services:
 browserless:
@douglasmiranda
douglasmiranda / validate.py
Created April 23, 2024 23:31
Mailgun Webhook: validate webhook request in Python
# https://documentation.mailgun.com/docs/mailgun/user-manual/tracking-messages/#webhooks
# Securing Webhooks
# To ensure the authenticity of event requests, Mailgun signs them and posts the signature alongside the webhook's event-data.
# A signature takes the following form:
# Parameter Type Description
# timestamp int Number of seconds passed since January 1, 1970.
# token string Randomly generated string with length of 50.
# signature string String with hexadecimal digits generated by an HMAC algorithm
@douglasmiranda
douglasmiranda / example.html
Created April 21, 2024 20:39
[HTML / Javascript form] multiple submit buttons/actions | capturing submit action server side | alert before submit javascript may affect submit button on server side
Lets say you have a form, with multiple submit buttons:
<form method="POST">
<button type="submit" name="save">
save
</button>
<button type="submit" name="send-all">
send all
</button>
</form>
@douglasmiranda
douglasmiranda / a.py
Created April 16, 2024 23:35
Python 2 urlencode unicode error (UnicodeEncodeError: 'ascii' codec can't encode character...)
import urllib
# 'a=%3F'
urllib.urlencode({'a': 'Á'}, 'utf-8')
@douglasmiranda
douglasmiranda / dramatiq.md
Created April 12, 2024 23:35
Dramatiq Task Queue - Check task status OR retrieve task result/status

It's much better simply implementing the callbacks on_success / on_failure.

import dramatiq


@dramatiq.actor
def identity(x):
    return x
@douglasmiranda
douglasmiranda / entrypoint.sh
Created April 10, 2024 01:58
Docker entrypoint template
#!/bin/sh
set -o errexit
set -o pipefail
# exec command: [..] from docker-compose.yml / stack.yml
exec "$@"
# Wait for any process to exit
wait -n