Skip to content

Instantly share code, notes, and snippets.

View hornc's full-sized avatar
👾
🎼

Charles Horn hornc

👾
🎼
  • Wellington, New Zealand
View GitHub Profile
@hornc
hornc / iacite.py
Created June 22, 2023 21:09
Generate a Wipkipedia cite book template for an archive.org identifier (with optional page number + link)
#!/usr/bin/env python
import argparse
import internetarchive as ia
import biblionames
#from isbn_hyphenate import hyphenate
from isbnlib import mask as hyphenate
OCLC_TAG = 'urn:oclc:record:'
@hornc
hornc / rename_images_and_make_pdf.sh
Created June 1, 2023 08:03
Rename images to correctly sort them, and make a pdf
# Install reqs
sudo apt install rename
# Test rename command:
rename -n 's/(\d+)(?=.*\.)/sprintf("%03d",$1)/eg' *.jpg
# Bash commands to 0 pad (to 3 digits) numbers in .jpg filenames
rename 's/(\d+)(?=.*\.)/sprintf("%03d",$1)/eg' *.jpg
@hornc
hornc / validISSN.py
Created May 18, 2023 03:06
Validate and format ISSNs
# Validate and format ISSNs
def validISSN(issn):
issn = issn.upper().replace('X', 'A').replace('-', '')
return len(issn) == 8 and sum((8 - i) * int(n, 16) for i, n in enumerate(issn)) % 11 == 0
def formatISSN(issn):
issn = issn.upper().replace('-', '')
return f'{issn[:4]}-{issn[4:]}'
from baseconv import base58
from isbnlib import is_isbn13, check_digit13
def compress_isbn(isbn):
# compresses a valid checkdigit ISBN13
isbn = isbn.replace('-', '')
assert is_isbn13(isbn)
prefix = isbn[:3]
body = isbn[3:-1]
@hornc
hornc / validISNI.py
Last active November 20, 2022 21:21
Validate and ISNI
"""
[copied from https://github.com/internetarchive/openlibrary/pull/6841#issuecomment-1207507062]
I'm recording this here because there is very little information currently about ISNI check digits online.
ISNI uses a checkdigit {0-9, X} calculation system which is called something like "MOD 11-2" and is
defined in the standard [ISO 7064](https://en.wikipedia.org/wiki/ISO/IEC_7064), but you have to pay to read it....
The same "MOD 11-2" system is used by the
Chinese [Resident Identity Card](https://en.wikipedia.org/wiki/Resident_Identity_Card),
which has its own specs and more publicly available code examples.
@hornc
hornc / gc.py
Last active January 10, 2023 23:36
G_arD^EN CorUtY@rD esolang interpreter
#!/usr/bin/env python
"""
Esoteric programming language interpreter for
G_arD^EN CorUtY@rD
https://esolangs.org/wiki/G_arD%5EEN_CorUtY@rD
Interpreter by Salpynx, 2022.
"""
import sys
@hornc
hornc / pypmm.py
Last active September 9, 2022 03:48
"""
Python Portable Minsky Machine
Implements a Python version of
https://esolangs.org/wiki/Portable_Minsky_Machine_Notation
"""
counters = {}
def inc(counter):
@hornc
hornc / isbnregions.sh
Created November 30, 2021 01:42
Takes an input list of ISBN13s and counts how many belong to each of the possible regional agencies.
#!/bin/bash
# Takes an input file argument (a sorted list of ISBN13s)
# and counts how many ISBNs are present belonging to each
# regional agency as listed in the official
# ISBN export_rangemessage.xml data.
wget -nc https://www.isbn-international.org/export_rangemessage.xml
while read prefix agency; do
#!/usr/bin/env python3
# Based on Google Drive v3 API Quickstart example
# https://developers.google.com/drive/api/v3/quickstart/python
import os.path
import io
import sys
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
@hornc
hornc / barcode-checks.py
Created July 2, 2020 03:10
testing some barcode check digit validations
# Library codabar (14 digit barcode) check digit validator
# (1 digit):material type + (4 digits):library code + (8 digits):item code + (1 digit): checksum
# source: http://www.makebarcode.com/specs/codabar.html
# - GOOD!
def vcodabar(n):
"""Validate the checkdigit of a 14 digit library barcode."""
check = int(n[-1])
data = n[:-1]
total = 0
for i, c in enumerate(data):