#! /usr/bin/env python

__module_name__ = "amarok display"
__module_version__ = "1.0"
__module_description__ = "amarok display"

import xchat
from os import popen
from random import randint
import string
from urllib import unquote

def show_song(word, word_eol, userdata):

########################## CONFIGURATION ######################
#
#Written by Moobert and Eno_
#
#
#
##You can your own xchat colours in the song display by editing the below:
#
#
	info_name = 9
	first_bracket = 5
	second_bracket = 7
	song_info = 8

#
#then type;
#/disork 
#	to show the song information.
#/disrok help
#	to get a list of other commands.
#
#problems/ideas:
#	Drop one of us a line on server irc.odinnet.ca, in #oldskool
#
#Extra fun stuff added by Koi
# irc.necron.org
#
##############################################################
	
	songdata = ''
	arun = 0

	def zeroadd(info):
		if len(str(info)) < 2:
			return '0'+str(info)
		else:
			return ''+str(info)
	
	c = [zeroadd(info_name), zeroadd(first_bracket), zeroadd(second_bracket), zeroadd(song_info)]
	
	def getinfod(info):
		return popen("dcop amarok default "+info,"r").read().strip()

	for line in popen("dcop 2> /dev/null","r").readlines():
		if 'amarok' in line:
			arun = 1
			if int(getinfod('status')) == 2:
				arun = 2

	def getVol():
		getVolume = int(getinfod('getVolume'))
		if (getVolume <= 10):
			return "Super Low"
		elif (getVolume > 10 and getVolume <= 20):
			return "Ambiant"
		elif (getVolume > 20 and getVolume <= 30):
			return "Quiet"
		elif (getVolume > 30 and getVolume <= 40):
			return "Soft"
		elif (getVolume > 40 and getVolume <= 50):
			return "Noisey"
		elif (getVolume > 50 and getVolume <= 60):
			return "Noiser"
		elif (getVolume > 60 and getVolume <= 70):
			return "Sorta Loud"
		elif (getVolume > 70 and getVolume <= 80):
			return "Loud"
		elif (getVolume > 80 and getVolume <= 90):
			return "Rocking"
		elif (getVolume > 90 and getVolume <= 95):
			return "Rocking Out"
		elif (getVolume > 95):
			return "Turned all the way to 11"

	def randn():
		return zeroadd(randint(0, 15))

	def getRating():
		score = int(getinfod('score'))
		if (int(getinfod('trackPlayCounter')) == 0):
			return "Not Applicable"
		if (score <= 20):
			return "Abominable"
		elif (score > 20 and score <= 30):
			return "Bad"
		elif (score > 30 and score <= 40):
			return "Low"
		elif (score > 40 and score <= 50):
			return "Below Average"
		elif (score > 50 and score <= 60):
			return "Average"
		elif (score > 60 and score <= 80):
			return "Good"
		elif (score > 80 and score <= 90):
			return "Excellent"
		elif (score > 90):
			return "Get naked and dance"

	def getQuality():
		sample = str(int(getinfod('sampleRate'))/1000)
		bitrate = getinfod('bitrate').strip(string.letters + ' ')
		return bitrate + '/' + sample

	def getPlayCounter():
		if (int(getinfod('trackPlayCounter')) == 0):
			return "Never before"
		elif (int(getinfod('trackPlayCounter')) == 1):
			return "Once before"
		else:
			return getinfod('trackPlayCounter') + " times before"

	if arun > 0:
		if len(word) > 1:
			if word[1] == 'rand':
				c = [randn(), randn(), randn(), randn()]
			elif word[1] == 'vol':
				getinfod('setVolume '+word[2])
				print 'Amarok\'s volume set to ' + word[2] + '%'
				arun = -1
			elif word[1] == 'stop':
				getinfod('stop')
				arun = -1
			elif word[1] == 'play':
				getinfod('play')
				arun = -1
			elif word[1] == 'prev':
				getinfod('prev')
				arun = -1
			elif word[1] == 'next':
				getinfod('next')
				arun = -1
			elif word[1] == 'send':
				file = unquote(getinfod('encodedURL')).replace('file:', '')
				songdata = 'dcc send ' + word[2] + ' "' + file + '"'
				arun = -1
			else:
				print """
				Valid commands are:
				/disrok rand         Display amarok's currently playing song using random colors
				/disrok stop         Stop amarok
				/disrok play         Resume amarok
				/disrok prev         Play previous song in amarok playlist
				/disrok next         Play next song in amarok playlist
				/disrok vol <0-100>  Set audio volume in amarok
				/disrok send <nick>  Send current playing song to nick
				/disrok              Display amarok's currently playing song
				"""
				arun = -1

	if arun > 1:
		isplaying = 'me ' + c[0] + 'is playing:' + c[1] + '[' + c[2] + '[' + c[3] + '"' + getinfod('title') + '"' + c[0] +' by ' + c[3] + '"' + getinfod('artist') + '"' + c[0] + ' from the album ' + c[3] + '"' + getinfod('album') + '"' + c[2] + ']' + c[1] + ']'
		quality =  c[0] + 'Quality:' + c[1] + '[' + c[2] + '[' + c[3] + getQuality() + c[2] + ']' + c[1] + ']'
		position =  c[0] + 'Position:' + c[1] + '[' + c[2] + '[' + c[3]  + getinfod('currentTime')+' of '+getinfod('totalTime') + c[2] + ']' + c[1] + ']'
		volume =  c[0] + 'Volume:' + c[1] + '[' + c[2] + '[' + c[3]  + getVol() + c[2] + ']' + c[1] + ']'
		played = c[0] + 'Played:' + c[1] + '[' + c[2] + '[' + c[3]  + getPlayCounter() + c[2] + ']' + c[1] + ']'
		score = c[0] + 'Rating:' + c[1] + '[' + c[2] + '[' + c[3] + getRating() + c[2] + ']' + c[1] + ']'
		songdata = isplaying + ' ' + quality + ' ' + position + ' ' + volume + ' ' + played + ' ' + score
	else:
		if arun == 1:
			print "amarok doesn't seem to be playing..."
		elif arun == 0:
			print 'load amarok, or check that dcop is running.'

	xchat.command(songdata)
	return xchat.EAT_ALL

xchat.hook_command("disrok", show_song)
print 'Display amarok loaded, type "/disrok help" for a command list'
