from plugins.util import command, get_url
import json
import re

SPOTIFY_URI_REGEX = r"(?<=spotify:)(?:track|album|artist):[a-zA-Z0-9]{22}"
ENDPOINT = "https://api.spotify.com/v1/{0}s/{1}"

@command()
def spotify(m):
    spotify_uris = re.findall(SPOTIFY_URI_REGEX, m.body)
    for spotify_uri in spotify_uris:
        try:
            type, id = _parse_spotify_uri(spotify_uri)
            req = get_url(m, ENDPOINT.format(type, id))
            if req:
                blob = json.loads(req)
                if type == "track" or type == "album":
                    m.bot.private_message(m.location,
                                          blob["artists"][0]["name"] + " - " + blob["name"])
                else:
                    m.bot.private_message(m.location, blob["name"])
        except ValueError:
            m.bot.logger.error("Invalid Spotify URI: " + spotify_uri)

def _parse_spotify_uri(s):
    [type, id] = s.split(':')
    return type, id

from plugins.util import command, get_url
import json
import re

SPOTIFY_URI_REGEX = r"(?<=spotify:)(?:track|album|artist):[a-zA-Z0-9]{22}"
ENDPOINT = "https://api.spotify.com/v1/{0}s/{1}"

@command()
def spotify(m):
    spotify_uris = re.findall(SPOTIFY_URI_REGEX, m.body)
    for spotify_uri in spotify_uris:
        try:
            type, id = _parse_spotify_uri(spotify_uri)
        except ValueError:
            m.bot.logger.error("Invalid Spotify URI: " + spotify_uri)
        else:
            req = get_url(m, ENDPOINT.format(type, id))
            if req:
                blob = json.loads(req)
                if type == "track" or type == "album":
                    m.bot.private_message(m.location, '"{0}" by {1} - {2}'
                                          .format(blob["name"], blob["artists"][0]["name"],
                                                  blob["external_urls"]["spotify"]))
                else:
                    m.bot.private_message(m.location, "{0} - {1}"
                                          .format(blob["name"], blob["external_urls"]["spotify"]))

def _parse_spotify_uri(s):
    [type, id] = s.split(':')
    return type, id

Change formatting of response, add URL
Change formatting of response, add URL

  from plugins.util import command, get_url
  import json
  import re
  
  SPOTIFY_URI_REGEX = r"(?<=spotify:)(?:track|album|artist):[a-zA-Z0-9]{22}"
  ENDPOINT = "https://api.spotify.com/v1/{0}s/{1}"
  
  @command()
  def spotify(m):
      spotify_uris = re.findall(SPOTIFY_URI_REGEX, m.body)
      for spotify_uri in spotify_uris:
          try:
              type, id = _parse_spotify_uri(spotify_uri)
+         except ValueError:
+             m.bot.logger.error("Invalid Spotify URI: " + spotify_uri)
+         else:
              req = get_url(m, ENDPOINT.format(type, id))
              if req:
                  blob = json.loads(req)
                  if type == "track" or type == "album":
-                     m.bot.private_message(m.location,
+                     m.bot.private_message(m.location, '"{0}" by {1} - {2}'
-                                           blob["artists"][0]["name"] + " - " + blob["name"])
+                                           .format(blob["name"], blob["artists"][0]["name"],
+                                                   blob["external_urls"]["spotify"]))
                  else:
-                     m.bot.private_message(m.location, blob["name"])
+                     m.bot.private_message(m.location, "{0} - {1}"
+                                           .format(blob["name"], blob["external_urls"]["spotify"]))
-         except ValueError:
-             m.bot.logger.error("Invalid Spotify URI: " + spotify_uri)
  
  def _parse_spotify_uri(s):
      [type, id] = s.split(':')
      return type, id
  
Change formatting of response, add URL
## Code Before:
from plugins.util import command, get_url
import json
import re

SPOTIFY_URI_REGEX = r"(?<=spotify:)(?:track|album|artist):[a-zA-Z0-9]{22}"
ENDPOINT = "https://api.spotify.com/v1/{0}s/{1}"

@command()
def spotify(m):
    spotify_uris = re.findall(SPOTIFY_URI_REGEX, m.body)
    for spotify_uri in spotify_uris:
        try:
            type, id = _parse_spotify_uri(spotify_uri)
            req = get_url(m, ENDPOINT.format(type, id))
            if req:
                blob = json.loads(req)
                if type == "track" or type == "album":
                    m.bot.private_message(m.location,
                                          blob["artists"][0]["name"] + " - " + blob["name"])
                else:
                    m.bot.private_message(m.location, blob["name"])
        except ValueError:
            m.bot.logger.error("Invalid Spotify URI: " + spotify_uri)

def _parse_spotify_uri(s):
    [type, id] = s.split(':')
    return type, id

## Instruction:
Change formatting of response, add URL
## Code After:
from plugins.util import command, get_url
import json
import re

SPOTIFY_URI_REGEX = r"(?<=spotify:)(?:track|album|artist):[a-zA-Z0-9]{22}"
ENDPOINT = "https://api.spotify.com/v1/{0}s/{1}"

@command()
def spotify(m):
    spotify_uris = re.findall(SPOTIFY_URI_REGEX, m.body)
    for spotify_uri in spotify_uris:
        try:
            type, id = _parse_spotify_uri(spotify_uri)
        except ValueError:
            m.bot.logger.error("Invalid Spotify URI: " + spotify_uri)
        else:
            req = get_url(m, ENDPOINT.format(type, id))
            if req:
                blob = json.loads(req)
                if type == "track" or type == "album":
                    m.bot.private_message(m.location, '"{0}" by {1} - {2}'
                                          .format(blob["name"], blob["artists"][0]["name"],
                                                  blob["external_urls"]["spotify"]))
                else:
                    m.bot.private_message(m.location, "{0} - {1}"
                                          .format(blob["name"], blob["external_urls"]["spotify"]))

def _parse_spotify_uri(s):
    [type, id] = s.split(':')
    return type, id
