1#!/usr/bin/env perl
2use strict;
3use warnings;
4use HTTP::Proxy;
5use HTTP::Proxy::BodyFilter::save;
6use Digest::MD5 qw( md5_hex);
7use POSIX qw( strftime );
8
9my $proxy = HTTP::Proxy->new(@ARGV);
10
11# a filter to save FLV files somewhere
12my $flv_filter = HTTP::Proxy::BodyFilter::save->new(
13    filename => sub {
14        my ($message) = @_;
15        my $uri = $message->request->uri;
16
17        # get the id, or fallback to some md5 hash
18        my ($id) = ( $uri->query || '' ) =~ /id=([^&;]+)/i;
19        $id = md5_hex($uri) unless $id;
20
21        # compute the filename (including the base site name)
22        my ($host) = $uri->host =~ /([^.]+\.[^.]+)$/;
23        my $file = strftime "flv/%Y-%m-%d/${host}_$id.flv", localtime;
24
25        # ignore it if we already have it
26        return if -e $file && -s $file == $message->content_length;
27
28        # otherwise, save
29        return $file;
30    },
31);
32
33# push the filter for all MIME types we want to catch
34for my $mime (qw( video/flv video/x-flv )) {
35    $proxy->push_filter(
36        mime     => $mime,
37        response => $flv_filter,
38    );
39}
40
41$proxy->start;
42
43