SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2014-12-21 13:58:02

ohnonot
...again
Registered: 2012-05-22
Posts: 2,205

conky/bash/MOC music on console track history with artwork downloading

the idea:
when listening to online radio, i get track titles, but sometimes they change to the next track too early (esp. soma.fm) or i'm just late looking at my desktop to see what the name of that beautiful song was...
so, some basic track history would be good.
==============================================
after some hacking i added a script that searches last.fm for lp covers from that artist.
i found last.fm better than discogs.com or coverart.org:

  • the url syntax is very forgiving

  • the html offers a simple way to access the cover of the most succesful album. this is not always the cover of the song being played, but it's better than accidentally getting press photographs or some such.

  • there's not so many false positives as with the other 2 mentioned. actually none so far, neither have i had to revert to the default.png

i made 1 script out of 2 (mochist & artart) so that conky has to call only one.
so without further ado here's the screenshot:
mocarthist.png
it's very functional. i'm not so good with making things look good, but maybe you are.

here's the bash script (i saved it as $HOME/bin/mochist):

#!/bin/bash

# 2 scripts to be used with conky - 1: keep a history of played tracks
# and 2: search online for artist artwork, download and display with conky

### PART 1: mochist ###########

histfile="$HOME/.moc/mochist"
current=$(mocp -Q '%title')
previous=$(sed -n 2p "$histfile")
# how many songs do you want in history?
histsize=50

echo -e "$(date)\n========================="
echo -e "this is the current title:\n$current"
echo -e "this is the previous title:\n$previous"
echo -n "they are "

if [ "$current" != "$previous" ]
# here is the action!
then
	# save the internal field seperator...
	SAVEIFS=$IFS
	# and change it to newline only.
	IFS=$(echo -en "\n")
	# (i think there was a simpler way to express this)
	echo -n "not "
	history="$(head -$(((histsize-1) * 2)) $histfile)"
	echo -e "$(date +%R)\n$current\n$history" > "$histfile"
	# ...and restore it again. though i think it's not necessary.
	IFS=$SAVEIFS
fi
echo "equal!"

echo "======================="

### PART 2: artart ##################

done=0
artdir="$HOME/Pictures/artdir"
# this is where the script slowly builds up an artist-centric 
# (so not by albums) database of artwork.
conkydir="$HOME/.config/conky"

# let's first check around if we even need to fetch anything.
query="$(dirname "$(mocp -Q %file)")"
if [ ! $(echo $query | grep 'http:') ]
	then
	echo "local search first..."
	for imgfile in cover front album
	do
		for ext in jpg png gif
		do
			echo "querying $query/$imgfile.$ext"
			if [ -f "$query/$imgfile.$ext" ]
			then 
				query="$query/$imgfile.$ext"
				echo "$query already exists. done."
				done=1
				break
			fi
		done
	done
fi

if [ "$done" == "0" ]
then
	query="$(mocp -Q %artist)"
	
	if [ "x$query" == "x" ]
	then 
		echo "artist is empty - using %title instead"
		query="$(echo $current | sed 's/ - .*$//')"
	#!!!!!!!! REUSING A VARIABLE FROM mochist HERE
	fi
	
	artdir="$artdir/$query"
	
	# Skip already processed ones
	 
	for ext in jpg png gif
	do
		echo "searching for cover.$ext in $artdir"
		if [ -f "$artdir/cover.$ext" ]
		then 
			query="$artdir/cover.$ext"
			echo "$query already exists. done."
			done=1
			break
		fi
	done
	
	if [ "$done" == "0" ]
	then
		echo "Searching for: [$query]"
		url="http://www.last.fm/music/$query"
		echo "Searching ... [$url]"
		
		coverurl="$(wget -qO - "$url" | xmllint --html --xpath '//section[@class="artist-top-albums"]/ul/li[1]/section/a/div/img/@src' - 2> /dev/null | cut -d '"' -f 2)"
		
		if [ "x$coverurl" == "x" ]
		then
			query="$conkydir/default.png"
			echo "no image url found. using default.png."
		else
			echo -e "found $coverurl\nnow wget it..."
			#
			# for albumart.org:
			#~ url="http://www.albumart.org/index.php?searchk=$encoded&searchindex=Music"
			#~ coverurl="$(wget -qO - "$url" | hxextract img.image_border - |grep -o -m 1 -E 'http://.*.jpg'|cut -d '"' -f 1)"
			#~ coverurl=`wget -qO - $url | xmllint --html --xpath  'string(//a[@title="View larger image" and starts-with(@href, "http://ecx.images-amazon")]/@href)' - 2>/dev/null`
			
			mkdir -p "$artdir"
			ext=${coverurl##*.}
			query="$artdir/cover.$ext"
			echo -e "extension: $ext\nfinal output: $query"
			wget "$coverurl" -qO "$query"
		fi
	fi
fi

ln -f -s "$query" "$conkydir/coverart.jpg"

and here's a complete conky:

#<conkyrc> 
background yes
update_interval 2
override_utf8_locale yes
short_units yes
double_buffer yes
no_buffers yes
own_window_hints undecorated,sticky,skip_taskbar,skip_pager,below
own_window yes
own_window_transparent yes
border_inner_margin 0
border_outer_margin 0
minimum_size 160 60
maximum_width 160
alignment bl
gap_x 10
gap_y 34
draw_outline no
draw_shades no
border_width 10
draw_borders no
draw_graph_borders no
default_color 332C1A
use_xft yes
xftfont bitstream vera sans mono:style=bold:size=12
format_human_readable true
use_spacer none

TEXT
${if_running mocp}${if_match "${exec mocp -Q %state}" != "STOP"}#
${image ~/.config/conky/coverart.jpg -p 0,0 -s 165x160 -n}#
${execi 10 ~/bin/mochist > debug 2>&1 }#
${voffset 165}${exec sed -n 2p ~/.moc/mochist | fold -sw 16}
${voffset -6}$hr${font bitstream vera sans mono:size=8}
(prev) ${exec sed -n 3,4p ~/.moc/mochist | fold -sw 22}$endif$endif

("fold -sw nn" inserts newlines after nn characters, keeping words intact. that's the value you're most likely want to adjust)
the default.png (a little like #! forum's default avatar) can be found here

i've tested this with soma.fm and local music.
i guess last.fm cannot cover all usage scenarios for artwork (classical music?).
anyhow, this will slowly build up a database of images, but only by artists, not albums.
for local music, it will always look for local images (cover, front, album) first.

edit 22.12.: corrected a glitch in the conky

Last edited by ohnonot (2014-12-22 10:20:13)

Offline

Be excellent to each other!

#2 2014-12-21 14:05:00

pvsage
Internal Affairs
From: North Carolina
Registered: 2009-10-18
Posts: 13,970

Re: conky/bash/MOC music on console track history with artwork downloading

Conky's native moc functions have been a mess for as long as I can remember.  Always cause 100% CPU usage, glitchy response (and not in a good techno way either), yada.

This requires an OnSongChange in ./moc/config, yes?

Offline

#3 2014-12-21 16:41:42

PackRat
#! Die Hard
From: USA
Registered: 2011-03-03
Posts: 1,572

Re: conky/bash/MOC music on console track history with artwork downloading

There is/was an old thread on this forum by vastone that has HowTo's for setting up music players in conky; pretty sure moc is one of them. Also, I think chameleon's dwmX spin uses moc+conky so the scripts may be available on his site.


"It does not require many words to speak the truth." - Chief Joseph, Nez Perce tribe

Offline

#4 2014-12-22 01:37:36

ohnonot
...again
Registered: 2012-05-22
Posts: 2,205

Re: conky/bash/MOC music on console track history with artwork downloading

pvsage wrote:

This requires an OnSongChange in ./moc/config, yes?

no, the script is executed from the conky.
it's all because of online radio; OnSongChange doesn't work with that.

anyway i have expanded the script quite a bit, see update/edit in post #1.

@packrat: yeah i know, conky for moc is nothing new. i just felt like implementing history myself. and now the online artwork...

Last edited by ohnonot (2014-12-22 02:06:05)

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: acrobat

Debian Logo