Skip to content

Instantly share code, notes, and snippets.

View pachacamac's full-sized avatar
🌶️
Mmmmhmmmm

Marc pachacamac

🌶️
Mmmmhmmmm
  • Hacker/Founder
  • Germany, Berlin
View GitHub Profile
@pachacamac
pachacamac / whereami.sh
Created June 30, 2011 17:34
Wanna know where you are?
#!/bin/bash
echo -n "{\"version\": \"1.1.0\",\"host\": \"maps.google.com\",\"request_address\": true,\"address_language\": \"en_GB\", \"wifi_towers\": [`iwlist scan 2> /dev/null | tr -d '\n' | sed -e 's/Cell [0-9]* - Address: \([0-9A-Z:]*\)[^C]*Channel:\([0-9]*\)[^S]*Signal level=\([0-9-]*\) dBm[^E]*E[^E]*ESSID:"\([^"]*\)"/\{"mac_address": "\1","signal_strength": \3,"age": 0,"channel": \2,"ssid": "\4"}/g' -e 's/[^{]*{/{/' -e 's/}[^{]*{/},{/g' -e 's/\}[^}]*$/\}/' `]}" | curl -s -X POST -d @/dev/fd/0 http://www.google.com/loc/json
# TIP: run with sudo for improved accuracy since iwlist can then provide more than the active hotspot
@pachacamac
pachacamac / mrisc.rb
Created July 7, 2011 10:20
A Simple Assembler Language and VM
#!/usr/bin/env ruby
class MRISC
def run(code)
tokens = code.gsub(/(\*.*?\*)|[^a-z0-9,-;@\._]/,'').split(';')
@vars,stack,i = {:_pc=>-1,:_oc=>0},[],0
tokens.map!{|t| t.chars.first=='@' ? (@vars[t.to_sym]=i-1;nil) : (i+=1;t.split(',').map{|e|numeric?(e) ? e.to_i : e.to_sym})}.compact!
while @vars[:_pc] < tokens.size-1
@vars[:_pc] += 1
@vars[:_oc] += 1
@pachacamac
pachacamac / snake.rb
Created July 21, 2011 23:29
A snake game in a hand full of Ruby
#!/usr/bin/env ruby
d,l,s,c,f,S=15,3,[[5,6],[5,5],[5,4]],[1,0],[rand(15),rand(15)],STDIN
system'stty -icanon -echoke'
loop do
k=S.getc if k=select([S],[],[],0.2)&&S.getc=="\e"&&S.getc==?[
c={?A=>[0,-1],?B=>[0,1],?C=>[1,0],?D=>[-1,0]}[k]||c
s=[[s[0][0]+c[0],s[0][1]+c[1]]]+s[0..-2]
exit if s[0][0]+1>d||s[0][1]+1>d||s[0][0]+1==0||s[0][1]+1==0||s[1..-1].index(s[0])
s+=[[s[0][0]+c[0],s[0][1]+c[1]]] if s[0]==f
f=[rand(d),rand(d)] if s.index f
@pachacamac
pachacamac / hough.rb
Created September 25, 2011 23:27
Ruby Hough Transformation
require 'mathn'
require 'rubygems'
require 'gd2'
include GD2
def hough_transform(img)
mx, my = img.w*0.5, img.h*0.5
max_d = Math.sqrt(mx**2 + my**2)
min_d = max_d * -1
hough = Hash.new(0)
@pachacamac
pachacamac / pushinfo.php
Created December 6, 2011 00:49
one line (needs to be 1 line!) push info script. stores simple text requests and or displays them back
<?header('Content-type: text/plain');if($d=htmlspecialchars(join("\n",array_merge($_GET,$_POST)))){@file_put_contents($f=__file__,array_shift(file($f)).$d);exit($d);}?>
@pachacamac
pachacamac / music.sh
Created December 6, 2011 01:37
music for my travel netbook
#!/bin/bash
mpg123 -C \
http://scfire-mtc-aa04.stream.aol.com:80/stream/1005/?smoothjazz \
http://ice.somafm.com/secretagent \
http://ice.somafm.com/groovesalad \
http://ice.somafm.com/covers \
http://ice.somafm.com/lush \
http://ice.somafm.com/indiepop \
http://ice.somafm.com/bootliquor \
http://ice.somafm.com/suburbsofgoa \
@pachacamac
pachacamac / google_speech_recognition.rb
Created December 11, 2011 10:52
google speech recognition with ruby
require 'rest_client'
require 'json'
a = `sox -d --norm -t .flac - silence -l 1 0 1% 1 6.0 1% rate 16k`
#a = `arecord -q -d 3 -c 1 -f S16_LE -r 22050 -t wav | flac - -f --totally-silent -o-`
r = RestClient.post 'https://www.google.com/speech-api/v1/recognize?lang=en-US', a,
:content_type => 'audio/x-flac; rate=16000'
if j = JSON.parse(r)
(p j; `espeak 'you said: #{j['hypotheses'].first['utterance']}'`)
end
@pachacamac
pachacamac / hough.rb
Created May 22, 2012 16:28
Bidirectional Hough Transformation with ChunkyPNG
require 'mathn'
require 'chunky_png'
def hough_transformation(img, cutoff=185, steps=1)
mx, my = img.width*0.5, img.height*0.5
to_rad = Math::PI / 180.0
hough = Hash.new(0)
img.width.times do |x|
#puts "#{x} of #{img.width}"
img.height.times do |y|
@pachacamac
pachacamac / clusterize.rb
Created May 22, 2012 19:11
Cluster algorithm for n-dimensional data and a given number of clusters
# A
# B
#
# D
#
# C
# E
#
# F
#
@pachacamac
pachacamac / oui_lookup.rb
Created May 31, 2012 07:44
Mac address OUI lookup
db, oui, company, address = {}, nil, nil, ''
lines = File.readlines('oui.txt') #http://standards.ieee.org/develop/regauth/oui/oui.txt
lines.each do |line|
#line.encode!('UTF-16', 'UTF-8', :invalid => :replace, :replace => '').encode!('UTF-8', 'UTF-16').rstrip!
line = line.unpack("C*").pack("U*").rstrip
if m = line.match(/^(\d\d-\d\d-\d\d)\s+\(hex\)\s+(.*?)$/)
oui, company = m[1], m[2]
elsif oui && company && m = line.match(/^\s+(.*?)$/)
address << m[1] << "\n"
elsif oui && company && line == ''