Get Title from spotify link

Discussion in 'Internet for Musician' started by Highdom, May 22, 2021.

  1. Highdom

    Highdom Kapellmeister

    Joined:
    Oct 16, 2016
    Messages:
    391
    Likes Received:
    47
    Hello guys,
    I'm wondering if you know how to get automatically the title of a track from a Spotify URL (or a list). I mean this type of link https://open.spotify.com/track/blablabla

    Thanks

     
  2.  
  3. phumb-reh

    phumb-reh Guest

    Run this on your browser console:

    Code:
    var songname = document.getElementsByTagName('h1')[0].innerText;
    var artist = "";
    for(let a of document.getElementsByTagName('a')) {
        if(/artist/.test(a.href) && a.attributes.tabindex !== undefined) {
            artist = a.innerText;
        }
    }
    var str = artist + " - " + songname;
    alert(str);
    
    Or create a bookmark and run it on that page:
    Code:
    javascript:var sn=document.getElementsByTagName('h1')[0].innerText;var ar="";for(let a of document.getElementsByTagName('a')){if(/artist/.test(a.href)&&a.attributes.tabindex!==undefined){ar = a.innerText;}};var str=ar+" - "+sn;alert(str);
    Adjust as necessary.
     
    • Like Like x 2
    • Interesting Interesting x 1
    • List
  4. Highdom

    Highdom Kapellmeister

    Joined:
    Oct 16, 2016
    Messages:
    391
    Likes Received:
    47
    is there a R version of this code? Thanks
     
  5. phumb-reh

    phumb-reh Guest

    Best Answer
    Code:
    #install.packages("tidyverse")
    
    library(httr)
    library(rvest)
    library(dplyr)
    
    url <- "https://open.spotify.com/track/1kTlT3phtU2yqTJYT1x6hb"
    
    html <- read_html(url)
    
    songname <- html_text(html_nodes(html,"h1"))[1]
    
    for(link in html_nodes(html, "a")) {
      if (grepl("artist", html_attr(link, "href"))) {
        artist <- html_text(link)
        break
      }
    }
    
    title <- paste(artist, songname, sep = " - ")
    
    print(title)
    
     
  6. Highdom

    Highdom Kapellmeister

    Joined:
    Oct 16, 2016
    Messages:
    391
    Likes Received:
    47
    Thanks, for your reply! Very useful

    I was wondering if with R I can do something more. I usually download mp3 from MP3freeclick (https://myfreemp3music.com/), a website suggested from people in this forum.

    My question is: can I use R to get also automatically the mp3 from a spotify link?
    More precisely, now with R i have the string that i usually put manually in the search field of that website and then hit search. So, can R automatically perform the search and download the first result for me?

    Thanks, it would be an amazing result!
     
  7. Futurewine

    Futurewine Audiosexual

    Joined:
    Oct 4, 2017
    Messages:
    885
    Likes Received:
    558
    Location:
    Sound City Labs
    julia julia .. im exploring.. omgg
     
  8. phumb-reh

    phumb-reh Guest

    Sure it's possible, httr is a wrapper around libcurl so it can retrieve arbitary/raw data, also it can do POSTs which you would need for searching.

    Looks like you need to find the post endpoint on myfreemp3music.com (https://myfreemp3music.com/api/search.php), craft a POST query to it with content-type application/x-www-form-urlencoded with the similar payload: "q=my+song+title+search&page=0", you'll get back some JSON which contains the results (you can use jsonlite or similar to parse the JSON).

    Then use GET from httr to download the raw data and save it as mp3.

    Or so I'd think.
     
  9. Highdom

    Highdom Kapellmeister

    Joined:
    Oct 16, 2016
    Messages:
    391
    Likes Received:
    47
    How can I find the post endpoint? It seems that when I push "search" the url doesn't change
     
  10. phumb-reh

    phumb-reh Guest

    Use the dev tools in your browser, F12 in both Firefox and Chrome based browsers, then select network-tab, make your search and inspect the request/response. It's a XMLHttpRequest POST -query.
     
  11. Highdom

    Highdom Kapellmeister

    Joined:
    Oct 16, 2016
    Messages:
    391
    Likes Received:
    47
    I think finally I came up with the POST query. The content of the responce seems to be json code, indeed if I convert from raw to text the $content of the responce i see

    Code:
    [1] "({\"response\":[\"apple\",{\"artist\":\"Fedez\",\"id\":74089577,\"owner_id\":-2001089577,\"title\":\"Bella Storia\",\"duration\":176,\"access_key\":\"f7b67f96d93d5a54f7\",\"is_licensed\":true,\"url\":\"https:\\/\\/cs9-17v4.vkuseraudio.net\\/p1\\/8e345e77f94555.mp3?extra=yBNWfWXsct6bIE-VXZMJ8-oXSKMaIGvW5I-GVS8aucGKFKTgamcF2caqEPiw_ELC7YiLaPbqtAaGtm6vdc5IlqjRArs1i1WP8RbplPCfQUVP26uepZ68sKtGO06yrqEHjkKNowuTakO_NvXZM1mPTLGzZQ&long_chunk=1\",\"date\":1600117136, ....... 
    But if i try to parse it with jsonlite::parse_json() I got the following error
    Code:
    Error: lexical error: invalid char in json text.
                                           ({"response":["apple",{"artist"
                         (right here) ------^
    
    Any suggestion to parsing correctly? Thanks
     
  12. Highdom

    Highdom Kapellmeister

    Joined:
    Oct 16, 2016
    Messages:
    391
    Likes Received:
    47
    I don't know but this is not working anymore... can you guess why?
     
Loading...
Similar Threads - Title spotify link Forum Date
Please help me identify name of Nexus Expansion title Presets, Patches Dec 4, 2024
I am looking for the artist or song title of this song: … Working with Sound Dec 2, 2024
Anyone create or have handwritten calligraphy text animation titles for video? Working with Video Aug 26, 2024
Best source for Subtitles? Lounge May 15, 2024
Need software to identify song titles. Shazam Alternative Music Jan 1, 2024
Loading...