diff options
Diffstat (limited to 'scripts')
-rwxr-xr-x | scripts/mgLyric.py | 52 | ||||
-rwxr-xr-x | scripts/muggle_getlyrics | 118 |
2 files changed, 114 insertions, 56 deletions
diff --git a/scripts/mgLyric.py b/scripts/mgLyric.py new file mode 100755 index 0000000..97042f5 --- /dev/null +++ b/scripts/mgLyric.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python +import os, sys, locale + +title = sys.argv[1] +artist = sys.argv[2] +outdir = sys.argv[3] + +# add other possible paths here: +googlFound = False +for scriptdir in ('/usr/share/apps/amarok/scripts/Googlyrics2', \ + 'NULL'): + if os.path.isdir(scriptdir+'/sites/'): + sys.path.append(scriptdir + "/lib/") + sys.path.append(scriptdir + "/sites/") + os.chdir(scriptdir) + googlFound = True + break + +if not googlFound: + outfile = open(outdir + '/1.raw',"w") + outfile.write("Googlyrics2 is not installed\nSee http://quicode.com/googlyircs2") + outfile.close + sys.exit(0) + +Debugging = False + +if Debugging: + outlyric=["Version 1","Version 2","Version 3"] + for idx,item in enumerate(outlyric): + outfile = open(outdir + '/' + str(idx) + '.raw',"w") + outfile.write(item) + outfile.close + sys.exit(0) + +from Googlyrics import * +g = Googlyrics() + +outlyric = g.find_lyrics(title, artist) +if len(outlyric) > 0: + for idx,item in enumerate(outlyric): + l = item.getLyric() + if l is not None: + if l.lyrics is not None: + if len(l.lyrics)>2: +# if we pipe or write output to a file, python by default recodes into ascii, +# and sys.stdout.encoding is also set to ascii. But if the system +# default locale is for example utf-8, we also want the file to be +# encoded like that + outfile = open(outdir + '/' + str(idx) + '.raw',"w") + outfile.write(l.lyrics.encode(locale.getdefaultlocale()[1])) + outfile.close + diff --git a/scripts/muggle_getlyrics b/scripts/muggle_getlyrics index 190e413..db7fbdf 100755 --- a/scripts/muggle_getlyrics +++ b/scripts/muggle_getlyrics @@ -1,66 +1,72 @@ #!/bin/sh -export GOOGLYRICS=/usr/share/apps/amarok/scripts/googlyrics/googlyrics +# usage: +# artist title outfile +# +# we might find several different lyrics for a song. This is handled +# by this script: We use a subdirectory outfile.new. If it exists +# and contains a file, we just return that file and remove it from +# outfile.new. If outfile.new is empty, we read all versions again +# with googlyrics. -rm -f "$3" -txtfound=0 -artist=`echo $1 | sed 's/ /%20/'g` -title=`echo $2 | sed 's/ /%20/'g` +# ultimately I want to integrate this shell script into mgLyrics.py -if test ! -x $GOOGLYRICS -then - echo $GOOGLYRICS nicht gefunden > $3 - exit 2 -fi +artist="$1" +title="$2" +outfile="$3" +orgoutfile=`echo "$3" | sed 's/.tmp$//'` +outdir="$3.new" -export success=0 -echo fetchLyrics $artist $title | $GOOGLYRICS 2>&1 | - sed 's/\x0d//g' | - sed 's/\xc2\xb4/\x27/g' | - sed 's/\xc3\x82\x27/\x27/g' | - sed 's/\xc3\x82/\x27/g' | - sed 's/\xc3\xb9/\x27/g' | - sed 's/\xe2\x80\x99/\x27/g' | - grep -ive 'NEW.*ringtones' | - grep -v '--------------' | - recode HTML..utf8 | - sed 's/\xc2\x91/\x27/g' | # in unicode, those two are reserved for - sed 's/\xc2\x92/\x27/g' | # private use, but still some sites use them... -while read line -do -# did we find a text yet? - if expr "$line" : '<br>Regex success' >/dev/null 2>&1 - then - success=1 - continue - fi -# googlyrics tries to send the finished text to amarok: - if expr "$line" : 'object not accessible' >/dev/null 2>&1 - then - killall googlyrics >/dev/null 2>&1 - break - fi -# googlyrics starts main loop again: - if expr "$line" : '.*scalar chomp.*STDIN' >/dev/null 2>&1 +echo > "$outfile".loading + +if test ! -d "$outdir" +then + mkdir "$outdir" + MGLYRICS="`dirname $0`/mgLyric.py" + if test ! -x "$MGLYRICS" then - killall googlyrics >/dev/null 2>&1 - break + echo $MGLYRICS not found > "$outdir"/1.raw + else + "$MGLYRICS" "$title" "$artist" "$outdir" fi - test $success = 0 && continue -# suppress other googlyrics error messages - expr "$line" : '.*'$GOOGLYRICS >/dev/null 2>&1 && continue - notempty=0 - test x"$line" = x || notempty=1; - test $notempty -eq 1 && txtfound=1; - test $txtfound -eq 1 && echo $line -done > "$3".loading 2>/dev/null -# use .loading because the file is already there when googlyrics starts -# but muggle thinks we are done as soon as $3 exists -if test -s "$3".loading + for i in "$outdir"/*.raw + do + test -r "$i" || break + cat "$i" | + sed 's/\x0d//g' | + sed 's/\xc2\xb4/\x27/g' | + sed 's/\xc3\x82\x27/\x27/g' | + sed 's/\xc3\x82/\x27/g' | + sed 's/\xc3\xb9/\x27/g' | + sed 's/\xe2\x80\x99/\x27/g' | + grep -ive 'NEW.*ringtones' | + grep -v -e '--------------' | + recode HTML..utf8 | + sed 's/\xc2\x91/\x27/g' | # in unicode, those two are reserved for + sed 's/\xc2\x92/\x27/g' | # private use, but still some sites use them... + cat > "$i.converted" 2>/dev/null + rm -f "$i" + echo cmp -s "$i.converted" "$orgoutfile">>/tmp/log.wr + cmp -s "$i.converted" "$orgoutfile" + if test $? -eq 0 + then + echo rm -f "$i.converted" >>/tmp/log.wr + rm -f "$i.converted" + continue + fi + done +fi + +rmdir "$outdir" 2>/dev/null +if test -d "$outdir" then - mv "$3".loading "$3" -else - rm -f "$3".loading + ls -l "$outdir" + ls "$outdir"/*.converted | head -1 | while read fn + do + test -r "$fn" || break + mv "$fn" "$outfile" + done + rmdir "$outdir" 2>/dev/null fi -test -s "$3" # we want the exit code +rm -f "$outfile".loading |