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:]}'
@hornc
hornc / move_editions.py
Last active January 31, 2023 01:34
Move OL editions to correct Work
from olclient.openlibrary import OpenLibrary
ol = OpenLibrary()
def move_editions(editions, master):
"""Sets a list of <editions> to a new master work ID using the Open Library Client.
Copies across extra author data from editions to master, if not already present.
Example:
move_editions(['OL8306567M','OL16682544M'], 'OL4005510W')
"""
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 / 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 / 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 / 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
@hornc
hornc / APLBAONWSJAS.py
Last active October 14, 2021 17:00
A ridiculous interpreter for the joke esoteric programming language APLBAONWSJAS https://esolangs.org/wiki/APLBAONWSJAS
#!/usr/bin/env python3
import re
import subprocess
"""
This is a ridiculous interpreter for the joke esoteric programming language APLBAONWSJAS
see https://esolangs.org/wiki/APLBAONWSJAS for details.
!!!WARNING!!! this takes user input and runs it without checking as system commands,
this could have unintended consequences, and what you type could make changes to your