Skip to content

Instantly share code, notes, and snippets.

@mitchellh
Created August 25, 2011 17:44
Show Gist options
  • Save mitchellh/1171262 to your computer and use it in GitHub Desktop.
Save mitchellh/1171262 to your computer and use it in GitHub Desktop.
They see me wiresharkin'... they hatin'...
#!/usr/bin/env python
"""
This Python scripts listens for all HTTP requests to the
Turntable CDNs and downloads the file requested into the
current directory.
Disclaimer: This is a proof of concept. The author of this
script is not responsible for how this is used.
"""
import os
import re
import shlex
import subprocess
# A set to keep track of all the songs we already downloaded
downloaded = set()
# Execute tshark::
# * Listen for TCP connections
# * Listen for HTTP requesting limelight or static turntable host
# * Output the full request URI of the HTTP request
command = "/usr/local/bin/tshark -l -f tcp -R 'http.host == \"fp-limelight.musicnet.com\" or http.host == \"static.turntable.fm\"' -z 'proto,colinfo,http.request.full_uri,http.request.full_uri'"
process = subprocess.Popen(shlex.split(command),
stdout=subprocess.PIPE,
stderr=open(os.devnull))
while True:
line = process.stdout.readline()
if not line: break
# Parse the full URL from the line if available
regex = r".*http\.request\.full_uri == \"(.+)\""
match = re.match(regex, line)
if not match: continue
# Extract the capture which contains the URL
url = match.group(1)
# Ignore URLs we've already downloaded
if url in downloaded: continue
# Download the new song and add it to the set of downloaded
# to make sure we never download it again.
print "Downloading: %s" % url
downloaded.add(url)
subprocess.call(shlex.split("/usr/bin/curl -O %s" % url))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment