All pastes #880068 Raw Edit

Etwas

public text v1 · immutable
#880068 ·published 2008-01-29 19:06 UTC
rendered paste body
#!/usr/bin/perl -w

use strict;
use LWP::Simple;

my $text = $ARGV[0];

if ( $text =~ /(http:\/\/|)(www\.|)youtube\.com\/watch\?v=/ ) {
        my ($url) = $text =~ /(youtube\.com.*?)(?:\s+|$)/;
        $url =~ s/(&|;|\$|{).*//;
        print $url." ";

        # Seite wird runtergeladen
        my $content = get "http://www.".$url;
        die "Couldn't get $url" unless defined $content;

        # Titel des Videos

        my $title;
        if ($content =~ m/<title>YouTube - (.*)<\/title>/) {
                $title = $1;
                # HTML Entities für Anführungszeichen werden ersetzt
                $title =~ s/&quot;/\"/gi;
                # Titel wird von einem evtl. störenden Zeilenumbruch befreit.
                chomp $title;
                if ( length( $title ) > 40 ) {
                        $title = substr($title, 1, 40) . "...";
                }
        }

        # Tags des Videos

        my $taglist;
        my $counter = 0;
        my @lines = split(/\n/, $content);
        foreach my $tag (@lines) {
                if ($tag =~ m/.*class="dg">.*/) {
                        $tag =~ s/.*dg\">//;
                        $tag =~ s/<.*//;
                        chomp $tag;
                        if ( length( $tag ) < 20 && $counter < 5 ) {
                                $counter++;
                                $taglist .= ", " if $taglist;
                                $taglist .= $tag;
                        }
                }
        }
        $taglist .= ", ..." if $counter == 5;
        if ( $title ) {
                print( "YouTube video: ".${title}." - tags: ".${taglist}."\n" );
        }
} else {
        print "There is no YouTube video.";
}