spCSVParse.py
· 2.6 KiB · Python
Raw
#!/usr/bin/env python
# Spotify URI list to CSV with track information
# by blha303 <stevensmith.ome@gmail.com>
# Licensed under the Mozilla Public License 2.0 https://www.mozilla.org/MPL/2.0/
# Tested under Python 2.7.3. Small changes may need to be made to be backwards-compatible.
# Usage: spCSVParse.py <list of spotify uri's separated by spaces>
# Select songs from a playlist in Spotify, right click when they're all selected,
# click Copy Spotify URI, then paste in after 'spCSVParse.py '.
# Alternatively, a file called playlists in the same folder
# This file can contain other lines (e.g. section headings) if desired. The script
# only parses lines starting with 'spotify:track' or 'spotify:local'
# If you're making the playlists file by pasting from Spotify, make sure to:
# 1. set the linebreaks to \n instead of \r\n (also known as UNIX mode, as opposed
# to Windows mode)
# 2. replace all spaces with \n. I did this by replacing " spotify" with "\nspotify"
# If you have questions about this file, ask below, or on irc.esper.net #blha303
import json
from urllib import urlopen
import sys
import codecs
import time
def getJson(uri):
data = urlopen("http://ws.spotify.com/lookup/1/.json?uri=%s" % uri).read()
datad = json.loads(data)
if datad["info"]["type"] != "track": #validating
print "Invalid api reply for " + s
sys.exit()
return datad
def csvEscape (s):
if "," in s:
s.replace(',', '""')
s = '"' + s + '"'
return s
content = sys.argv[1:]
if content == []:
f = codecs.open("playlists", "r", "utf-8")
content = f.readlines()
f.close()
nf = codecs.open("output.csv", "w", "utf-8")
starttime = time.time()
print "Started at " + str(int(starttime))
for s in content:
if s[:13] == "spotify:track":
datad = getJson(s)
name = csvEscape(datad["track"]["name"])
artist = csvEscape(datad["track"]["artists"][0]["name"])
album = csvEscape(datad["track"]["album"]["name"])
output = artist + "," + name + "," + album
nf.write(output + "\n")
print s.replace("\n", "") + " : " + output
elif s[:13] == "spotify:local":
out = s.replace("spotify:local:", "").split(":")
artist = csvEscape(out[0].replace("+", " "))
album = csvEscape(out[1].replace("+", " "))
name = csvEscape(out[2].replace("+", " "))
output = artist + "," + name + "," + album
nf.write(output + "\n")
print s.replace("\n", "") + " : " + output
else:
print s.replace("\n", "")
if "\n" in s:
nf.write(s)
else:
nf.write(s + "\n")
nf.close()
endtime = time.time()
print "Completed at " + str(int(endtime))
duration = endtime - starttime
print "Process completed in " + str(int(duration)) + " seconds"
| 1 | #!/usr/bin/env python |
| 2 | # Spotify URI list to CSV with track information |
| 3 | # by blha303 <stevensmith.ome@gmail.com> |
| 4 | # Licensed under the Mozilla Public License 2.0 https://www.mozilla.org/MPL/2.0/ |
| 5 | |
| 6 | # Tested under Python 2.7.3. Small changes may need to be made to be backwards-compatible. |
| 7 | # Usage: spCSVParse.py <list of spotify uri's separated by spaces> |
| 8 | # Select songs from a playlist in Spotify, right click when they're all selected, |
| 9 | # click Copy Spotify URI, then paste in after 'spCSVParse.py '. |
| 10 | # Alternatively, a file called playlists in the same folder |
| 11 | # This file can contain other lines (e.g. section headings) if desired. The script |
| 12 | # only parses lines starting with 'spotify:track' or 'spotify:local' |
| 13 | # If you're making the playlists file by pasting from Spotify, make sure to: |
| 14 | # 1. set the linebreaks to \n instead of \r\n (also known as UNIX mode, as opposed |
| 15 | # to Windows mode) |
| 16 | # 2. replace all spaces with \n. I did this by replacing " spotify" with "\nspotify" |
| 17 | # If you have questions about this file, ask below, or on irc.esper.net #blha303 |
| 18 | |
| 19 | import json |
| 20 | from urllib import urlopen |
| 21 | import sys |
| 22 | import codecs |
| 23 | import time |
| 24 | |
| 25 | def getJson(uri): |
| 26 | data = urlopen("http://ws.spotify.com/lookup/1/.json?uri=%s" % uri).read() |
| 27 | datad = json.loads(data) |
| 28 | if datad["info"]["type"] != "track": #validating |
| 29 | print "Invalid api reply for " + s |
| 30 | sys.exit() |
| 31 | return datad |
| 32 | |
| 33 | def csvEscape (s): |
| 34 | if "," in s: |
| 35 | s.replace(',', '""') |
| 36 | s = '"' + s + '"' |
| 37 | return s |
| 38 | |
| 39 | content = sys.argv[1:] |
| 40 | if content == []: |
| 41 | f = codecs.open("playlists", "r", "utf-8") |
| 42 | content = f.readlines() |
| 43 | f.close() |
| 44 | nf = codecs.open("output.csv", "w", "utf-8") |
| 45 | starttime = time.time() |
| 46 | print "Started at " + str(int(starttime)) |
| 47 | for s in content: |
| 48 | if s[:13] == "spotify:track": |
| 49 | datad = getJson(s) |
| 50 | name = csvEscape(datad["track"]["name"]) |
| 51 | artist = csvEscape(datad["track"]["artists"][0]["name"]) |
| 52 | album = csvEscape(datad["track"]["album"]["name"]) |
| 53 | output = artist + "," + name + "," + album |
| 54 | nf.write(output + "\n") |
| 55 | print s.replace("\n", "") + " : " + output |
| 56 | elif s[:13] == "spotify:local": |
| 57 | out = s.replace("spotify:local:", "").split(":") |
| 58 | artist = csvEscape(out[0].replace("+", " ")) |
| 59 | album = csvEscape(out[1].replace("+", " ")) |
| 60 | name = csvEscape(out[2].replace("+", " ")) |
| 61 | output = artist + "," + name + "," + album |
| 62 | nf.write(output + "\n") |
| 63 | print s.replace("\n", "") + " : " + output |
| 64 | else: |
| 65 | print s.replace("\n", "") |
| 66 | if "\n" in s: |
| 67 | nf.write(s) |
| 68 | else: |
| 69 | nf.write(s + "\n") |
| 70 | |
| 71 | nf.close() |
| 72 | endtime = time.time() |
| 73 | print "Completed at " + str(int(endtime)) |
| 74 | duration = endtime - starttime |
| 75 | print "Process completed in " + str(int(duration)) + " seconds" |