Skip to content

Instantly share code, notes, and snippets.

@jrivero
jrivero / OptionParser.py
Last active February 21, 2024 11:35
OptionParser boilerplate
if __name__ == '__main__':
parser = OptionParser()
options_a = [
["-s", "--spiders", dict(dest="spiders", type="int", default=1, help="sends more than one spider")],
["-S", "--nospinner", dict(dest="spinner", action="store_false", default=True, help="turns off the spinner")],
["-v", "--verbose", dict(dest="verbose", action="store_true", default=False, help="outputs every request (implies --nospiner)")],
["-d", "--depth", dict(dest="depth", type="int", default=-1, help="does a breadth-first crawl, stopping after DEPTH levels (implies --breadth)")],
["-b", "--breadth", dict(dest="breadth", action="store_true", default=False, help="does a breadth-first crawl; may be used with --depth")],
]
for s, l, k in options_a:
@jrivero
jrivero / LargestTables.sql
Created February 23, 2012 23:10
Finding out largest tables on MySQL Server
-- http://www.mysqlperformanceblog.com/2008/02/04/finding-out-largest-tables-on-mysql-server/
SELECT CONCAT(table_schema, '.', table_name),
CONCAT(ROUND(table_rows / 1000000, 2), 'M') rows,
CONCAT(ROUND(data_length / ( 1024 * 1024 * 1024 ), 2), 'G') DATA,
CONCAT(ROUND(index_length / ( 1024 * 1024 * 1024 ), 2), 'G') idx,
CONCAT(ROUND(( data_length + index_length ) / ( 1024 * 1024 * 1024 ), 2), 'G') total_size,
ROUND(index_length / data_length, 2) idxfrac
FROM information_schema.TABLES
ORDER BY data_length + index_length DESC
@jrivero
jrivero / array_reindex.php
Created March 8, 2012 18:05
Array reindex
<?php
// found at http://php.net/manual/en/function.array-filter.php
function array_reindex(array $source, $blacklist = array())
{
$i = 0;
foreach ($source as $key => $val) {
if ($key != $i) {
unset($source[$key]);
@jrivero
jrivero / index.php
Created March 14, 2012 22:40
Implementing Twitter sign-in with Silex and PHP
<?php
define('CONS_KEY', 'Application consumer key');
define('CONS_SECRET', 'Application consumer secret');
require_once __DIR__.'/silex.phar';
$app = new Silex\Application();
// register the session extension
$app->register(new Silex\Extension\SessionExtension());
@jrivero
jrivero / get_ip_address.php
Created March 15, 2012 10:02
The most accurate way to retrieve a user's correct IP address in PHP
<?php
// found http://stackoverflow.com/questions/1634782/what-is-the-most-accurate-way-to-retrieve-a-users-correct-ip-address-in-php
function get_ip_address()
{
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key){
if (array_key_exists($key, $_SERVER) === true){
foreach (explode(',', $_SERVER[$key]) as $ip){
$ip = trim($ip); // just to be safe
#!/bin/sh
wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gzip -d -f GeoIP.dat.gz
wget -N http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz
gzip -d -f GeoIPv6.dat.gz
wget -N http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gzip -d -f GeoLiteCity.dat.gz
@jrivero
jrivero / make_pretty.php
Last active February 21, 2024 11:34
Human redable sizes
<?php
// http://www.geekality.net/2010/11/17/php-output-a-number-of-bytes-in-a-human-readable-way/
function make_pretty($bytes)
{
$symbols = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB');
$exp = floor(log($bytes) / log(1024));
return sprintf('%.2f '.$symbols[$exp], $bytes/pow(1024, floor($exp)));
@jrivero
jrivero / hack.sh
Created March 31, 2012 13:32 — forked from erikh/hack.sh
OSX For Hackers
#!/usr/bin/env sh
##
# This is script with usefull tips taken from:
# https://github.com/mathiasbynens/dotfiles/blob/master/.osx
#
# install it:
# curl -sL https://raw.github.com/gist/2108403/hack.sh | sh
#
@jrivero
jrivero / cachedHTMLForURL.php
Created September 24, 2012 11:12
Retrieve Google Cached HTML
<?php
// http://hactheplanet.com/blog/11
function cachedHTMLForURL($url)
{
// Request the cache from Google.
$googleRequestURL = "http://webcache.googleusercontent.com/search?q=" . urlencode("cache:" . $url);
$googleResponse = file_get_contents($googleRequestURL);
@jrivero
jrivero / konami.js
Created April 2, 2013 10:42
Konami code with jquery
$(document).ready(function(){
var keys = [];
var konami = '38,38,40,40,37,39,37,39,66,65';
$(document)
.keydown(
function(e) {
keys.push( e.keyCode );
if ( keys.toString().indexOf( konami ) >= 0 ){
console.log("konami code")