Archive for the ‘Unix’ Category

Writing an Amarok script/plugin by PHP to broadcast your music

Wednesday, February 18th, 2009

I am a music lover. So I listen to musics a lot. I not only listen to music, but also share it. For this purpose I made a php script long time ago that is used for song broadcasting. In windows I used winmap’s “dosomething” plugin to broadcast. Nowadays I listen song in my linux machine. No winamp is there. I use amarok to listen song. Amarok is a great player if you ever used it, you’ll realize. But I couldn’t broadcast song using amarok. Being a programmer I didnt search any plugin or script, rather I thought I’d write it myself.

I googled for writing amarok plugin. And I found these are not plugin actually. These are called script. Amarokscript. So I have to write amarok script. In the amarok wiki all the example was in Ruby and Python. But they said, any language can be used even bash would do. So I though why not try it with PHP. As I write PHP a lot. So It’ll be faster.

The amarok wiki in kde.org helped me a lot. Actually I have realy only that page and start writing the script. Bellow goes the main howto.

HOWTO:

An amarokscriopt is an tar archive file. It contians.

script/
README
COPYING
script.php (executable script)
script.spec

README: When you click on the about button/tab for this script in amarok the contents of this file is shown.

COPYING: If you have this file a tab/button named License will be shown. And the text from there will be contained there.

script.php: its the main script. It MUST be executable. In example I used .php extension. It can be any extension.  For Ruby its .rb, for python its .py, for perl its .pl, for binary it can be empty or .o. the main point is it MUST be executable.

script.spec: Its the file where you’ll write configureation values for amarok. Amarok will read this file and determine which file is the main script and what type of script is this.  In spec file there are 2 main entries. Name and type. If name is not given the first part of the .spec file is used as name. And type is a must.

The main script for me was in PHP. The main script should be executable. So I change the mode of the script to executable.

chmod +x songinfo-broadcaster.php

but even its executable, it can not run. Because shell doesn’t know what interpreter to use to run it. From shell you have to run it by,

$ php songinfo-broadcaster.php

Amarok wont call it that way for sure.  If it would call it that way it wouldn’t require the script to be executable. It will be call like,

$ ./songinfo-broadcaster.php

To make sure that this script runs this way add the following line at the top of the script.

The script will look like,

#!/usr/bin/php
<?php

?>

Now I have to code in between the php tags.

Here goes my main script skeleton. The dowork() function does the main script logic.

#!/usr/bin/php
<?php
echo "starting script", PHP_EOL;
function dowork(){
}
echo "starting listenning events", PHP_EOL;
$fp = fopen("php://stdin","r");
while($command = fgets($fp)):
echo "Event: $command",PHP_EOL;
$com = trim($command);
echo "event to procsss: $com", PHP_EOL;
switch($com):
case 'trackChange':
dowork();
break;
endswitch;
endwhile;
fclose($fp);
echo "Script Exited", PHP_EOL;
?>

My script should execute the main logic only when the  trackChange event occurred. So I had to catch it. Amarok sends event information in the scripts STDIN. So I read the STDIN. And when its trackChange I call the dowork(). Very simple logic.

I also needed current song information. It can be collected by DCOP api. DCOP is desktop communication protocol. While running amarok issue the following command in consoe,

$ dcop amarok player title

You’ll see the current songs title.

This technique is used to get the current song information. My dowork function is,

function dowork(){
$length = `dcop amarok player trackTotalTime`;
echo "Length: $length",PHP_EOL;
$album =`dcop amarok player album`;
echo "Album: $album",PHP_EOL;
$title =`dcop amarok player title`;
echo "Title: $title",PHP_EOL;
$artist = `dcop amarok player artist`;
echo "Artist: $artist",PHP_EOL;
$sec = $length % 60;
$min = (int)(($length-$sec)/60);
$length = "$min:$sec";

// Code to broadcast length, artist, album and title
}

See, how did I collect those information. I just used the backtick operator in php to capture shell command output.

Main script is done. Now the spec file needs to be writen.
I just put the following line in songinfo-broadcaster.spec file.

type = generic

After that I wrote some notes about the script in README. Then a copyright notice  in COPYING.

When every thing is written. Its time to package it. My directory structure was,


songinfo-broadcaster/
songinfo-broadcaster/README
songinfo-broadcaster/COPYING
songinfo-broadcaster/songinfo-broadcaster.php
songinfo-broadcaster/songinfo-broadcaster.spec

I pack it by following command.

$ tar -cf songinfo-broadcaster.amarokscript.tar -p songinfo-broadcaster

One this I MUST say. Every amarok script archive must be a .amarokscript fie. That means when they are in tar archive it will be .amarokscriopt.tar extension and when they are in bz2 archive it will be .amarokscript.tar.bz2.

The script is created.

Now
1)Open amarok
2)Go to Tools->ScriptManger.
3)Click Install Script button
4)Browse the script and load it
5)Select the script and click RUN

The script will be running.

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to StumbleUpon

Domain redirection to localhost

Thursday, March 20th, 2008

What is this: When you enter http://www.yahoo.com in your address bar of browser, It wont go to http://www.yahoo.com rather it will show a custom page which is inside you pc not in Internet.

Why is this: Sometimes you may need to restrict your children from accessing site that contains adult content, violent pictures etc. Also web service developer may use it to simulate and debug web service host.

Tools: You need actually nothing. Only skill of editing files. A text editor will do. In Windows machine you can use notepad.exe. For Linux machine you can use vi, ed, ee etc.

How: There is a special file called `hosts` which contain about domain and ip address mapping information. The format is plain text. In Windows the file in “%SystemRoot%\System32\Drivers\Etc” directory and in Linux its in “/etc” directory. Here is a sample file content for my Windows XP Service pack 2.


# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host

127.0.0.1 localhost

Its almost same for Linux. This file is responsible to redirect any request to “localhost” domain to 127.0.0.1 IP. Yes, thats why you get ping response from 127.0.0.1 when you ping “localhost”.

Now open the file and add some alias to “localhost” entry. Like the following,

127.0.0.1 localhost badweb.com www.badweb2.net

When you are done saving the file. Ping “badweb.com”. You’ll get response from 127.0.0.1. Now whenever someone enters http://www.badweb2.net in any browser it will show the content of http://127.0.0.1/. If you don’t have any web server installed it will show a “page not found” page. And it serves our purpose as well. The surfer of badweb.com will always see the web is down. ;)

Disadvantage: The main problem is if you put “badweb.com” as alias it wont work for any sub domain of “badweb.com”. For example, If you put,

127.0.0.1 localhost badweb.com

Entering http://www.badweb.com/ wont redirect to http://127.0.0.1 also http://anysubdomain.badweb.com wont work too. To work around you have to put series of sub domains. Like this,

127.0.0.1 localhost badweb.com www.badweb.com anysubdomain.badweb.com

For this the entry will be too long. :( :(

Some addition: You can enhance it more by the following methods

  1. Installing a web server.
  2. Creating alias IP for your network device

Installing a web server will make you show a customized page. You can put a message there too. Like “Access denied”. So unauthorized user will see your message every time they visit their page. Also you can put per domain customized page with a little knowledge of Javascript or PHP or URL rewrite.

If you can create alias of your own IP pinging the domain will get response from different IP and all of the IP are alias. Its just same web but IPs are different. if you use 176.16.16.[1-254] as you alias you can put some entry in hosts file like the follwing


127.0.0.1 localhost
176.16.16.1 badweb.com
176.16.16.4 www.badweb.com
176.16.16.2 badweb.net
176.16.16.3 verybadweb.com

Now you’ll get response from different IPs. It’ll be hard for visitor to track the whole thing.

Thats all.

Keep bluffing. ;)



© 2008 by A K M Mokaddim

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Ping.fm Post to StumbleUpon