Programming

screenshot-detrateshobo-music-internet_radio

The “easy” way to listen to internet radio in Ubuntu

I started with rhythmbox like most new Ubuntu users. It seemed nice enough but not in the area I was concerned with, internet radio. I tried out many players but was disappointed with different areas of different players. From Ubuntu 8.04 to 8.10 I was using the “good” Amarok (for KDE 3.5). Disappointed by the exclusion of that version in Ubuntu Jaunty 9.04 and unimpressed with workarounds like the PPA’s, I decided to play the field. I came across Exaile, which I’ve blogged about in the past. It’s a good enough player… most of the time. It crashed too often for my likings and I’m getting sick of pkilling it.

It struck me today that I needed a simplier more streamlined solution for my simple needs. I needed a console application. Through a little research, I found moc, which happens to play shoutcast streams as I’ve become accustom to.

I then proceeded to set myself the following way:

sudo apt-get install moc
mkdir -p ~/Music/internet_radio && cd ~/Music/internet_radio
wget -r -l2 -nd -Nc -A.pls http://www.di.fm/index.php
for file in *.pls; do mocp -a $file; done
mocp

1) Installed moc
2) created a directory to download all the playlists from di.fm (since this is the station I listen to most often)
3) wget all the playlists
4) add them all to moc
5) start moc and [tab] to the play list side, enter to play

screenshot-detrateshobo-music-internet_radio

  enter  -- starts playing
  s      -- stops playing
  n      -- plays next item from the playlist
  b      -- plays previous item from the playlist
  space  -- pause
  p      -- pause

  S      -- plays at random
  R      -- repeats the same song in a loop,
	    Next (X button below) must be OFF
  X      -- switches to play sequentially
  o      -- plays a file from the Internet
  u      -- moves playlist item up
  j      -- moves playlist item down
  Ctrl+u -- adds the URL to the playlist
  g      -- searches marked string in file names
  /      -- searches marked string in file names

  r      -- rereads the directory
  T      -- switches to the theme selection menu
  f      -- toggles display mode of song titles
  TAB    -- switches marker bar between the playlist
	    and the file manager panels
  l      -- switches between displaying the playlist
            or the file manager panel
  P      -- switches full path in the playlist
  H      -- toggles hidden files view
  Ctrl-t -- toggles song duration time
  Ctrl-f -- toggles format file view
  m      -- moves to directory entered in config file
  G      -- moves to directory with currently played file
  i      -- moves to marked directory
  U      -- moves to upper directory
  a      -- adds a file to the playlist
  A      -- adds a directory recursively to the playlist
  C      -- clears the playlist
  V      -- saves the playlist
  d      -- removes marked item from the playlist
  Y      -- removes all empty items from the playlist

  < -- decreases volume by 1%
  ,      -- decreases volume by 5%
  >      -- increases volume by 1%
  .      -- increases volume by 5%

  x      -- toggles the mixer channel
  ?      -- shows help

  !      -- goes to a fast dir 1 (set in config file)
  @      -- goes to a fast dir 2
  #      -- goes to a fast dir 3
  $      -- goes to a fast dir 4
  %      -- goes to a fast dir 5
  ^      -- goes to a fast dir 6
  &      -- goes to a fast dir 7
  *      -- goes to a fast dir 8
  (      -- goes to a fast dir 9
  )      -- goes to a fast dir 10

  F1     -- executes ExecCommand1 (set in config file)
  F2     -- executes ExecCommand2
  F3     -- executes ExecCommand3
  F4     -- executes ExecCommand4
  F5     -- executes ExecCommand5
  F6     -- executes ExecCommand6
  F7     -- executes ExecCommand7
  F8     -- executes ExecCommand8
  F9     -- executes ExecCommand9
  F10    -- executes ExecCommand10

Above commands from polish linux’s article on moc audo player, great resource.

codepad.org – an online compiler/interpreter, and a simple collaboration tool.

codepad.org is an online compiler/interpreter, and a simple collaboration tool.

Creating a simple news script that reads from an XML file

I recently recoded a website for a friend because his markup was pretty ugly (thanks to WYSIWYG editors). While I was doing that, he mentioned he’d like to have an xml file he could edit to update his news. Simple enough I said and banged out the following which includes a little helpful function that wraps paragraphs in HTML p tags. My friend alpha helped me rewrite some ugly regex with a novel idea:

print join '', map {qq[<p>$_</p>]} split (/\r?\n/,$object->content)

Of course, he’s a big perl coder so I had to create a php equivalent, nls2p.

Edit: Mookow mentioned he wanted to write links, so obviously a system would have to be put in place to support this. Using HTML tags in XML gets a little sloppy so I opted to use bbcode which is a very popular method to format text in web applications and very simple to add more filters.

The news post creator:

<?php
 
if (file_exists('news.xml')) {
    $xml = simplexml_load_file('news.xml');
	foreach ($xml->post as $post) {
	   echo "<h4>".$post->title." - ".$post->date."</h4>\n";
	   echo "<div>".bbcode(nls2p($post->content))."</div>\n";
	   echo "<p>--".$post->author."</p>\n";
	}
} else {
    echo "<p>Failed to open news source</p>";
}

The functions for formatting:

// Wrap paragraphs with <p> tags
function nls2p($str) {
	$split_str = preg_split('#[\t]+?[\r\n]{1}#',$str);
	foreach ($split_str as $line) {
		$new_str = $new_str."<p>".preg_replace("/[\t\n]/","",$line)."</p>\n";
	}
	return $new_str;
}
 
// Parse bbcode as HTML
function bbcode($string) {
	$search = array(
		'@\[(?i)b\](.*?)\[/(?i)b\]@si',
		'@\[(?i)i\](.*?)\[/(?i)i\]@si',
		'@\[(?i)u\](.*?)\[/(?i)u\]@si',
		'@\[(?i)img\](.*?)\[/(?i)img\]@si',
		'@\[(?i)url=(.*?)\](.*?)\[/(?i)url\]@si',
		'@\[(?i)code\](.*?)\[/(?i)code\]@si'
	);
	$replace = array(
		'<b>\\1</b>',
		'<i>\\1</i>',
		'<u>\\1</u>',
		'<img src="\\1">',
		'<a href="\\1" target="_blank">\\2</a>',
		'<code>\\1</code>'
	);
	return preg_replace($search , $replace, $string);
}
?>

The XML data file:

<?xml version='1.0' standalone='yes'?>
<news>
	<post>
		<title>Site looks update</title>
		<date>03-29-09</date>
		<author>Moo</author>
		<content>The site has been completely recoded by a friend of mine, -z- from NexuizNinjaz.com. He even added this fancy news script. Now, to update the news, I don't even have to touch index.php! Alright, I'm out for now.
 
		OMG A [b]NEW LINE[/b]!
 
		Now with [i]italics[/i] and [u]underlines[/u] and [url=http://www.nexuizninjaz.com]links[/url]
 
		[code]code blocks[/code]
 
		and images
 
		[img]http://www.xepic.net/pics/pic_091622001182920068.jpg[/img]
		</content>
	</post>
	<post>
		<title>Site looks update</title>
		<date>03-29-09</date>
		<author>Moo</author>
		<content>Yeah, I removed the stupid gray background, because after a while it got boring. I changed the "Random Shiznit" column into something that could be a bit more productive. Once all of the other sites are up, I'll update the main page with the latest news from those. This main news column will be for the more intriguing news. I also decided to add titles to the news articles...Duuhhhhhhh
 
		OMG A NEW LINE!
 
		and again, LOL!
		</content>
	</post>
	<post>
		<title>Site looks update</title>
		<date>03-25-09</date>
		<author>Moo</author>
		<content>I added a boring gray background to the main page, as you can see. The hover text-changing effect was removed because of its stupidity. A cool guy named Fabzor re-colored the header for me.</content>
	</post>
	<post>
		<title>Site looks update</title>
		<date>03-23-09</date>
		<author>Moo</author>
		<content>Website updated, not much. Cool hover effects were added, but we still don't have much color in the page. Hopefully, I can find a good background and get this show on the road.</content>
	</post>
	<post>
		<title>Site just started...</title>
		<date>03-22-09</date>
		<author>Moo</author>
		<content>Most of the links up top will not work for the time being, this site is just getting edited. Soon, we'll have a better webpage than this one, but for now, deal with this one!</content>
	</post>
</news>

That’s pretty much it verbatim, including mookow’s news ^_^. Hope this is helpful to someone.