Skip to content

Instantly share code, notes, and snippets.

@greencoder
greencoder / copy.py
Last active January 18, 2023 22:38
Randomly copy MP3 files to a new directory until the target directory reaches a certain file size
import pathlib
import random
import shutil
mp3_files = list(pathlib.Path('/Users/scott/Desktop/Music').rglob('*.mp3'))
dst_dir = pathlib.Path('/Users/snewman/Desktop/CopiedMusic')
TARGET_SIZE = 1979999999
for x in range(0, len(mp3_files)):
@greencoder
greencoder / download.py
Last active August 14, 2022 03:23
Download MP3 Files From a Podcast Feed
import feedparser
import pathlib
import posixpath
import requests
import tqdm
import urllib
feed = feedparser.parse('https://feeds.buzzsprout.com/981391.rss')
urls = []
@greencoder
greencoder / sample.py
Last active November 4, 2021 16:55
Argparse Example with Date and Directory Validation
import argparse
import arrow
import pathlib
def valid_date_arg(value):
""" Used by argparser to validate date arguments """
try:
return arrow.get(value, 'YYYY-MM-DD')
except arrow.parser.ParserError:
msg = f'Not a valid date in YYYY-MM-DD format: "{value}"'
@greencoder
greencoder / app.js
Created June 16, 2021 19:13
Vue.js Boilerplate (No CLI needed)
function main() {
window.app = new Vue({
el: 'div#app',
store: store,
data: {
message: 'Hello from Vue!'
},
mounted() {
this.$store.commit('updateColor', 'purple');
},
@greencoder
greencoder / blank.html
Last active June 16, 2021 19:00
Blank HTML Document
<!DOCTYPE html>
<html lang="en">
<head>
<title></title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="shortcut icon" href="#" />
<style></style>
<script></script>
</head>
@greencoder
greencoder / animated_gif.py
Created May 18, 2021 19:55
Create "pulsing" animated gifs with Pillow
from PIL import Image
colors = {
'prearrival': (165, 226, 250, 255),
'arrived': (253, 245, 210, 255),
'waiting': (170, 255, 190, 255),
'waiting_long': (255, 160, 159, 255),
'unreviewed': (255, 160, 159, 255),
}
@greencoder
greencoder / jinja2_example.py
Created May 4, 2021 15:09
Jinja2 Setup Example
import arrow
import jinja2
# Set up the template engine
template_loader = jinja2.FileSystemLoader('./templates')
template_env = jinja2.Environment(loader=template_loader)
template_env.filters['fmt_date'] = lambda v: arrow.get(v, fmt).format(fmt)
# Write the file
template = template_env.get_template('template.html')
@greencoder
greencoder / argparse_example.py
Last active October 26, 2021 15:27
Python Argparse Example
import argparse
import pathlib
def valid_dir_arg(value):
""" Used by argparse to determine if the value is a valid directory """
filepath = pathlib.Path(value)
if not filepath.exists() or not filepath.is_dir():
msg = f'Error! This is not a directory: {value}'
raise argparse.ArgumentTypeError(msg)
@greencoder
greencoder / fix.sh
Created September 23, 2020 15:44
Fix broken virtualenv (dyld: Library not loaded error)
#!/usr/bin/env bash
ENV_PATH="$(dirname "$(dirname "$(which pip)")")"
SYSTEM_VIRTUALENV="$(which -a virtualenv|tail -1)"
BAD_ENV_PATHS="/usr/local"
echo "Ensure the root of the broken virtualenv:"
echo " $ENV_PATH"
@greencoder
greencoder / convert.py
Created September 16, 2020 15:49
Rename all the RSN files from snesmusic.org with their titles and create a zip file
import glob
import pathlib
import os
import shutil
import unrar.rarfile
# Convert the title to a string that is safe to use as a filename
def make_filename(string):
keepcharacters = (' ','.','_')
string = string[2:]