help spotify playlist

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jay lee
Posts: 83
Joined: 14 Oct 2021, 11:17

help spotify playlist

Post by jay lee » 25 Mar 2023, 08:44

Hey, I made a script that gets the track names from a spotify playlist without the official api but the webrequest only loads the first 30 songs of a playlist. Any help?
Here is my code, I know it could be cleaner but it works

Code: Select all

searchURL = https://open.spotify.com/playlist/4yfb4bev0TlWmjiZClib9D?si=b916fe50ce414781
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", searchURL, false)
whr.Send()
responseText := UrlDecode(whr.ResponseText)
Clipboard := UrlDecode(responseText)
playlistname := SubStr(responseText, InStr(responseText, "<title>") + 7, InStr(responseText, "- playlist by") - InStr(responseText, "<title>") - 8)
FileCreateDir, %A_ScriptDir%\Downloads\%playlistname%
search = aria-label="track
search2 = "><span class="
search3 = href="/artist/
search4 = ">
search5 = </a>
search6 = </a>, <a href="/artist
global ID, search, playlistname
FileDelete, list.txt
loop
{
    if(A_Index = 31)
        break
    name2 := SubStr(responseText, (InStr(responseText, search , false, 1, A_Index) + 18))
    name := SubStr(name2, 1, (InStr(name2, search2) - 1))
    artist3 := SubStr(name2, (InStr(name2, search3 , false, 1, 1) + 14))
    artist2 := SubStr(artist3, 1, (InStr(artist3, search4) - 1))
    artist := SubStr(artist3, (StrLen(artist2) + 3), (InStr(artist3, search5) - (StrLen(artist2) + 3)))
    artist4 := SubStr(artist3, InStr(artist3, search5), 22)
    if(artist4 = search6) {
        artist5 := SubStr(artist3, InStr(artist3, search5) + 22)
        artist6 := SubStr(artist5, 1, (InStr(artist5, search4) - 1))
        artist7 := SubStr(artist5, (StrLen(artist6) + 3), (InStr(artist5, search5) - (StrLen(artist6) + 3)))
        out = %artist%, %artist7% - %name%
    }
    else
        out = %artist% - %name%
    FileAppend, %out%`n, list.txt
}
exitapp


UrlDecode(url) {
    needle := "i)(?:%[0-9a-f]{2})+"
    position := 1
    Loop {
        position := RegExMatch(url, needle, code, position)
        If (position = 0) {
            Break
        }
        VarSetCapacity(binary, StrLen(code) // 3, 0)
        Loop, Parse, % SubStr(code, 2), % "%" 
        {
            NumPut("0x" . A_LoopField, binary, A_Index - 1, "UChar")
        }
        replacement := StrGet(&binary, "UTF-8")
        url := RegExReplace(url, needle, replacement, , 1, position)
        position := position + StrLen(replacement)
    }
    Return url
}

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: help spotify playlist

Post by swagfag » 25 Mar 2023, 10:22

use the official api or automate an entire browser instance, scroll the tracklist in it all the way down, then parse the tracks out of the DOM tree/the requests sent

malcev
Posts: 1769
Joined: 12 Aug 2014, 12:37

Re: help spotify playlist

Post by malcev » 25 Mar 2023, 11:05

Your song names are in json.
Open spotify site in browser and analyze network requests.

Post Reply

Return to “Ask for Help (v1)”