Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I've already posted a node to CUFP about an MP3 shuffler. This is solving a different problem. The other week I've succeeded in getting audio working on one of my Linux boxes - one with a reasonable sound card. The issue is that my music collection is on a different machine.

Having rejected NFS and Samba shares as an option for getting at the mp3 files, I explore the various options for playing audio, and discover that Kaffeine can take a URL. Excellent! I tweak the server's Apache config, and can now serve music files to my Linux box, when I point a browser at the mp3. Pasting the URL into Kaffeine wasn't completely successful as it couldn't cope with URL escaping, so I needed to manually replace all the %20 with spaces.

Next, I'm thinking that what would be really nice is to prime Kaffeine with a playlist of random tracks from my music collection. Having dug around the file system, I couldn't find where the playlist is stored, but eventually, I realised this was the wrong approach, and discovered the "Save" and "Import" functions. What do they save playlists as? Answer: XML! Bingo! I can now write some Perl to automate the whole process: discover the albums and artists on my server, and make some random selections. Save the URLs, unescaped as an XML file and Bob's your old man's brother.

#!/usr/bin/perl use strict; use warnings; use WWW::Mechanize; use XML::Writer; use IO::File; use URI::Escape; use Getopt::Long; my $server = 'http://orinoco/musicfiles/'; my $ntracks = 10; my $outfile = 'rand.kaffeine'; GetOptions( 'server=s' => \$server, 'tracks=i' => \$ntracks, 'output=s' => \$outfile, ); my $mech = WWW::Mechanize->new; $mech->get($server); my @artists = $mech->find_all_links(text_regex => qr(/$)); my @pick; for (1..$ntracks) { my $rart = int(rand(@artists)); $mech->follow_link( text => $artists[$rart]->text ); my @albums = $mech->find_all_links(text_regex => qr(/$)); my $ralb = int(rand(@albums)); $mech->follow_link( text => $albums[$ralb]->text ); my @tracks = $mech->find_all_links(text_regex => qr(^\d\d)); my $rtk = int(rand(@tracks)); push @pick, $tracks[$rtk]; $mech->back; $mech->back; } my $out = IO::File->new(">$outfile"); print $out "<!DOCTYPE XMLPlaylist>\n"; # Horrible hack: XML::Writer is + too strict my $xml = XML::Writer->new( OUTPUT => $out ); #$xml->doctype('XMLPlaylist', undef, undef); $xml->startTag( 'playlist', client => 'kaffeine'); for my $track (@pick) { $xml->emptyTag( 'entry', url=>uri_unescape($track->url_abs)); } $xml->endTag('playlist'); $xml->end;

Enjoy

--
wetware hacker
(Qualified NLP Trainer and Hypnotherapist)


In reply to Random tune picker for Kaffeine by rinceWind

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (7)
    As of 2014-08-21 06:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (128 votes), past polls