Skip to content

Instantly share code, notes, and snippets.

View gabalese's full-sized avatar

Gabriele Alese gabalese

View GitHub Profile
@gabalese
gabalese / russian_roulette.py
Created July 23, 2016 12:22
A simple game of russian roulette played with the bbc:microbit. Reset the device when you "die"
from microbit import *
import random
choices = [1,2,3,4,5,6]
death_number = random.choice(choices)
click_counter = 0
while True:
display.show(Image.ASLEEP)
@gabalese
gabalese / sortncx.py
Created March 6, 2014 14:35
Python script to sort a NCX with no gaps in playOrder.
#! /usr/bin/env python
from lxml import etree as ET
import zipfile as ZIP
import sys
import os
namespaces = { "opf":"http://www.idpf.org/2007/opf",
"dc":"http://purl.org/dc/elements/1.1/",
"ncx":"http://www.daisy.org/z3986/2005/ncx/"
@gabalese
gabalese / checkisbn.py
Last active August 29, 2015 13:56
Script to check the formal validity of a given ISBN-10 or ISBN-13.
#! /usr/bin/env python
import sys
import re
def is_valid(isbn):
sum = 0
isbn = re.sub(r"[-–—\s]", "", isbn)
# in case of isbn13
if len(isbn) == 13 and isbn[0:3] == "978" or "979": # is it a ISBN? Thanks @librarythingtim
@gabalese
gabalese / html_splitter.py
Last active December 26, 2015 11:49
Splits a HTML file into several smaller files, given a tag and a (optional) class name. Also fixes links in the same document.
#! /usr/bin/env python
try:
import lxml.etree as ET
except ImportError:
from xml.etree import ElementTree as ET
from StringIO import StringIO
import sys
pub_id = "-//W3C//DTD XHTML 1.1//EN"
@gabalese
gabalese / simplewatermark.py
Created September 11, 2013 15:11
Basic example of how simple it is to add (slightly) less obvious watermarks to EPUB files with pyepub library and stepic
#! /usr/bin/env python
import os
from pyepub import EPUB # see https://github.com/gabalese/pyepub
from StringIO import StringIO
from PIL import Image
import stepic
epub = EPUB("startepub.epub","a")
@gabalese
gabalese / previewbuilder.py
Last active December 22, 2015 00:48
Raw script to build a quick and dirty preview from an existing full EPUB. Useful to send out previews of existing ebooks.
#! /usr/bin/env python
# by Gabriele Alese <gabriele@alese.it> / http://www.alese.it
# Released in Public Domain where applicable: http://creativecommons.org/publicdomain/zero/1.0/
# ***
# REQUIREMENTS:
# this script makes use of pyepub 2.0.9
# download here: https://github.com/gabalese/pyepub
# ***
# USAGE:
@gabalese
gabalese / attributealtfield.py
Last active December 21, 2015 04:29
Make a list of every img alt attribute in htmls and print it to stdout
#! /usr/bin/env python
# file: attributealtfield.py
# Make a list of every img alt attribute in htmls and print to stdout
# Usage: from the command line, python attributealtfield.py <epub.epub>
from __future__ import print_function
import os
import sys
import zipfile as ZIP
@gabalese
gabalese / tweet.py
Created June 3, 2013 15:04
Quick and dirty script to post a tweet from the CLI interface. Requires twitter library.
#! /usr/bin/env python3
from twitter import *
import os, sys
CONSUMER_KEY = <your_consumer_key>
CONSUMER_SECRET <your_consumer_secret_key>
MY_TWITTER_CREDS = os.path.expanduser('~/.token_cache')
if not os.path.exists(MY_TWITTER_CREDS):
try:
@gabalese
gabalese / annot2html.py
Last active December 16, 2015 20:19
Brain-dead simple utility to dump a HTML list out of a Sony Reader annot. db. (Made on a Mac, so it look for a /Volumes/READER mount. I'm way too lazy to amend that to accept a path argv[n])
#! /usr/bin/env python
# Brain-dead simple utility to dump a list of annotations from a Sony Reader .db
# Usage: $ python annot2html.py [int number of annot to fetch]
import sys
import sqlite3 as sql
import xml.etree.ElementTree as ET
# Mac-path!
file_db = sql.connect('/Volumes/READER/Sony_Reader/database/books.db')