Skip to content

Instantly share code, notes, and snippets.

View mitchellrj's full-sized avatar

Richard Mitchell mitchellrj

View GitHub Profile
@mitchellrj
mitchellrj / base.py
Created July 25, 2014 08:21
Tastypie - make fields read only for object update but not for creation
from tastypie.resources import ModelResource as BaseModelResource
class ReadOnlyFieldsAfterAddMeta(BaseModelResource.__metaclass__):
def __new__(cls, name, bases, attrs):
new_class = super(ReadOnlyFieldsAfterAddMeta, cls).__new__(cls, name, bases, attrs)
read_only_fields_after_add = getattr(new_class._meta, 'read_only_after_add', ())
for field_name in read_only_fields_after_add:
@mitchellrj
mitchellrj / watch-and-revert.sh
Last active August 29, 2015 14:07
Script to watch a file for modifications and revert it immediately
#!/bin/bash
#
# Usage: watch-and-revert.sh filename
#
BACKUP="$(dirname $1)/.backup-$(basename $1)"
function cleanup {
rm $BACKUP
}
trap cleanup EXIT
cat $1 > $BACKUP
@mitchellrj
mitchellrj / README.rst
Last active November 14, 2015 11:44
Correct naming and tags of music files using MusicBrainz

Corrects the filenames and metadata of music files.

Assumptions

  • You want your music files named:

    ALBUM/TRACK-NUMBER - ARTIST - TRACK-TITLE.EXT
    
@mitchellrj
mitchellrj / cue-split.py
Created November 24, 2015 15:44
Script to split a single audio file into multiple files using a Cue file.
#!env python3
import argparse
import datetime
import enum
import math
import os.path
import shlex
import shutil
import subprocess
@mitchellrj
mitchellrj / fixed_point.py
Created March 27, 2016 18:11
Create and read fixed-point byte representations of numbers in Python
import decimal
class FixedPoint(decimal.Decimal):
def to_fixed_point(self, whole_bits, fraction_bits, signed=False):
# returns bytes aligned to the last fraction bit
total = round(self * (1 << fraction_bits))
num_bytes = (whole_bits + fraction_bits) / 8
if num_bytes % 1:
@mitchellrj
mitchellrj / settings.py
Created May 17, 2016 11:35
Show all queries in console for Django
LOGGING = {
'handlers': {
'console': {
# ...
},
# ...
},
'loggers': {
'django.db.backends': {
'handlers': ['console'],
@mitchellrj
mitchellrj / Dockerfile
Created January 2, 2018 21:01
home-assistant.io Docker
FROM alpine:latest
EXPOSE 8123
VOLUME ["/root/.homeassistant"]
RUN ["apk", "update"]
RUN ["apk", "add", "--no-cache", "g++", "python3-dev", "python3", "linux-headers"]
RUN ["python3", "-m", "pip", "install", "homeassistant", "netdisco", "sqlalchemy", "warrant", "xmltodict"]
# Trim the fat
@mitchellrj
mitchellrj / export_single_slack_channel.py
Last active January 4, 2019 10:44
Export history from a single Slack channel
#!/usr/bin/env python3
import csv
import datetime
import getpass
import os
import re
import sys
import time
@mitchellrj
mitchellrj / plex-auto-updater.sh
Created May 12, 2018 19:17
CentOS / Red Hat auto updater script for Plex media server
#!/bin/sh
plex_token="YOUR TOKEN HERE"
plex_rpm_url="https://plex.tv/downloads/latest/1?channel=8&build=linux-ubuntu-x86_64&distro=redhat&X-Plex-Token=$plex_token"
current_version="$(rpm -q --queryformat '%{VERSION}\n' plexmediaserver)"
latest_version="$(curl -I $plex_rpm_url 2>/dev/null | egrep '^Location:' | cut -d'/' -f 5 | cut -d'-' -f1)"
if [ "$current_version" != "$latest_version" ]; then
rpm -Uvh $plex_rpm_url >/dev/null 2>&1
success="$?"
@mitchellrj
mitchellrj / README.md
Last active June 4, 2018 16:52
RFC6225 value calculator

I have no idea if this is a correct implementation or not, as I have no reference implementation to compare it to. I swear there used to be an implementation as part of Android but I can't find it now.