Skip to content

Instantly share code, notes, and snippets.

View geraintwhite's full-sized avatar

Geraint White geraintwhite

View GitHub Profile
@geraintwhite
geraintwhite / index.js
Created November 20, 2014 15:08
NodeJS HTTP server testing
var http = require('http'),
bl = require('bl');
function handler(req, res) {
req.pipe(bl(function (err, data) {
if (err) {
console.log(err)
res.writeHead(400, {'Content-Type': 'text/plain'});
res.end('Error receiving data');
@geraintwhite
geraintwhite / tcp.py
Last active August 29, 2015 14:09
Python TCP chat server and client
from threading import Thread
import socket
import sys
class Server(Thread):
def __init__(self, host='', port=1337):
super().__init__()
self.host = host
self.port = port
@geraintwhite
geraintwhite / setup.sh
Last active January 25, 2016 09:40
Setup file for deployment system
#!/bin/bash
ssh_url="git@github.com"
base_path="/home/git/deploy"
repo_dir="$base_path/repos"
processing="$base_path/processing"
getter="$base_path/github-getter"
post_receive="$base_path/post-receive"
full_name="itsapi/github-getter"
@geraintwhite
geraintwhite / console.py
Created November 12, 2014 10:10
Python module to get terminal size
import sys
def getTerminalSize():
import os
env = os.environ
def ioctl_GWINSZ(fd):
try:
import fcntl, termios, struct, os
cr = struct.unpack('hh', fcntl.ioctl(fd, termios.TIOCGWINSZ, '1234'))
@geraintwhite
geraintwhite / analyser.py
Last active August 29, 2015 14:09
Disk Usage Analyser
import os
import sys
import colors
class Node():
_OPT = {
'depth': False,
'colors': True,
'abspath': False,
@geraintwhite
geraintwhite / colors.py
Last active August 29, 2015 14:07
Python terminal colours module
import sys
def _has_colors(stream):
if not hasattr(stream, 'isatty'):
return False
if not stream.isatty():
return False
try:
import curses
@geraintwhite
geraintwhite / suspend.sh
Created June 14, 2014 15:44
Script to suspend the system. I use with keyboard shortcut ctrl-alt-backspace. Works with my NFC screen unlock (https://gist.github.com/grit96/11268473).
#!/bin/bash
# Trigger my NFC unlock to lock the computer
curl http://localhost:6001/?secret=secret
# Send standby signal
dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend
@geraintwhite
geraintwhite / github.js
Created June 7, 2014 12:50
GitHub user feed widget
function github_req(user, cb) {
makeRequest(
'https://api.github.com/users/' + user + '/events',
function (text) {
var data = JSON.parse(text);
cb(data);
}
);
}
@geraintwhite
geraintwhite / bookmarklets.js
Created June 7, 2014 12:42
A selection of useful bookmarklets
/* Convert YouTube to MP3 */
javascript:window.open('http://www.video2mp3.net/loading.php?url='+window.location)
/* Include jQuery */
javascript:(function(a){if(!a.jQuery){var d=document,b=d.createElement('script');b.src='//code.jquery.com/jquery-latest.js';d.getElementsByTagName('head')[0].appendChild(b)}})(this)
/* View Source */
@geraintwhite
geraintwhite / unlock.js
Last active August 6, 2020 01:31
A Node.js app to use NFC to unlock/lock Ubuntu.
var http = require('http'),
url = require('url'),
exec = require('child_process').exec;
var locked = false;
var app = http.createServer(function(request, response) {
var url_parts = url.parse(request.url, true);
var path = url_parts.pathname.replace(/^\/|\/$/g, '');