Skip to content

Instantly share code, notes, and snippets.

@jcarlosroldan
jcarlosroldan / image-mapper.py
Created November 15, 2023 13:54
The script creates an HTML map showcasing images from a chosen directory.
from folium import Map, Marker, Popup
from os import listdir
from os.path import isfile
from piexif import load
from PIL import Image
from PIL.ExifTags import GPSTAGS, TAGS
from pillow_heif import read_heif
EXTENSIONS = {'jpg', 'jpeg', 'jfif', 'gif', 'png', 'webp', 'heic'}
PATH = 'your/path/here'
@jcarlosroldan
jcarlosroldan / reveal.css
Created April 13, 2023 01:57
Accurate Polaroid reveal effect
/* See in action at https://codepen.io/juancroldan/pen/vYVLYBx */
.polaroid {
background: #eeefea;
box-shadow: 0 0 .5px 1px #5415;
border-radius: 3px;
}
.polaroid img {
width: 230px;
height: 305px;
@jcarlosroldan
jcarlosroldan / filescan.cpp
Created April 13, 2023 01:54
Iteratively explore every file in a system with multiple workers, build a tree and save it as a text file
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <filesystem>
#include <thread>
#include <mutex>
#include <queue>
#include <condition_variable>
@jcarlosroldan
jcarlosroldan / server.py
Created June 21, 2022 09:18
Minimal Flask server for a JSON API
from flask import Flask
from flask.helpers import request
from traceback import format_exc
SERVER = Flask(__name__)
PORT = 5000
# SSL_CONTEXT = '/etc/ssl/certificate.crt', '/etc/ssl/private.key'
SSL_CONTEXT = None
def api(data: dict):
@jcarlosroldan
jcarlosroldan / tabber.py
Created October 27, 2021 19:41
Convert the files in a directory from space-indented to tab indented detecting the indentation level automatically
''' TABBER: Because spaces suck
This script will recursively convert all files in its directory from space indented to tab indented.
It will detect the number of spaces used for indentation and transform them into tabbed files.
Usage:
1. Copy the directory you want to convert and put this file inside. If you're brave enough, skip the
copying part.
2. Review the EXTENSIONS and IGNORE_FILES values.
3. Run it.
@jcarlosroldan
jcarlosroldan / weekday.py
Last active June 15, 2021 10:42
Weekday computation
WEEK_DAYS = 'mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'
def weekday(day: int, month: int, year: int) -> str: # You'd rather operate with datetimes, but this is funnier.
''' Computes the day of the week using Zeller's Congruence. '''
year_shift = year + year // 4 - year // 100 + year // 400
return WEEK_DAYS[(5 + day + (13 * (month + 1)) // 5 + year_shift) % 7]
@jcarlosroldan
jcarlosroldan / directory-listing.py
Created July 26, 2019 13:19
Minimal directory listing
from flask import Flask, send_file, request
from hashlib import sha256
from os import listdir
from os.path import join, abspath, dirname, isdir
from sys import stderr
app = Flask(__name__)
template = '<!doctype html><head><meta charset="utf-8"><title>%s</title></head><body><h1>%s</h1><ul>%s</ul></body></html>'
base = abspath('.')
salt = 'changeme' # generated with https://www.random.org/passwords/?num=1&len=24&format=html&rnd=new
@jcarlosroldan
jcarlosroldan / download_file.py
Last active October 2, 2018 19:24
Download file to local with a nice progress bar and time estimate.
from datetime import timedelta
from requests import get
from time import time
from sys import stdout
def bytes_to_human(size, decimal_places=2):
for unit in ["", "k", "M", "G", "T", "P", "E", "Z", "Y"]:
if size < 1024: break
size /= 1024
return "%s%sB" % (round(size, decimal_places), unit)
@jcarlosroldan
jcarlosroldan / euler.py
Created July 24, 2018 12:00
A set of very optimised math auxiliar functions that I used in Project Euler.
from math import sqrt, ceil
from itertools import zip_longest, compress, chain, product, combinations
from functools import reduce
def prime_sieve(n):
"""
Returns: list of primes, 2 <= p < n
Performance: 0.5s for 10**7, 5s for 10**8, 69s for 10**9
"""
sieve = [True] * int(n / 2)
@jcarlosroldan
jcarlosroldan / mail.py
Last active July 24, 2018 11:56
Send a mail using SMTP
from smtplib import SMTP
def send_gmail(to, subject, text, username="********@gmail.com", password="********"):
""" Send a mail using simple SMTP. """
server = SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username, password)
msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n%s"%(username, to, subject, text)
server.sendmail(username, to, msg)
server.quit()