"""
by Oskar Flordal
### LICENSE ########################################################
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
###################################################################
"""
__author__ = 'Oskar Flordal'
__version__ = '0.1'
print "tidigt"
#
# standard import for varius stuff
#
import karamba,random
import re, time, urllib, os
import xmms.control #pyXMMS
#
# variables & constants
#
class Var:
"""
Helper class for keeping runtime variables
"""
def __init__(self, **kws):
self.__dict__ = kws
VAR = Var(shown=False, utime=None,
bib={}, lyric=[], artist='', availFortunes =[], selectedFortunes=[], source='')
try:
import xmmslyr
VAR.bib = xmmslyr.dict
except Exception,e:
print "no cached lyrics found"
#selectedFortunes is an aray with numbers to be used i availFortunes
# [0 3] would use 0 and three for easy randomization
CHAR_H = 15
LYRICMAXLENGTH = "400"
WIDGETWIDTH = 400
LINEHEIGHT = 14
LYRIC_BG_X, LYRIC_BG_Y = (0,15)
LYRIC_COLOR = (5, 64, 78)
print "tidigt2"
def find_song():
[artist,tune] = xmms.get_playlist_title(xmms.get_playlist_pos()).split('-')
#find artist and tune by looking at the current playing song and splitting by a - bar which ois hopefully configuered
#fix whitespace
artist = artist.rstrip()
artistsearch = artist.replace('and','%26')
tune = tune.lstrip()
#check if it is cached
try:
print "looking up: " + artist + tune
return (VAR.bib[artist+tune],artist + " - " +tune)
except Exception,e:
print "Not in dict"
# We+Rule+the+School&submit=search&category=song
BASEURL = "http://search.sing365.com/search.php?searchstr="
# ARTIST = "artist=" + artist.replace(' ','+')
# ALBUM = "&albummode=0&album=&songmode=0&"
SONG = tune.replace(' ','+') +"&submit=search&category=song"
# print BASEURL + SONG
try:
txt = urllib.urlopen(BASEURL + SONG).readlines()
except Exception,e :
print "Bad url probably some error with the tune name"
return (['not found'],artist + " - " +tune)
# print artist.replace(' ','%20').lower()
nr = 0
for x in range(0,len(txt)):
if (txt[x].lower().find(artist.replace(' ','%20').lower())!=-1):
nr = x
break
# print nr #185
temp = txt[nr].split('click.php?')
# print temp
nr = 0
for x in range(0,len(temp)):
if (temp[x].lower().find(artistsearch.replace(' ','%20').lower())!=-1):
nr = x
break
if (nr == 0):
print "Song not found"
return (['not found'],artist + " - " +tune)
print nr
# print 'http://search.sing365.com/click.php?'+temp[2].split('>',1)[0]
try:
txt = urllib.urlopen('http://search.sing365.com/click.php?'+temp[nr+1].split('>',1)[0]).readlines()
except Exception,e :
print "Bad url probably some error with the tune name"
return (['not found'],artist + " - " +tune)
# print txt
nr = 0
for x in range(0,len(txt)):
if (txt[x].find("Print the Lyrics")!=-1): #best row...
nr = x
break
for x in range(nr,len(txt)):
if (txt[x].find("pagead2.googlesyndication.com")!=-1):
#...if there are no google ads below
nr = x
break
nr2 = 0
for x in range(nr,len(txt)):
if (txt[x].find("color=#cccccc")!=-1):
nr2 = x
break
# print nr #132
lyric = txt[nr+2:nr2-1]
for row in range(len(lyric)):
lyric[row] = lyric[row].replace('
','')
lyric[row] = lyric[row].replace('\n','')
lyric[row] = lyric[row].replace('\r','')
# lyric[row] = lyric[row].replace(''','\'')
# lyric[row] = lyric[row].replace('"','\"')
lyric[row] = lyric[row].lstrip()
# print lyric[row]
print "saving: " +artist+tune
VAR.bib[artist+tune] = lyric
print "KLJKLJKLJKLJKL"
file = open("/home/oskar/karamba/xmmslyrics/xmmslyr.py","w")
print "file opened"
print file.write("""#Don't touch if you don't know what you are doing
dict=%s""" % (VAR.bib))
file.close()
print "and closed"
return [lyric,artist +" - " +tune]
def find_song2():
print "ifnd song"
[artist,tune] = xmms.get_playlist_title(xmms.get_playlist_pos()).split('-')
#find artist and tune by looking at the current playing song and splitting by a - bar which ois hopefully configuered
#fix whitespace
artist = artist.rstrip()
tune = tune.lstrip()
BASEURL = "http://www.leoslyrics.com/advanced.php?artistmode=0&"
ARTIST = "artist=" + artist.replace(' ','+')
ALBUM = "&albummode=0&album=&songmode=0&"
SONG = "song=" + tune.replace(' ','+')
EXTRA = "++&lyrics=&mode=0"
"""
BASEURL = "http://www.leoslyrics.com/advanced.php?artistmode=0&"
ARTIST = "artist=death+cab+for+cutie"
ALBUM = "&albummode=0&album=&songmode=0&"
SONG = "song=Amputations"
EXTRA = "++&lyrics=&mode=0"
"""
print BASEURL + ARTIST + ALBUM +SONG +EXTRA
url = urllib.urlopen(BASEURL + ARTIST + ALBUM +SONG +EXTRA)
txt = url.readlines()
url.close()
nr = 0
for x in range(0,len(txt)):
if (txt[x].find("listlyrics.php")!=-1):
nr = x
# print x
try:
url = urllib.urlopen("http://www.leoslyrics.com/listlyrics.php?hid=" + txt[nr].split("=")[3].split("\"")[0])
except Exception,e :
print "Song not found :("
return (['not found'],artist +" - "+tune)
# sys.exit()
lyricraw = url.readlines()
nr = 472
#for x in range(0,len(lyricraw)):
# if (lyricraw[x].find("wanted")!=-1):
# nr = x
#print nr
x = ""
lyric = []
while (x.find("")==-1):
lyric.append(x)
x = lyricraw[nr]
nr = nr+1
#print lyric[x]
for row in range(len(lyric)):
lyric[row] = lyric[row].replace('
','')
lyric[row] = lyric[row].replace('\n','')
lyric[row] = lyric[row].replace('\r','')
lyric[row] = lyric[row].replace(''','\'')
lyric[row] = lyric[row].replace('"','\"')
lyric[row] = lyric[row].lstrip()
print lyric[row]
print "saving: " +artist+tune
VAR.bib[artist+tune] = lyric
#save to file
file = open("cachedlyrics.py","w")
file.write("""#Don't touch if you don't know what you are doing
dict=%s""" % (VAR.bib))
file.close()
return [lyric,artist +" - " +tune]
print "tidigt3"
#This gets called when an item is clicked in the theme CONFIGURATION menu,
#not the popup menus that you create.
# key = the reference to the configuration key that was changed
# value = the new value (true or false) that was selected
#def menuOptionChanged(widget, key, value):
# if (key == "all"):
# karamba.setMenuConfigOption(widget, "all", "0")
# print "all"
# for x in VAR.availFortunes :
# karamba.setMenuConfigOption(widget, x, "1")
# karamba.readMenuConfigOption(widget, "all")
# VAR.selectFortunes = range(0,len(VAR.availFortunes))
# print "in"
# else:
# configchange = 1
# werethreading = 1
#def initConfMenu(widget):
# karamba.setMenuConfigOption(widget, x, "0")
#
# Karamba functions
#
def initWidget(widget):
print "init"
# initConfMenu(widget)
_update(widget)
# print VAR.availFortunes
print "init2"
# karamba.setMenuConfigOption(widget, "all", "0")
karamba.attachClickArea(widget, karamba.getThemeText(widget,'BACKGROUND'))
print "init2"
widgetUpdated(widget)
def getSong():
VAR.lyric, VAR.artist = find_song()
VAR.source = 'sing365'
if (VAR.lyric==['not found']):
VAR.lyric, VAR.artist = find_song2()
VAR.source = 'leo'
def widgetUpdated(widget):
if VAR.utime is None or time.time()-VAR.utime > 80000:
VAR.utime = time.time()
getSong()
_update(widget)
print "tidigt4"
def meterClicked(widget, meter, button):
print "meter"
if VAR.lyric:
if VAR.shown:
_hide_lyric(widget)
_update(widget)
getSong()
_update(widget)
else:
# _update(widget)
_show_lyric(widget)
karamba.redrawWidget(widget)
VAR.shown = not VAR.shown
#
# protected methods
#
def _update(widget):
karamba.changeText(widget,
karamba.getThemeText(widget, 'ARTIST'),
VAR.artist)
karamba.redrawWidget(widget)
def _show_lyric(widget):
bgHeight = CHAR_H*1+4
bgW = karamba.createImage(widget, LYRIC_BG_X, LYRIC_BG_Y+4,
'images/details_bg.png')
# for i in range(5, bgHeight, 8):
# karamba.resizeImage(widget, bgW, 400, i)
# karamba.redrawWidget(widget)
nrlines = len(VAR.lyric)
karamba.resizeWidget(widget, WIDGETWIDTH, 20+nrlines*LINEHEIGHT)
karamba.resizeImage(widget, bgW, WIDGETWIDTH,5+nrlines*LINEHEIGHT )
karamba.redrawWidget(widget)
lyricW = karamba.createText(widget, LYRIC_BG_X+3, LYRIC_BG_Y+4,
WIDGETWIDTH, 20+nrlines*LINEHEIGHT, '\n'.join(VAR.lyric))
karamba.changeTextSize(widget, lyricW, 11)
karamba.changeTextColor(widget, lyricW, *LYRIC_COLOR)
VAR.lyricW = lyricW
VAR.bgW = bgW
def _hide_lyric(widget):
karamba.hideImage(widget, VAR.bgW)
karamba.hideText(widget, VAR.lyricW)
karamba.deleteImage(widget, VAR.bgW)
karamba.deleteText(widget, VAR.lyricW)
VAR.lyricW = VAR.bgW = None
print "tidigt6"