spCSVParse.py
· 3.4 KiB · Python
Raw
#!/usr/bin/env python2
# Spotify URI list to CSV with track information
# by blha303 <stevensmith.ome@gmail.com>
# Licensed under the zlib license:
# Copyright © 2013 Steven Smith
# This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
# Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
# 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
# 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 python2 |
| 2 | # Spotify URI list to CSV with track information |
| 3 | # by blha303 <stevensmith.ome@gmail.com> |
| 4 | # Licensed under the zlib license: |
| 5 | |
| 6 | # Copyright © 2013 Steven Smith |
| 7 | # This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. |
| 8 | # Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: |
| 9 | # 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. |
| 10 | # 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. |
| 11 | # 3. This notice may not be removed or altered from any source distribution. |
| 12 | |
| 13 | # Tested under Python 2.7.3. Small changes may need to be made to be backwards-compatible. |
| 14 | # Usage: spCSVParse.py <list of spotify uri's separated by spaces> |
| 15 | # Select songs from a playlist in Spotify, right click when they're all selected, |
| 16 | # click Copy Spotify URI, then paste in after 'spCSVParse.py '. |
| 17 | # Alternatively, a file called playlists in the same folder |
| 18 | # This file can contain other lines (e.g. section headings) if desired. The script |
| 19 | # only parses lines starting with 'spotify:track' or 'spotify:local' |
| 20 | # If you're making the playlists file by pasting from Spotify, make sure to: |
| 21 | # 1. set the linebreaks to \n instead of \r\n (also known as UNIX mode, as opposed |
| 22 | # to Windows mode) |
| 23 | # 2. replace all spaces with \n. I did this by replacing " spotify" with "\nspotify" |
| 24 | # If you have questions about this file, ask below, or on irc.esper.net #blha303 |
| 25 | |
| 26 | import json |
| 27 | from urllib import urlopen |
| 28 | import sys |
| 29 | import codecs |
| 30 | import time |
| 31 | |
| 32 | def getJson(uri): |
| 33 | data = urlopen("http://ws.spotify.com/lookup/1/.json?uri=%s" % uri).read() |
| 34 | datad = json.loads(data) |
| 35 | if datad["info"]["type"] != "track": #validating |
| 36 | print "Invalid api reply for " + s |
| 37 | sys.exit() |
| 38 | return datad |
| 39 | |
| 40 | def csvEscape (s): |
| 41 | if "," in s: |
| 42 | s.replace(',', '""') |
| 43 | s = '"' + s + '"' |
| 44 | return s |
| 45 | |
| 46 | content = sys.argv[1:] |
| 47 | if content == []: |
| 48 | f = codecs.open("playlists", "r", "utf-8") |
| 49 | content = f.readlines() |
| 50 | f.close() |
| 51 | nf = codecs.open("output.csv", "w", "utf-8") |
| 52 | starttime = time.time() |
| 53 | print "Started at " + str(int(starttime)) |
| 54 | for s in content: |
| 55 | if s[:13] == "spotify:track": |
| 56 | datad = getJson(s) |
| 57 | name = csvEscape(datad["track"]["name"]) |
| 58 | artist = csvEscape(datad["track"]["artists"][0]["name"]) |
| 59 | album = csvEscape(datad["track"]["album"]["name"]) |
| 60 | output = artist + "," + name + "," + album |
| 61 | nf.write(output + "\n") |
| 62 | print s.replace("\n", "") + " : " + output |
| 63 | elif s[:13] == "spotify:local": |
| 64 | out = s.replace("spotify:local:", "").split(":") |
| 65 | artist = csvEscape(out[0].replace("+", " ")) |
| 66 | album = csvEscape(out[1].replace("+", " ")) |
| 67 | name = csvEscape(out[2].replace("+", " ")) |
| 68 | output = artist + "," + name + "," + album |
| 69 | nf.write(output + "\n") |
| 70 | print s.replace("\n", "") + " : " + output |
| 71 | else: |
| 72 | print s.replace("\n", "") |
| 73 | if "\n" in s: |
| 74 | nf.write(s) |
| 75 | else: |
| 76 | nf.write(s + "\n") |
| 77 | |
| 78 | nf.close() |
| 79 | endtime = time.time() |
| 80 | print "Completed at " + str(int(endtime)) |
| 81 | duration = endtime - starttime |
| 82 | print "Process completed in " + str(int(duration)) + " seconds" |