Skip to content

Instantly share code, notes, and snippets.


Filippo Valsorda FiloSottile

View GitHub Profile
FiloSottile /
Created March 12, 2012 13:52
Simple script to dump an IMAP folder into eml files
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import imaplib
import getpass
import argparse
argparser = argparse.ArgumentParser(description="Dump a IMAP folder into .eml files")
argparser.add_argument('-s', dest='host', help="IMAP host, like", required=True)
argparser.add_argument('-u', dest='username', help="IMAP username", required=True)
FiloSottile /
Created March 15, 2012 23:44
Download a youtube playlist using rg3/youtube-dl by @orschiro
import shutil
import os
import sys
import subprocess
import re
# Settings
root_folder = 'C:/Users/Robert/Videos/YouTube/Playlists/'
destination_regex = re.compile(r'^\[download\] Destination: (.*)$', re.M)
FiloSottile /
Created March 18, 2012 16:39
Script to follow HTTP redirects and print the resulting URL - a.k.a. URL un-shortener
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
import urllib2
# This script uses HEAD requests (with fallback in case of 405)
# to follow the redirect path up to the real URL
# (c) 2012 Filippo Valsorda - FiloSottile
# Released under the GPL license
FiloSottile /
Created March 18, 2012 16:43
How to send a HEAD HTTP request in Python with urllib2
import urllib2
class HeadRequest(urllib2.Request):
def get_method(self):
return "HEAD"
class HEADRedirectHandler(urllib2.HTTPRedirectHandler):
Subclass the HTTPRedirectHandler to make it use our
HeadRequest also on the redirected URL
FiloSottile / gist:2176911
Created March 24, 2012 01:00
Youtube video URL regex from `youtube-dl` explained
View gist:2176911
# The youtube-dl YouTube URL regex

	(?:https?://)?										# http(s):// (optional)
	(?:youtu\.be/|(?:\w+\.)?youtube(?:-nocookie)?\.com/|tube\.majestyc\.net/) # the various hostnames, with wildcard subdomains
	(?!view_play_list|my_playlists|artist|playlist)		# ignore playlist URLs
	(?:													# the various things that can precede the ID:
		(?:(?:v|embed|e)/)								# v/ or embed/ or e/
FiloSottile / gist:2352407
Created April 10, 2012 16:02
Get element by id from HTML document using only HTMLParser
View gist:2352407
import HTMLParser
class IDParser(HTMLParser.HTMLParser):
"""Modified HTMLParser that isolates a tag with the specified id"""
def __init__(self, id): = id
self.result = None
self.started = False
self.depth = {}
self.html = None
FiloSottile / Ubuntu-console.txt
Created April 19, 2012 17:07
Some testing around OS I/O encodings in python
View Ubuntu-console.txt
*** locale.getpreferredencoding() : UTF-8
u'\xe8' è
repr(sys.argv[1]) u'\xe8'
repr(raw_input()) u'\xe8'
*** sys.stdout.encoding : UTF-8
u'\xe8' è
repr(sys.argv[1]) u'\xe8'
repr(raw_input()) u'\xe8'
FiloSottile / rasterize.js
Created May 12, 2012 15:38
PhantomJS rasterize.js with "Retina" output
View rasterize.js
var page = require('webpage').create(),
address, output, size;
if (phantom.args.length < 2 || phantom.args.length > 3) {
console.log('Usage: rasterize.js URL filename');
} else {
address = phantom.args[0];
output = phantom.args[1];
page.viewportSize = { width: 1280, height: 1024 };
FiloSottile / gist:2667279
Created May 12, 2012 15:54
Render only an element with PhantomJS
View gist:2667279
page.clipRect = page.evaluate(function() {
return document.getElementById(ELEMENT_ID).getBoundingClientRect();
FiloSottile / rtmpdump.log
Created July 6, 2012 15:08
RTMP logs for weird YouTube videos
View rtmpdump.log
RTMPDump v2.5 GIT-2012-03-31 (Handshake 10 support by Xeebo)
(c) 2010 Andrej Stepanchuk, Howard Chu, The Flvstreamer Team; license: GPL
Connecting ...
WARNING: HandShake: Type mismatch: client sent 6, server answered 10
INFO: Connected...
ERROR: HandleCtrl: SWFVerification Type 2 request not supported, attempting to use SWFVerification Type 1! Patches welcome...
ERROR: RTMP_ReadPacket, failed to read RTMP packet header