{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.last.fm/api/show/artist.getTopTags\n",
    "\n",
    "Get the top tags for an artist on Last.fm, ordered by popularity.  \n",
    "\n",
    "**artist (Required (unless mbid)] :** The artist name  \n",
    "**mbid (Optional) :** The musicbrainz id for the artist  \n",
    "**autocorrect[0|1] (Optional) :** Transform misspelled artist names into correct artist names, returning the correct   version instead. The corrected artist name will be returned in the response.  \n",
    "**api_key (Required) :** A Last.fm API key.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def artist_getTopTags(artist, api_key='3675d2cb82f8d59bcc885774869aca18', mbid=None, autocorrect=None, maxTags = 10):\n",
    "    import http.client,  urllib.parse, json\n",
    "\n",
    "    #Parametros necesarios\n",
    "    params_dict = {\n",
    "            'method':'artist.getTopTags',\n",
    "            'artist' : artist,\n",
    "            'api_key' : api_key,\n",
    "            'format' : 'json'\n",
    "    }\n",
    "\n",
    "    #Parametros opcionales\n",
    "    if autocorrect:\n",
    "        params_dict['autocorrect'] = autocorrect\n",
    "    if mbid:\n",
    "        params_dict['mbid'] = mbid\n",
    "\n",
    "    params = urllib.parse.urlencode(params_dict) #Parametros de la llamada\n",
    "    conn = http.client.HTTPConnection('ws.audioscrobbler.com') #URL de la API\n",
    "    conn.request('GET', '/2.0/', params, {}) #Llamada a la API\n",
    "    res = conn.getresponse() #Obtenemos la respuesta\n",
    "    body = json.loads(res.read().decode('utf-8')) #Leemos el body y lo convertimos en dict\n",
    "    conn.close() #Cerramos la conexion\n",
    "\n",
    "    #Extras\n",
    "    #headers = res.getheaders() #Leemos los headers\n",
    "    #code = res.getcode() #Leemos el codigo (200 Ok)\n",
    "    #reason = res.reason #Leemos la descripcion del codigo\n",
    "    tags = []\n",
    "    #Obtenemos únicamente los tags\n",
    "    i = 0\n",
    "    for d in body['toptags']['tag']:\n",
    "        if i == maxTags:\n",
    "            break;\n",
    "        tag = d['name']\n",
    "        tags.append(tag)\n",
    "        i+=1\n",
    "        \n",
    "    return tags\n",
    "\n",
    "RHCP_tags = artist_getTopTags('Red Hot Chili Peppers')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['rock', 'alternative rock', 'alternative', 'Funk Rock', 'funk', 'seen live', 'Red Hot Chili Peppers', 'punk', 'american', 'indie']\n"
     ]
    }
   ],
   "source": [
    "print(RHCP_tags)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "https://www.last.fm/api/show/artist.getInfo\n",
    "\n",
    "Get the metadata for an artist. Includes biography, truncated at 300 characters.\n",
    "\n",
    "**artist (Required (unless mbid)] :** The artist name  \n",
    "    **mbid (Optional) :** The musicbrainz id for the artist  \n",
    "    **lang (Optional) :** The language to return the biography in, expressed as an ISO 639 alpha-2 code.  \n",
    "    **autocorrect[0|1] (Optional) :** Transform misspelled artist names  into correct artist names, returning the correct version instead.   The corrected artist name will be returned in the response.  \n",
    "    **username (Optional) :** The username for the context of the request. If supplied, the user's playcount for this artist is included in the response.  \n",
    "**api_key (Required) :** A Last.fm API key.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def artist_getInfo(artist, api_key='3675d2cb82f8d59bcc885774869aca18', mbid=None, lang=None, autocorrect=None, username=None,  maxTags = 10):\n",
    "    import http.client,  urllib.parse, json\n",
    "\n",
    "    #Parametros necesarios\n",
    "    params_dict = {\n",
    "            'method':'artist.getInfo',\n",
    "            'artist' : artist,\n",
    "            'api_key' : api_key,\n",
    "            'format' : 'json'\n",
    "    }\n",
    "\n",
    "    #Parametros opcionales\n",
    "    if mbid:\n",
    "        params_dict['mbid'] = mbid\n",
    "    if lang:\n",
    "        params_dict['lang'] = lang\n",
    "    if autocorrect:\n",
    "        params_dict['autocorrect'] = autocorrect\n",
    "    if mbid:\n",
    "        params_dict['username'] = username\n",
    "    \n",
    "\n",
    "    params = urllib.parse.urlencode(params_dict) #Parametros de la llamada\n",
    "    conn = http.client.HTTPConnection('ws.audioscrobbler.com') #URL de la API\n",
    "    conn.request('GET', '/2.0/', params, {}) #Llamada a la API\n",
    "    res = conn.getresponse() #Obtenemos la respuesta\n",
    "    body = json.loads(res.read().decode('utf-8')) #Leemos el body y lo convertimos en dict\n",
    "    conn.close() #Cerramos la conexion\n",
    "\n",
    "    #Extras\n",
    "    #headers = res.getheaders() #Leemos los headers\n",
    "    #code = res.getcode() #Leemos el codigo (200 Ok)\n",
    "    #reason = res.reason #Leemos la descripcion del codigo\n",
    "    if 'artist' in body.keys():\n",
    "        listeners = int(body['artist']['stats']['listeners'])\n",
    "    else:\n",
    "        listeners = -1\n",
    "        \n",
    "    return listeners\n",
    "\n",
    "artist_getInfo('Artist Senju/Artist Senju')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(RHCP_info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dfToList = df['one'].tolist()\n",
    "\n",
    "dfList = list(df['one'])\n",
    "\n",
    "dfValues = df['one'].values\n",
    "\n",
    "print(\"dfToList is\", dfToList, \"and it's a\", type(dfToList))\n",
    "print(\"dfList is  \", dfList,   \"and it's a\", type(dfList))\n",
    "print(\"dfValues is\", dfValues, \"and it's a\", type(dfValues))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
