Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Every time you visit for the FIRST TIME today... [Part 1]

Name: Anonymous 2013-10-20 22:56

Post a random function that you made. May be from any project you've done or make one impromptu.

Any QUALITY is allowed. No bullying!

Name: Anonymous 2013-11-18 7:36

I stuck this in a cronjob repeated a few times, and it downloads a random Touhou doujin CD from the TLMC to the pwd (which is set in the cronjob to be my mp3 player's SD card, if I have it plugged in overnight) so that I have some new random albums to listen to the next day at work. It's probably one of the worst pieces of code I've ever written, because I don't actually know Perl, so I just cobbled it together from help pages on CPAN.

#!/usr/bin/perl -w

use strict;
use warnings;
use Encode qw(decode_utf8);
use File::Temp qw(tempdir tempfile);
use File::Path qw(make_path);
use HTML::Parser;
use LWP::Simple qw(getstore);
use LWP::UserAgent;
use URI::Escape qw(uri_unescape);

my $ua = LWP::UserAgent->new;
$ua->agent("Some kind of randomized wget machine/0.2.1");
my $pl_parser = HTML::Parser->new( api_version => 3,
start_h => [\&pl_start, "tagname, attr"],
end_h => [\&pl_end, "tagname"],
marked_sections => 1,
);
my $req = HTTP::Request->new(GET => 'http://otokei-douj.in/');
my $pl_in_playlist = 0;
my @playlist = ();

my $res = $ua->request($req);
if ($res->is_success) {
$pl_parser->parse(decode_utf8($res->content));
} else {
die ($res->status_line . "\n");
}

if ($#playlist < 0) {
die ("Couldn't get any elements - has the site changed layout?");
}

my $subpage = "http://otokei-douj.in/" . $playlist[rand @playlist];
$req = HTTP::Request->new(GET => $subpage);
@playlist = ();
$res = $ua->request($req);
if ($res->is_success) {
$pl_parser->parse(decode_utf8($res->content));
} else {
die ($res->status_line . "\n");
}

if ($#playlist < 0) {
die ("Couldn't get any elements - has the site changed layout?");
}

my $dlpage = "http://otokei-douj.in/download.php" . $playlist[rand @playlist];
my $dir = tempdir( CLEANUP => 1 );
my ($fh, $filename) = tempfile( DIR => $dir, SUFFIX => '.7z' );
my @title_parts = ($dlpage =~ /link=%2F(.*)&album=(.*)/);
system("wget -q \"$dlpage\" -O \"$filename\"");

# One side effect of not pulling the link exactly is that '+' is
# present where '%20' would be if a second page were followed. Actual
# '+' characters are still represented as %2B, so this will not harm
# actual filenames
$title_parts[0] =~ s/\+/%20/g;
$title_parts[0] = decode_utf8(uri_unescape($title_parts[0]));
make_path($title_parts[0]);
system("7z x \"-o$title_parts[0]\" \"$filename\"");

# ----------------------------------------------------------------------
# Parsing functions:
sub pl_start {
my ($tagname, $attr) = @_;
if ($tagname eq "ul" && $attr->{'class'} eq "playlist") {
$pl_in_playlist = 1;
} elsif ($tagname eq "a" && $pl_in_playlist == 1) {
push (@playlist, $attr->{'href'});
}
}

sub pl_end {
my ($tagname) = @_;
if ($tagname eq "ul") {
$pl_in_playlist = 0;
}
}

Name: Anonymous 2013-11-18 15:12

>>85
Touhou vocals are shit.

Name: Anonymous 2013-11-18 16:28

>>82
I can't tell what the heckie this is supposed to do!

Name: Anonymous 2013-11-19 0:44

>>85
use HTML::TreeBuilder::XPath;

my $tree = HTML::TreeBuilder::XPath->new;
my $host = 'http://otokei-douj.in';

my $res = $ua->request(HTTP::Request->new(GET => "$host/"));
die "$res->status_line" unless $res->is_success;

$tree->parse(decode_utf8($res->content));
my @playlist = $tree->findnodes('//ul[@class="playlist"]/li/a');
my $album_url = $playlist[rand @playlist]->attr('href');

Insert error handling as desired, also you might want to make this a sub since you call it a couple of times (once for the main page, once for the artist(?) page)

Name: Anonymous 2013-11-19 4:55

>>85
N-n-nice find.
How do you guys even find these? That server is is so fast, and that page is so fucking parsable. Isn't it all a php|perl a script? I want to embed HTSQL so badly to it. If someone donates me 200, I'll download everything, and make a schema.

But I agree with >>86, vocals are shit, no matter the song.

Do they even have fakumen's work?:
https://www.youtube.com/watch?v=t0PcB9RzBnU&list=PL8F1671253111E1D6
https://www.youtube.com/watch?v=SQQy8W-gUb4&list=RDSQQy8W-gUb4

Facepalm:
http://fuuka.otokei-douj.in
Whomever is admin, is boss.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List