Skip to content

Instantly share code, notes, and snippets.

View tombasche's full-sized avatar

Thomas Basche tombasche

  • Helsinki, Finland
View GitHub Profile
@tombasche
tombasche / 0_reuse_code.js
Last active September 14, 2015 06:20
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@tombasche
tombasche / json_error_catch.php
Created December 18, 2015 04:41
JSON Error Catch
<?php
/** \file jsonError.php
* \brief This code defines an error catch function to detect any errors in the JSON formatting. It converts the obscure code to the definition as provided in the JSON documentation.
* @see http://php.net/manual/en/function.json-last-error.php
*
*/
function jsonErrorCatch() {
switch (json_last_error())
{
//JSON_ERROR_DEPTH
@tombasche
tombasche / removeEmptyFolders.bat
Created April 30, 2016 06:52
Run in a directory to remove all empty folders (in windows)
for /f "delims=" %%d in ('dir /s /b /ad ^| sort /r') do rd "%%d"
@tombasche
tombasche / cdver.py
Last active November 4, 2016 08:03
Dronever
#inspired by http://cube-drone.com/comics/c/version-sacrifice
# note: you'll need to pip install pygithub
import time
import urllib2
import random
from github import Github
def nvl(var, val):
if var is None:
return val
param (
[Parameter(Mandatory=$true)][string]$Hostname
)
$failure = ""
while (!$failure) {
try {
$net = [System.Net.Dns]::GetHostAddresses($Hostname)
$Host.UI.RawUI.BackgroundColor = ($bckgrnd = 'DarkGreen')
} catch [Exception] {
$failure = "yes"
@tombasche
tombasche / merge.py
Last active January 12, 2017 22:48
Merges all pdf files in a directory (needs pypdf - get using pip). Instructions: pip install pillow pip install pypdf Backup any images first Ensure any mergedpdf files are deleted run python merge.py in the directory with the images/pdfs
import os
import copy
from PIL import Image
from pyPdf import PdfFileWriter, PdfFileReader
def append_pdf(input,output):
[output.addPage(input.getPage(page_num)) for page_num in range(input.numPages)]
output = PdfFileWriter()
cwd = os.getcwd()
@tombasche
tombasche / popclock.py
Last active March 1, 2017 22:15
Population Clock - using the ABS population API
import requests, json, time, os
ABS_URL = 'http://www.abs.gov.au/api/demography/populationprojection'
def getData():
r = requests.get(ABS_URL)
return json.loads(r.text)
def getPopulation():
text = getData()
@tombasche
tombasche / urlfuzzer.py
Last active April 11, 2017 09:54
Fuzzer to find folders and common files on websites
import requests
import os, sys
url = sys.argv[1]
dicts_dir = sys.argv[2]
result_dir = sys.argv[3]
def import_dict(dir, filepath):
file = open(os.path.join(dir,filepath), 'r')
return [item for item in file]
@tombasche
tombasche / process.py
Created August 17, 2017 22:52
Index a json file into elasticsearch pip install elasticsearch
import json
import sys
import elasticsearch
def send_to_es(payload, _id, list_name):
es = elasticsearch.Elasticsearch()
es.index(index=list_name, doc_type='item', id=_id, body=payload)
@tombasche
tombasche / validate_email.py
Last active July 20, 2018 20:28
Simple function and test to show off hypothesis
import re
from hypothesis import given, strategies as st
EMAIL_REGEX = re.compile(r'[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+')
def validate_email(email):
""" Validates an email address"""
if EMAIL_REGEX.match(email):