-
-
Save manniru/a7e0444756a57bf7b409211cbde2875f to your computer and use it in GitHub Desktop.
m3u to JSON converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rjson) | |
trim <- function (x) gsub("^\\s+|\\s+$", "", x) | |
data_dir = 'Documents/playlists/' | |
play_list = list() | |
for(i in list.files(data_dir,"^.*\\.(m3u)$")){ | |
year = substr(i,1,4) # record file specific information, in this case year from playlist group | |
d = data.frame() | |
for(j in readLines(paste0(data_dir,i),encoding = 'latin1')){ | |
if(substr(j,1,8) == "#EXTINF:"){ # track line | |
extract = trim(strsplit(x = substr(j,9,nchar(j)), split = c(" - |,"))[[1]][-1]) | |
d <- rbind(d,data.frame(extract[1],extract[2])) | |
} | |
} | |
colnames(d) <- c("artist", "song") | |
play_list[[i]] = list(name=i,year=year,tracks=d) | |
} | |
write(toJSON(play_list),paste0(data_dir,"playlists.json")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment