Jump to content

last.fm client


JRSmile
 Share

Recommended Posts

i had enough from thelastripper, or lastsharp to download songs from last.fm

i wrote some funcs to get free mp3s from last.fm..

will enhance it to a full client but for now it will download 5 songs of the metal global tag of last.fm if you enter a last.fm account and hit F5 :-)

you will need md5.au3 from the machine code thread.

#include <inet.au3>
#include "md5.au3"
#include <date.au3>
#include <string.au3>
#include <array.au3>
;#cs
$handle = __lastfm_login("<insert-username-here>", "<insert-password-here>")
if __lastfm_select_channel($handle) Then
    $result = __lastfm_get_playlist($handle)
    ConsoleWrite($result & @CRLF)
    $res = _StringBetween($result,"<location>", "</location>")
    $res2 = _StringBetween($result,"<title>", "</title>")
    $res3 = _StringBetween($result,"<creator>", "</creator>")
    _ArrayDisplay($res)
    for $i = 0 to UBound($res) -1
        InetGet($res[$i], $res3[$i+1] & " - "& $res2[$i+1] & ".mp3",1,1)
        While @InetGetActive
          ToolTip("Bytes = " & @InetGetBytesRead)
        Wend 
    next
EndIf

;#ce

Func __lastfm_login($username, $pass)
    Local $ret[3]
    Local $md5ed_pass = StringLower(StringTrimLeft(_md5($pass), 2))
    $result = _INetGetSource('http://ws.audioscrobbler.com/radio/handshake.php?version=1.5.2.38918&platform=win32&username='&$username&'&passwordmd5='&$md5ed_pass&'&debug=0&partner=')
    $tmp = _StringBetween($result, 'session=', @LF)
    if not @error Then $ret[0] = $tmp[0]
    $tmp = _StringBetween($result, 'base_url=', @LF)
    if not @error Then $ret[1] = $tmp[0]
    $tmp = _StringBetween($result, 'base_path=', @LF)
    if not @error Then $ret[2] = $tmp[0]
    __lastfm_login2($username, $pass)
    Return $ret
EndFunc   ;==>__lastfm_login

Func __lastfm_login2($username, $pass)
    $md5ed_pass = StringLower(StringTrimLeft(_md5($pass), 2))
    $timestamp = _DateDiff( 's',"1970/01/01 00:00:00",_NowCalc())
    $salted_pass = StringLower(StringTrimLeft(_md5($md5ed_pass & $timestamp), 2))
    $result = _INetGetSource('http://post.audioscrobbler.com/?hs=true&p=1.2&c=ass&v=1.5.2.38918&u='&$username&'&t='&$timestamp&'&a=' & $salted_pass)
    Return $result
EndFunc

Func __lastfm_select_channel($handle,$channel = "lastfm%3A%2F%2Fglobaltags%2Fmetal", $lang = "de")
    $result = _INetGetSource('http://' & $handle[1] & $handle[2] & '/adjust.php?session='&$handle[0]&'&url='&$channel&'&lang=' & $lang)
    if StringInStr($result, "response=OK") then Return True
    Return False
EndFunc

Func __lastfm_get_playlist($handle)
    $result = _INetGetSource('http://' & $handle[1] & $handle[2] & '/xspf.php?sk='&$handle[0]&'&discovery=0&desktop=1.5.2.38918')
    Return $result
EndFunc
Edited by JRSmile
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

here my much less dirty version, even download size calculation works (nearly)

features added:

*duration calculation

*album cover download

*band cover download

*folder structure creation

#include <inet.au3>
#include "md5.au3"
#include "_XMLDomWrapper.au3"
#include <date.au3>
#include <string.au3>
#include <array.au3>
Global $base_dir = @ScriptDir & "\Musik"
$handle = __lastfm_login("<username-here>", "<password-here>")
If __lastfm_select_channel($handle) Then
    $result = __lastfm_get_playlist($handle)
    $array = __lastfm_parse_playlist($result)
    For $i = 1 to $array[0][0]
        $file_name = $base_dir & "\" & $array[$i][0] & "\" & $array[$i][1] & "\" & $array[$i][2] & ".mp3"
        $folder_name = $base_dir & "\" & $array[$i][0]
        $album_name = $base_dir & "\" & $array[$i][0] & "\" & $array[$i][1]
        ConsoleWrite($file_name & @CRLF)
        if not FileExists($file_name) Then
            _FileCreate($file_name)
            FileDelete($file_name)
            InetGet($array[$i][3], $file_name, 0, 1)
            While @InetGetActive
              ToolTip($file_name & " ==> " & Round((@InetGetBytesRead/((($array[$i][5]/1000)*128*1024)/8))*100,0) & "%",0,0)
              Sleep(25)
            Wend
            ToolTip("getting ALBUM cover...",0,0)
            InetGet($array[$i][4], $album_name & "\folder.jpg")
            ToolTip("getting BAND cover...",0,0)
            InetGet($array[$i][6], $folder_name & "\folder.jpg")
        EndIf
    Next
EndIf
;#ce

Func __lastfm_login($username, $pass)
    Local $ret[3]
    Local $md5ed_pass = StringLower(StringTrimLeft(_md5($pass), 2))
    $result = _INetGetSource('http://ws.audioscrobbler.com/radio/handshake.php?version=1.5.2.38918&platform=win32&username=' & $username & '&passwordmd5=' & $md5ed_pass & '&debug=0&partner=')
    $tmp = _StringBetween($result, 'session=', @LF)
    If Not @error Then $ret[0] = $tmp[0]
    $tmp = _StringBetween($result, 'base_url=', @LF)
    If Not @error Then $ret[1] = $tmp[0]
    $tmp = _StringBetween($result, 'base_path=', @LF)
    If Not @error Then $ret[2] = $tmp[0]
    __lastfm_login2($username, $pass)
    Return $ret
EndFunc   ;==>__lastfm_login

Func __lastfm_login2($username, $pass)
    $md5ed_pass = StringLower(StringTrimLeft(_md5($pass), 2))
    $timestamp = _DateDiff('s', "1970/01/01 00:00:00", _NowCalc())
    $salted_pass = StringLower(StringTrimLeft(_md5($md5ed_pass & $timestamp), 2))
    $result = _INetGetSource('http://post.audioscrobbler.com/?hs=true&p=1.2&c=ass&v=1.5.2.38918&u=' & $username & '&t=' & $timestamp & '&a=' & $salted_pass)
    Return $result
EndFunc   ;==>__lastfm_login2

Func __lastfm_select_channel($handle, $channel = "lastfm%3A%2F%2Fglobaltags%2Fmetal", $lang = "de")
    $result = _INetGetSource('http://' & $handle[1] & $handle[2] & '/adjust.php?session=' & $handle[0] & '&url=' & $channel & '&lang=' & $lang)
    If StringInStr($result, "response=OK") Then Return True
    Return False
EndFunc   ;==>__lastfm_select_channel

Func __lastfm_get_playlist($handle)
    $result = _INetGetSource('http://' & $handle[1] & $handle[2] & '/xspf.php?sk=' & $handle[0] & '&discovery=0&desktop=1.5.2.38918')
    Return $result
EndFunc   ;==>__lastfm_get_playlist

Func __lastfm_get_artist_image($artist)
    $artist = _URLEncode($artist)
    Local $link[1]
    Local $result = _INetGetSource('http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=' & $artist & '&limit=1&api_key=b25b959554ed76058ac220b7b2e0a026')
    $link = _StringBetween($result, '<size name="largesquare" width="126" height="126">', '</size>')
    Return $link[0]
EndFunc   ;==>__lastfm_get_artist_image

Func __lastfm_parse_playlist($strXML)
    _XMLLoadXML($strXML, 'xmlns:lastfm="http://www.audioscrobbler.net/dtd/xspf-lastfm"')
    Local $location = _XMLGetValue("trackList/track/location")
    $title = _XMLGetValue("trackList/track/title")
    $album = _XMLGetValue("trackList/track/album")
    $creator = _XMLGetValue("trackList/track/creator")
    $duration = _XMLGetValue("trackList/track/duration")
    $cover = _XMLGetValue("trackList/track/image")
    
    Local $result[$location[0]+1][7], $c
    $result[0][0] = $location[0]
    For $c = 1 to $location[0]
        $result[$c][0] = $creator[$c]
        $result[$c][1] = $album[$c]
        $result[$c][2] = $title[$c]
        $result[$c][3] = $location[$c]
        $result[$c][4] = $cover[$c]
        $result[$c][5] = $duration[$c]
        $result[$c][6] = __lastfm_get_artist_image($creator[$c])
    Next
    Return $result
EndFunc   ;==>__lastfm_parse_playlist

Func _URLEncode($toEncode, $encodeType = 0)
    Local $strHex = "", $iDec
    Local $aryChar = StringSplit($toEncode, "")
    If $encodeType = 1 Then;;Encode EVERYTHING
        For $i = 1 To $aryChar[0]
            $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
        Next
        Return $strHex
    ElseIf $encodeType = 0 Then;;Practical Encoding
        For $i = 1 To $aryChar[0]
            $iDec = Asc($aryChar[$i])
            If $iDec <= 32 Or $iDec = 37 Then
                $strHex = $strHex & "%" & Hex($iDec, 2)
            Else
                $strHex = $strHex & $aryChar[$i]
            EndIf
        Next
        Return $strHex
    ElseIf $encodeType = 2 Then;;RFC 1738 Encoding
        For $i = 1 To $aryChar[0]
            If Not StringInStr("$-_.+!*'(),;/?:@=&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", $aryChar[$i]) Then
                $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
            Else
                $strHex = $strHex & $aryChar[$i]
            EndIf
        Next
        Return $strHex
    EndIf
EndFunc   ;==>_URLEncode

Func _FileCreate($sFilePath)
    ;==============================================
    ; modified to create folder structure...
    ;==============================================
    Local $hOpenFile
    Local $hWriteFile

    $hOpenFile = FileOpen($sFilePath, 10)

    If $hOpenFile = -1 Then
        SetError(1)
        Return 0
    EndIf

    $hWriteFile = FileWrite($hOpenFile, "")

    If $hWriteFile = -1 Then
        SetError(2)
        Return 0
    EndIf

    FileClose($hOpenFile)
    Return 1
EndFunc   ;==>_FileCreate
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

soooooooooooo bed time :-)

3 o'clock already here...

final version for "today":

added features:

*ID3v1 artist,album,title addition

*ID3v2 artist,album,title addition

*COVER integration

the file needs the md5.au3, the _xmldomwrapper.au3 and the ID3.au3 includes to run properly.

#include <inet.au3>
#include "md5.au3"
#include "_XMLDomWrapper.au3"
#include <date.au3>
#include <string.au3>
#include <array.au3>
#include "ID3.au3"

Global $base_dir = @ScriptDir & "\Musik"
$handle = __lastfm_login("<username-here>", "<password-here>")
If __lastfm_select_channel($handle) Then
    While True
        $result = __lastfm_get_playlist($handle)
        $array = __lastfm_parse_playlist($result)
        For $i = 1 To $array[0][0]
            $file_name = $base_dir & "\" & $array[$i][0] & "\" & $array[$i][1] & "\" & $array[$i][2] & ".mp3"
            $folder_name = $base_dir & "\" & $array[$i][0]
            $album_name = $base_dir & "\" & $array[$i][0] & "\" & $array[$i][1]
            ConsoleWrite($file_name & @CRLF)
            If Not FileExists($file_name) Then
                __lastfm_File_Create($file_name)
                FileDelete($file_name)
                InetGet($array[$i][3], $file_name, 0, 1)
                While @InetGetActive
                    ToolTip($file_name & " ==> " & Round((@InetGetBytesRead / ((($array[$i][5] / 1000) * 128 * 1024) / 8)) * 100, 0) & "%", 0, 0)
                    Sleep(25)
                WEnd
                ToolTip("getting ALBUM cover...", 0, 0)
                InetGet($array[$i][4], $album_name & "\folder.jpg")
                ToolTip("integrating ALBUM cover...", 0, 0)
                ;ID3v2
                _ID3SetTagField("TIT2",$array[$i][2])
                _ID3SetTagField("TPE1",$array[$i][0])
                _ID3SetTagField("TALB",$array[$i][1])
                _ID3SetTagField("APIC",$album_name & "\folder.jpg")
                ;ID3v1
                _ID3SetTagField("Title",$array[$i][2])
                _ID3SetTagField("Artist",$array[$i][0])
                _ID3SetTagField("Album",$array[$i][1])
                _ID3WriteTag($file_name)
                ToolTip("getting BAND cover...", 0, 0)
                InetGet($array[$i][6], $folder_name & "\folder.jpg")
            EndIf
        Next
    WEnd
EndIf


Func __lastfm_login($username, $pass)
    Local $ret[3]
    Local $md5ed_pass = StringLower(StringTrimLeft(_md5($pass), 2))
    $result = _INetGetSource('http://ws.audioscrobbler.com/radio/handshake.php?version=1.5.2.38918&platform=win32&username=' & $username & '&passwordmd5=' & $md5ed_pass & '&debug=0&partner=')
    $tmp = _StringBetween($result, 'session=', @LF)
    If Not @error Then $ret[0] = $tmp[0]
    $tmp = _StringBetween($result, 'base_url=', @LF)
    If Not @error Then $ret[1] = $tmp[0]
    $tmp = _StringBetween($result, 'base_path=', @LF)
    If Not @error Then $ret[2] = $tmp[0]
    __lastfm_login2($username, $pass)
    Return $ret
EndFunc   ;==>__lastfm_login

Func __lastfm_login2($username, $pass)
    $md5ed_pass = StringLower(StringTrimLeft(_md5($pass), 2))
    $timestamp = _DateDiff('s', "1970/01/01 00:00:00", _NowCalc())
    $salted_pass = StringLower(StringTrimLeft(_md5($md5ed_pass & $timestamp), 2))
    $result = _INetGetSource('http://post.audioscrobbler.com/?hs=true&p=1.2&c=ass&v=1.5.2.38918&u=' & $username & '&t=' & $timestamp & '&a=' & $salted_pass)
    Return $result
EndFunc   ;==>__lastfm_login2

Func __lastfm_select_channel($handle, $channel = "lastfm%3A%2F%2Fglobaltags%2Fmetal", $lang = "de")
    $result = _INetGetSource('http://' & $handle[1] & $handle[2] & '/adjust.php?session=' & $handle[0] & '&url=' & $channel & '&lang=' & $lang)
    If StringInStr($result, "response=OK") Then Return True
    Return False
EndFunc   ;==>__lastfm_select_channel

Func __lastfm_get_playlist($handle)
    $result = _INetGetSource('http://' & $handle[1] & $handle[2] & '/xspf.php?sk=' & $handle[0] & '&discovery=0&desktop=1.5.2.38918')
    Return $result
EndFunc   ;==>__lastfm_get_playlist

Func __lastfm_get_artist_image($artist)
    $artist = _URLEncode($artist)
    Local $link[1]
    Local $result = _INetGetSource('http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=' & $artist & '&limit=1&api_key=b25b959554ed76058ac220b7b2e0a026')
    $link = _StringBetween($result, '<size name="largesquare" width="126" height="126">', '</size>')
    Return $link[0]
EndFunc   ;==>__lastfm_get_artist_image

Func __lastfm_parse_playlist($strXML)
    _XMLLoadXML($strXML, 'xmlns:lastfm="http://www.audioscrobbler.net/dtd/xspf-lastfm"')
    Local $location = _XMLGetValue("trackList/track/location")
    $title = _XMLGetValue("trackList/track/title")
    $album = _XMLGetValue("trackList/track/album")
    $creator = _XMLGetValue("trackList/track/creator")
    $duration = _XMLGetValue("trackList/track/duration")
    $cover = _XMLGetValue("trackList/track/image")

    Local $result[$location[0] + 1][7], $c
    $result[0][0] = $location[0]
    For $c = 1 To $location[0]
        $result[$c][0] = $creator[$c]
        $result[$c][1] = $album[$c]
        $result[$c][2] = $title[$c]
        $result[$c][3] = $location[$c]
        $result[$c][4] = $cover[$c]
        $result[$c][5] = $duration[$c]
        $result[$c][6] = __lastfm_get_artist_image($creator[$c])
    Next
    Return $result
EndFunc   ;==>__lastfm_parse_playlist

Func _URLEncode($toEncode, $encodeType = 0)
    Local $strHex = "", $iDec
    Local $aryChar = StringSplit($toEncode, "")
    If $encodeType = 1 Then;;Encode EVERYTHING
        For $i = 1 To $aryChar[0]
            $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
        Next
        Return $strHex
    ElseIf $encodeType = 0 Then;;Practical Encoding
        For $i = 1 To $aryChar[0]
            $iDec = Asc($aryChar[$i])
            If $iDec <= 32 Or $iDec = 37 Then
                $strHex = $strHex & "%" & Hex($iDec, 2)
            Else
                $strHex = $strHex & $aryChar[$i]
            EndIf
        Next
        Return $strHex
    ElseIf $encodeType = 2 Then;;RFC 1738 Encoding
        For $i = 1 To $aryChar[0]
            If Not StringInStr("$-_.+!*'(),;/?:@=&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", $aryChar[$i]) Then
                $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
            Else
                $strHex = $strHex & $aryChar[$i]
            EndIf
        Next
        Return $strHex
    EndIf
EndFunc   ;==>_URLEncode

Func __lastfm_File_Create($sFilePath)
    ;==============================================
    ; modified to create folder structure...
    ;==============================================
    Local $hOpenFile
    Local $hWriteFile

    $hOpenFile = FileOpen($sFilePath, 10)

    If $hOpenFile = -1 Then
        SetError(1)
        Return 0
    EndIf

    $hWriteFile = FileWrite($hOpenFile, "")

    If $hWriteFile = -1 Then
        SetError(2)
        Return 0
    EndIf

    FileClose($hOpenFile)
    Return 1
EndFunc   ;==>_FileCreate
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

ok i added the nice dcon dll from mobius and ended up in a nice tool:

"lastfm.exe username password lastfm://globaltag/rock" would create a console application that is pritty the same as streamripper but with a much better stream handling, exact track cuts, cover and id3tag integration (v1+v2) and a nice green console color :P

Please be aware that the following includes are necessary to run the script properly:
#include "ID3.au3"
#include "_XMLDomWrapper.au3"
#include "md5.au3"
#INCLUDE "CONLIB.au3"


you can find them via the search command here in the forum, done by some genious coders.

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Change2CUI=y
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#NoTrayIcon
#include <inet.au3>
#include "md5.au3"
#include "_XMLDomWrapper.au3"
#include <date.au3>
#include <string.au3>
#include <array.au3>
#include "ID3.au3"
#INCLUDE "CONLIB.au3"
FileInstall("DCON.dll","DCON.dll",0)
Opt("OnExitFunc","_ConLibClose")    ;   Close lib on exit
_ConLibLoad(@SCRIPTDIR &"\DCON.dll")
_ConTitle("TheBestRipper 4 Last.fm - v1.0.0.0")
_ConCls()
if $cmdline[0] <> 3 Then
_ConPopStr("TheBestRipper 4 Last.fm - v1.0.0.0",1,20,10,0,7,0)
_CaretSet(5,0)
_ConPutStr("Please enter Username: ",1)
$username = _ConGetStr()
_CaretSet(6,0)
_ConPutStr("Please enter Password: ",1)
$password = _ConGetStr()
_CaretSet(6,0)
_ConPutStr("Please enter Channel: ",1)
$channel = _ConGetStr()
_ConCls()
Else
    $username = $cmdline[1]
    $password = $cmdline[2]
    $channel = $cmdline[3]
EndIf

Global $base_dir = @ScriptDir & "\Musik"
_ConPutStr("logging in with username: " & $username)
$handle = __lastfm_login($username, $password)
_ConPutStr("selecting channel: " & $channel)
If __lastfm_select_channel($handle, $channel) Then
    While True
        _ConPutStr("getting playlist")
        $result = __lastfm_get_playlist($handle)
        _ConPutStr("parsing playlist")
        $array = __lastfm_parse_playlist($result)
        For $i = 1 To $array[0][0]
            $file_name = $base_dir & "\" & $array[$i][0] & "\" & $array[$i][1] & "\" & $array[$i][2] & ".mp3"
            $folder_name = $base_dir & "\" & $array[$i][0]
            $album_name = $base_dir & "\" & $array[$i][0] & "\" & $array[$i][1]
            If Not FileExists($file_name) Then
                _ConPutStr("[ ripping ] "  & $array[$i][0] & " - "  & $array[$i][2],1)
                __lastfm_File_Create($file_name)
                InetGet($array[$i][3], $file_name, 0, 1)
                While @InetGetActive
                    $percent = Round((@InetGetBytesRead / ((($array[$i][5] / 1000) * 128 * 1024) / 8)) * 100, 2)
                    _CaretSet(0,73)
                    _ConPutStr(StringLeft($percent & "     ", 5) & "%", 1)
                    Sleep(25)
                WEnd
                _CaretSet(0,73)
                _ConPutStr(StringLeft("99.00" & "     ", 5) & "%", 1)
                InetGet($array[$i][4], $album_name & "\folder.jpg")
                ;ID3v2
                _ID3SetTagField("TIT2",$array[$i][2])
                _ID3SetTagField("TPE1",$array[$i][0])
                _ID3SetTagField("TALB",$array[$i][1])
                _ID3SetTagField("APIC",$album_name & "\folder.jpg")
                ;ID3v1
                _ID3SetTagField("Title",$array[$i][2])
                _ID3SetTagField("Artist",$array[$i][0])
                _ID3SetTagField("Album",$array[$i][1])
                _ID3WriteTag($file_name)
                InetGet($array[$i][6], $folder_name & "\folder.jpg")
                _CaretSet(0,73)
                _ConColor(10,0)
                _ConPutStr(StringLeft("DONE" & "     ", 6), 0)
                _ConColor(7,0)
            EndIf
        Next
    WEnd
EndIf


Func __lastfm_login($username, $pass)
    Local $ret[3]
    Local $md5ed_pass = StringLower(StringTrimLeft(_md5($pass), 2))
    $result = _INetGetSource('http://ws.audioscrobbler.com/radio/handshake.php?version=1.5.2.38918&platform=win32&username=' & $username & '&passwordmd5=' & $md5ed_pass & '&debug=0&partner=')
    $tmp = _StringBetween($result, 'session=', @LF)
    If Not @error Then $ret[0] = $tmp[0]
    $tmp = _StringBetween($result, 'base_url=', @LF)
    If Not @error Then $ret[1] = $tmp[0]
    $tmp = _StringBetween($result, 'base_path=', @LF)
    If Not @error Then $ret[2] = $tmp[0]
    __lastfm_login2($username, $pass)
    Return $ret
EndFunc   ;==>__lastfm_login

Func __lastfm_login2($username, $pass)
    $md5ed_pass = StringLower(StringTrimLeft(_md5($pass), 2))
    $timestamp = _DateDiff('s', "1970/01/01 00:00:00", _NowCalc())
    $salted_pass = StringLower(StringTrimLeft(_md5($md5ed_pass & $timestamp), 2))
    $result = _INetGetSource('http://post.audioscrobbler.com/?hs=true&p=1.2&c=ass&v=1.5.2.38918&u=' & $username & '&t=' & $timestamp & '&a=' & $salted_pass)
    Return $result
EndFunc   ;==>__lastfm_login2

Func __lastfm_select_channel($handle, $channel = "lastfm://globaltags/metal", $lang = "de")
    $channel = _URLEncode($channel)
    $result = _INetGetSource('http://' & $handle[1] & $handle[2] & '/adjust.php?session=' & $handle[0] & '&url=' & $channel & '&lang=' & $lang)
    If StringInStr($result, "response=OK") Then Return True
    Return False
EndFunc   ;==>__lastfm_select_channel

Func __lastfm_get_playlist($handle)
    $result = _INetGetSource('http://' & $handle[1] & $handle[2] & '/xspf.php?sk=' & $handle[0] & '&discovery=0&desktop=1.5.2.38918')
    Return $result
EndFunc   ;==>__lastfm_get_playlist

Func __lastfm_get_artist_image($artist)
    $artist = _URLEncode($artist)
    Local $link[1]
    Local $result = _INetGetSource('http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=' & $artist & '&limit=1&api_key=b25b959554ed76058ac220b7b2e0a026')
    $link = _StringBetween($result, '<size name="largesquare" width="126" height="126">', '</size>')
    Return $link[0]
EndFunc   ;==>__lastfm_get_artist_image

Func __lastfm_parse_playlist($strXML)
    _XMLLoadXML($strXML, 'xmlns:lastfm="http://www.audioscrobbler.net/dtd/xspf-lastfm"')
    Local $location = _XMLGetValue("trackList/track/location")
    $title = _XMLGetValue("trackList/track/title")
    $album = _XMLGetValue("trackList/track/album")
    $creator = _XMLGetValue("trackList/track/creator")
    $duration = _XMLGetValue("trackList/track/duration")
    $cover = _XMLGetValue("trackList/track/image")

    Local $result[$location[0] + 1][7], $c
    $result[0][0] = $location[0]
    For $c = 1 To $location[0]
        $result[$c][0] = $creator[$c]
        $result[$c][1] = $album[$c]
        $result[$c][2] = $title[$c]
        $result[$c][3] = $location[$c]
        $result[$c][4] = $cover[$c]
        $result[$c][5] = $duration[$c]
        $result[$c][6] = __lastfm_get_artist_image($creator[$c])
    Next
    Return $result
EndFunc   ;==>__lastfm_parse_playlist

Func _URLEncode($toEncode, $encodeType = 0)
    Local $strHex = "", $iDec
    Local $aryChar = StringSplit($toEncode, "")
    If $encodeType = 1 Then;;Encode EVERYTHING
        For $i = 1 To $aryChar[0]
            $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
        Next
        Return $strHex
    ElseIf $encodeType = 0 Then;;Practical Encoding
        For $i = 1 To $aryChar[0]
            $iDec = Asc($aryChar[$i])
            If $iDec <= 32 Or $iDec = 37 Then
                $strHex = $strHex & "%" & Hex($iDec, 2)
            Else
                $strHex = $strHex & $aryChar[$i]
            EndIf
        Next
        Return $strHex
    ElseIf $encodeType = 2 Then;;RFC 1738 Encoding
        For $i = 1 To $aryChar[0]
            If Not StringInStr("$-_.+!*'(),;/?:@=&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", $aryChar[$i]) Then
                $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
            Else
                $strHex = $strHex & $aryChar[$i]
            EndIf
        Next
        Return $strHex
    EndIf
EndFunc   ;==>_URLEncode

Func __lastfm_File_Create($sFilePath)
    ;==============================================
    ; modified to create folder structure...
    ;==============================================
    Local $hOpenFile
    Local $hWriteFile

    $hOpenFile = FileOpen($sFilePath, 10); here was a 2, added 8 (create folder structure)

    If $hOpenFile = -1 Then
        SetError(1)
        Return 0
    EndIf

    $hWriteFile = FileWrite($hOpenFile, "")

    If $hWriteFile = -1 Then
        SetError(2)
        Return 0
    EndIf

    FileClose($hOpenFile)
    FileDelete($sFilePath)
    Return 1
EndFunc   ;==>_FileCreate

additional features in the next releases:

multi ripping (more then one account at a time)

mp3 normalisation (mp3gain)

mysql database support (maybe)

artist filter (no really?)

album filter (boring but useful)

notification by mail (indeet)

notification by twitter (absolutely yes)

listening to the curently downloaded track (never, due to the fact that a stream only can be loaded once [its a stream])

listening to the track you currently finished downloading? (will be done)

Edited by JRSmile
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

hi

the first script (without "CONLIB.au3") works fine

the script with "CONLIB.au3" always fails with "Dependency Failure." - i tried local admin , still not working (vista 64 bit)

maybe the conlib can be omitted by using regular autoit dialogs?

regards

Link to comment
Share on other sites

the dependancy failure is because you are missing dcon.dll from here: http://www.autoitscript.com/forum/index.ph...138&hl=dcon

$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

hi

i added:

- gui autoit code to omit dcon.dll (which never worked here?)

- picture preview of current song

- channels in combobox for easier selection

- save name / pw to ini

- mp3 is now labeled "artist - filename.mp3"

- fixed bug: sometimes the "JPG" is a mislabeled "PNG" file - so transform all pic into JPG before setting to MP3

- added min:sec to File time (i hope its correct)..

regards nobbe

#Region AutoIt3Wrapper directives section
#AutoIt3Wrapper_Icon=lastfm.ico
#AutoIt3Wrapper_UseUpx=y
#AutoIt3Wrapper_Res_Comment="Lastfm"
#AutoIt3Wrapper_Res_Description="Lastfm"
#AutoIt3Wrapper_Res_Fileversion=1.0.0.2
#AutoIt3Wrapper_Res_FileVersion_AutoIncrement=y
#EndRegion AutoIt3Wrapper directives section


#include <inet.au3>
#include "md5.au3"
#include "_XMLDomWrapper.au3"
#include <date.au3>
#include <string.au3>
#include <array.au3>
#include "ID3.au3"

;; gui code
#include <GDIPlus.au3>  ; Initialize GDI+ library

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <ProgressConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $username ;
Global $password
Global $channel
Global $base_dir = @ScriptDir & "\Musik"


#Region ### START Koda GUI section ### Form=lastfm.kxf
$GUI = GUICreate("last.fm", 511, 180, 194, 133)
$edit_username = GUICtrlCreateInput("", 80, 16, 289, 21)
$lbl_user = GUICtrlCreateLabel("Username", 8, 16, 52, 17)
$lbl_pass = GUICtrlCreateLabel("Password", 8, 48, 50, 17)
$edit_password = GUICtrlCreateInput("", 80, 48, 289, 21, BitOR($ES_PASSWORD, $ES_AUTOHSCROLL))
$channel = GUICtrlCreateLabel("Channel", 8, 88, 43, 17)
$btn_login = GUICtrlCreateButton("Login", 384, 16, 75, 25, 0)
$lbl_filename = GUICtrlCreateLabel("                                                                                                                                                                        ", 8, 112, 364, 17)
$progress = GUICtrlCreateProgress(8, 144, 366, 17)
$pic = GUICtrlCreatePic("", 384, 56, 116, 116, BitOR($SS_NOTIFY, $WS_GROUP, $WS_CLIPSIBLINGS))
$combo_channels = GUICtrlCreateCombo("", 80, 80, 289, 25)
GUICtrlSetData(-1, "acoustic|ambient|blues|classical|country|electronic|emo|folk|gothic|hardcore|hip hop|indie|industrial|jazz|metal|pop|punk rock|reggae|rnb|rock|soul|world|60s|70s|80s|90s")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


; read out ini
Global $inilocation = @ScriptDir & "\settings.ini" ;
Global $ini_user = IniRead($inilocation, "settings", "user", "")
Global $ini_pass = IniRead($inilocation, "settings", "pass", "")

GUICtrlSetData($edit_username, $ini_user)
GUICtrlSetData($edit_password, $ini_pass)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        
        Case $btn_login
            _start()
            
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

#cs
    
    if $cmdline[0] <> 3 Then
    _ConPopStr("TheBestRipper 4 Last.fm - v1.0.0.0",1,20,10,0,7,0)
    _CaretSet(5,0)
    _ConPutStr("Please enter Username: ",1)
    $username = _ConGetStr()
    _CaretSet(6,0)
    _ConPutStr("Please enter Password: ",1)
    $password = _ConGetStr()
    _CaretSet(6,0)
    _ConPutStr("Please enter Channel: ",1)
    $channel = _ConGetStr()
    _ConCls()
    Else
    $username = $cmdline[1]
    $password = $cmdline[2]
    $channel = $cmdline[3]
    EndIf
#ce


;start

Func _start()
    Local $pic_preview
    
    
    $username = GUICtrlRead($edit_username)
    $password = GUICtrlRead($edit_password)
    $chan = GUICtrlRead($combo_channels)
    
    ; save to ini for next time
    IniWrite($inilocation, "settings", "user", $username)
    IniWrite($inilocation, "settings", "pass", $password)


    ; disable items
    GUICtrlSetState($edit_username, $GUI_DISABLE)
    GUICtrlSetState($edit_password, $GUI_DISABLE)
    GUICtrlSetState($channel, $GUI_DISABLE)
    GUICtrlSetState($btn_login, $GUI_DISABLE)

    GUICtrlSetData($progress, 0)
    
    GUICtrlSetData($lbl_filename, "logging in with username: " & $username);
    ;_ConPutStr("logging in with username: " & $username)

    $handle = __lastfm_login($username, $password)
    ;_ConPutStr("selecting channel: " & $channel)
    GUICtrlSetData($lbl_filename, "selecting channel: " & $chan)

    If __lastfm_select_channel($handle, $chan) Then
        ;   If __lastfm_select_channel($handle) Then
        While True

            ;_ConPutStr("getting playlist")
            GUICtrlSetData($lbl_filename, "getting playlist")

            $result = __lastfm_get_playlist($handle)
            ;        _ConPutStr("parsing playlist")
            GUICtrlSetData($lbl_filename, "parsing playlist")

            $array = __lastfm_parse_playlist($result)
            
            For $i = 1 To $array[0][0]
                $file_name = $base_dir & "\" & $array[$i][0] & "\" & $array[$i][1] & "\" & $array[$i][0] & " - " & $array[$i][2] & ".mp3"
                $folder_name = $base_dir & "\" & $array[$i][0]
                $album_name = $base_dir & "\" & $array[$i][0] & "\" & $array[$i][1]

                If Not FileExists($file_name) Then
                    ;                _ConPutStr("[ ripping ] "  & $array[$i][0] & " - "  & $array[$i][2],1)
                    
                    __lastfm_File_Create($file_name)

                    ; get album pictures first
                    $pic_preview = $album_name & "\folder.jpg";
                    $pic_preview1 = $album_name & "\folder1.jpg";
                    ; MsgBox(0, "", $array[$i][4]);
                    ; MsgBox(0, $pic_preview, $array[$i][6]);
                    
                    ; try to get length in min:sec
                    Local $t_in = ($array[$i][5] / 1000)
                    Local $l_min = Int($t_in / 60) ;
                    Local $l_sec = Int($t_in - ($l_min * 60)) ;
                    If $l_sec < 10 Then
                        $l_sec = "0" & $l_sec;
                    EndIf
                    
                    ;MsgBox(0, $t_in, $array[$i][5]); bytes total

                    $rc = InetGet($array[$i][4], $album_name & "\folder.jpg")
                    $rc = InetGet($array[$i][6], $pic_preview)
                    ;MsgBox(0, "", $rc);
                    
                    ; BUG sometimes is not a JPG but PNG named "*.jpg"
                    _GDIPlus_Startup()
                    Local $hImage
                    $hImage = _GDIPlus_ImageLoadFromFile($pic_preview)
                    _GDIPlus_ImageSaveToFile($hImage, $pic_preview1)
                    _GDIPlus_ImageDispose($hImage)
                    _GDIPlus_Shutdown()
                    
                    ; set pic before loading
                    GUICtrlSetImage($pic, $pic_preview1)

                    GUICtrlSetData($lbl_filename, "[Rec] " & $array[$i][0] & " - " & $array[$i][2] & " (" & $l_min & ":" & $l_sec & ")")
                    
                    InetGet($array[$i][3], $file_name, 0, 1)

                    While @InetGetActive
                        $percent = Round((@InetGetBytesRead / ((($array[$i][5] / 1000) * 128 * 1024) / 8)) * 100, 2)
                        GUICtrlSetData($progress, $percent)
                        
                        ;                    _CaretSet(0,73)
                        ;                   _ConPutStr(StringLeft($percent & "     ", 5) & "%", 1)
                        Sleep(25)
                    WEnd
                    ;                _CaretSet(0,73)
                    ;               _ConPutStr(StringLeft("99.00" & "     ", 5) & "%", 1)
                    GUICtrlSetData($progress, 100)

                    ;InetGet($array[$i][4], $album_name & "\folder.jpg")

                    ;ID3v2
                    _ID3SetTagField("TIT2", $array[$i][2])
                    _ID3SetTagField("TPE1", $array[$i][0])
                    _ID3SetTagField("TALB", $array[$i][1])
                    _ID3SetTagField("APIC", $pic_preview1);         $album_name & "\folder.jpg")
                    ;ID3v1
                    _ID3SetTagField("Title", $array[$i][2])
                    _ID3SetTagField("Artist", $array[$i][0])
                    _ID3SetTagField("Album", $array[$i][1])
                    _ID3WriteTag($file_name)
                    
                    ;                _CaretSet(0,73)
                    ;                _ConColor(10,0)
                    ;                _ConPutStr(StringLeft("DONE" & "     ", 6), 0)
                    ;               _ConColor(7,0)

                    GUICtrlSetData($lbl_filename, "Done..")

                EndIf
            Next
        WEnd
    EndIf
    
    
    
EndFunc   ;==>_start




Func __lastfm_login($username, $pass)
    ;   MsgBox (0, "", $username & "-"&  $pass)
    
    Local $ret[3]
    Local $md5ed_pass = StringLower(StringTrimLeft(_md5($pass), 2))
    $result = _INetGetSource('http://ws.audioscrobbler.com/radio/handshake.php?version=1.5.2.38918&platform=win32&username=' & $username & '&passwordmd5=' & $md5ed_pass & '&debug=0&partner=')
    ;   MsgBox (0, "", $result)
    
    
    $tmp = _StringBetween($result, 'session=', @LF)
    If Not @error Then $ret[0] = $tmp[0]
    $tmp = _StringBetween($result, 'base_url=', @LF)
    If Not @error Then $ret[1] = $tmp[0]
    $tmp = _StringBetween($result, 'base_path=', @LF)
    If Not @error Then $ret[2] = $tmp[0]
    __lastfm_login2($username, $pass)
    Return $ret
EndFunc   ;==>__lastfm_login

Func __lastfm_login2($username, $pass)
    $md5ed_pass = StringLower(StringTrimLeft(_md5($pass), 2))
    $timestamp = _DateDiff('s', "1970/01/01 00:00:00", _NowCalc())
    $salted_pass = StringLower(StringTrimLeft(_md5($md5ed_pass & $timestamp), 2))
    $result = _INetGetSource('http://post.audioscrobbler.com/?hs=true&p=1.2&c=ass&v=1.5.2.38918&u=' & $username & '&t=' & $timestamp & '&a=' & $salted_pass)
    Return $result
EndFunc   ;==>__lastfm_login2



; changed
Func __lastfm_select_channel($handle, $chan, $lang = "de")
    Local $channel

    If $chan <> "" Then
        $channel = "lastfm%3A%2F%2Fglobaltags%2F" & $chan
    Else ; default
        $channel = "lastfm%3A%2F%2Fglobaltags%2Frock"
    EndIf
    
    $result = _INetGetSource('http://' & $handle[1] & $handle[2] & '/adjust.php?session=' & $handle[0] & '&url=' & $channel & '&lang=' & $lang)
    If StringInStr($result, "response=OK") Then Return True
    Return False
EndFunc   ;==>__lastfm_select_channel




Func __lastfm_get_playlist($handle)
    $result = _INetGetSource('http://' & $handle[1] & $handle[2] & '/xspf.php?sk=' & $handle[0] & '&discovery=0&desktop=1.5.2.38918')
    Return $result
EndFunc   ;==>__lastfm_get_playlist


Func __lastfm_get_artist_image($artist)
    $artist = _URLEncode($artist)
    Local $link[1]
    Local $result = _INetGetSource('http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=' & $artist & '&limit=1&api_key=b25b959554ed76058ac220b7b2e0a026')
    $link = _StringBetween($result, '<size name="largesquare" width="126" height="126">', '</size>')
    Return $link[0]
EndFunc   ;==>__lastfm_get_artist_image

Func __lastfm_parse_playlist($strXML)
    _XMLLoadXML($strXML, 'xmlns:lastfm="http://www.audioscrobbler.net/dtd/xspf-lastfm"')
    Local $location = _XMLGetValue("trackList/track/location")
    $title = _XMLGetValue("trackList/track/title")
    $album = _XMLGetValue("trackList/track/album")
    $creator = _XMLGetValue("trackList/track/creator")
    $duration = _XMLGetValue("trackList/track/duration")
    $cover = _XMLGetValue("trackList/track/image")

    Local $result[$location[0] + 1][7], $c
    $result[0][0] = $location[0]
    For $c = 1 To $location[0]
        $result[$c][0] = $creator[$c]
        $result[$c][1] = $album[$c]
        $result[$c][2] = $title[$c]
        $result[$c][3] = $location[$c]
        $result[$c][4] = $cover[$c]
        $result[$c][5] = $duration[$c]
        $result[$c][6] = __lastfm_get_artist_image($creator[$c])
    Next
    Return $result
EndFunc   ;==>__lastfm_parse_playlist

Func _URLEncode($toEncode, $encodeType = 0)
    Local $strHex = "", $iDec
    Local $aryChar = StringSplit($toEncode, "")
    If $encodeType = 1 Then;;Encode EVERYTHING
        For $i = 1 To $aryChar[0]
            $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
        Next
        Return $strHex
    ElseIf $encodeType = 0 Then;;Practical Encoding
        For $i = 1 To $aryChar[0]
            $iDec = Asc($aryChar[$i])
            If $iDec <= 32 Or $iDec = 37 Then
                $strHex = $strHex & "%" & Hex($iDec, 2)
            Else
                $strHex = $strHex & $aryChar[$i]
            EndIf
        Next
        Return $strHex
    ElseIf $encodeType = 2 Then;;RFC 1738 Encoding
        For $i = 1 To $aryChar[0]
            If Not StringInStr("$-_.+!*'(),;/?:@=&abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", $aryChar[$i]) Then
                $strHex = $strHex & "%" & Hex(Asc($aryChar[$i]), 2)
            Else
                $strHex = $strHex & $aryChar[$i]
            EndIf
        Next
        Return $strHex
    EndIf
EndFunc   ;==>_URLEncode

Func __lastfm_File_Create($sFilePath)
    ;==============================================
    ; modified to create folder structure...
    ;==============================================
    Local $hOpenFile
    Local $hWriteFile

    $hOpenFile = FileOpen($sFilePath, 10); here was a 2, added 8 (create folder structure)

    If $hOpenFile = -1 Then
        SetError(1)
        Return 0
    EndIf

    $hWriteFile = FileWrite($hOpenFile, "")

    If $hWriteFile = -1 Then
        SetError(2)
        Return 0
    EndIf

    FileClose($hOpenFile)
    FileDelete($sFilePath)
    Return 1
EndFunc   ;==>__lastfm_File_Create
Edited by nobbe
Link to comment
Share on other sites

i also changed the function to avoid an error if no picture is found

Func __lastfm_get_artist_image($artist)
    $artist = _URLEncode($artist)
    Local $link[1]
    Local $result = _INetGetSource('http://ws.audioscrobbler.com/2.0/?method=artist.getimages&artist=' & $artist & '&limit=1&api_key=b25b959554ed76058ac220b7b2e0a026')
    $link = _StringBetween($result, '<size name="largesquare" width="126" height="126">', '</size>')

; or it will cause sometimes error when no picture is found
If IsArray($link) Then
        Return $link[0]
    Else
        Return ""
    EndIf
    
EndFunc   ;==>__lastfm_get_artist_image
Link to comment
Share on other sites

yea, error correction is the big thingie, i always keep in mind ................................... not to use on POCs... :-)

$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...