Skip to content

Instantly share code, notes, and snippets.

View peterc's full-sized avatar
🏠
Working from home

Peter Cooper peterc

🏠
Working from home
View GitHub Profile
@peterc
peterc / yql.rb
Created February 8, 2009 13:49
Quick and messy YQL client
require 'nokogiri'
require 'open-uri'
require 'cgi'
require 'ostruct'
# YQL - Yahoo! Query Language
# http://developer.yahoo.com/yql/console/
class YQL
BASE_URL = 'http://query.yahooapis.com/v1/public/yql?format=xml&q='
@peterc
peterc / domainavailable
Created February 13, 2009 13:30
Check if a domain is available to register (uses DNS before WHOIS to be fast) - supports .com,.net,.org,.co.uk - inspired by foca/giles http://gilesbowkett.blogspot.com/2009/02/simple-bash-domain-availability.html
#!/bin/bash
# domainavailable
# Fast, domain name checker to use from the shell
# Use globs for real fun:
# domainavailable blah{1..3}.{com,net,org,co.uk}
# Inspired by foca / giles:
# http://gilesbowkett.blogspot.com/2009/02/simple-bash-domain-availability.html
trap 'exit 1' INT TERM EXIT
@peterc
peterc / deploy
Created February 25, 2009 15:00
#!/bin/bash
REMOTE_HOST='whatever'
USERNAME='whatever'
git push origin master && ssh ${REMOTE_HOST} "cd /home/${USERNAME}/www/rails;git pull origin master;chown -R ${USERNAME}:${USERNAME} *"
#!/usr/bin/ruby
# Fetch Jakob Nielsen's Alertbox and turn it into a basic RSS feed
%w{rubygems hpricot open-uri rss/maker}.each { |l| require l }
puts "Content-type: text/xml\n\n"
feed = RSS::Maker.make("2.0") do |rss|
rss.channel.title = "Jakob Nielsen's Alertbox"
# Test a Wordpress blog for compliance with /feed, /feed/ and
# similar requests from normal Feed clients and FeedBurner
# == HELPERS
require 'net/http'
# Fetch a URL without processing redirects and return status code and body
def get_url(url, agent)
iurl = URI.parse(url)
req = Net::HTTP::Get.new(iurl.path, { "User-Agent" => agent })
# Monitor HTTP requests being made from your machine with a one-liner..
# Replace "en1" below with your network interface's name (usually en0 or en1)
sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E "Host\: .*|GET \/.*"
# OR.. to be able to use as "httpdump" from anywhere, drop this into ~/.bash_profile:
# (again replace "en1" with correct network interface name)
alias httpdump="sudo tcpdump -i en1 -n -s 0 -w - | grep -a -o -E "Host\: .*|GET \/.*""
# All the above tested only on OS X.
@peterc
peterc / urlmonitor
Created April 4, 2009 01:30
urlmonitor - print out the URLs requested system wide on the main network interface
# urlmonitor - print out the URLs requested system wide on the main network interface
# Accept a network interface name as an optional argument
iface = ARGV.first
# No interface specified? Try to guess which one is king..
unless iface
`ifconfig -l`.split.each do |iface|
next if iface =~ /^lo/
break if `ifconfig #{iface}` =~ /inet (0|1|2)/
# Basic Ruby event driven "chat" server
# Heavily based on example code at http://rev.rubyforge.org/rdoc/
# Works on Ruby 1.9.1 - not tested on 1.8.
require 'rev'
# Configuration
HOST = 'localhost'
PORT = 1234
#!/usr/bin/env ruby
# runas - Run another program under the privileges of a specified user and group.
# This is necessary because sudo demands a password, as we need it to be hands off.
# A poor man's suexec basically.
require 'etc'
user, group, cmd = ARGV
# Get the 50/20 week means of markets
# Sloppy ass coding that was a quick hack one night many moons ago..
# Easily changed to do 50/20 days instead, not posting that one :)
require 'fastercsv'
require 'open-uri'
class Array; def sum; inject( nil ) { |sum,x| sum ? sum+x : x }; end; def mean; sum.to_f / size; end; end
fifty_weeks_ago = Time.now - 30240000