from urllib.request import urlopen
from bs4 import BeautifulSoup
import re
import pandas as pd
import collections as clt

def get_songs(artist):

    url_site = "https://www.cifraclub.com.br{0}"
    artist = "/"+"-".join( artist.split(" ") ).lower()
    url_artist = url_site.format(artist)
    
    html = urlopen(url_artist).read()
    bsObj = BeautifulSoup(html, "html.parser")
    song_list = bsObj.find("ul", {"class":"list-links art_musics alf all"})
    
    songs_dict = {"song_name":[],
                  "url_note":[]}
    
    for song in song_list.children:

        title = song.find("a").attrs["title"]
        songs_dict["song_name"].append(title)
        
        note = song.find("a",{"data-title":re.compile("^[0-9]* vers(ão|ões) de violão e guitarra")})
        if note != None:
            url_note = note.attrs["href"]
            songs_dict["url_note"].append(url_site.format(url_note))
        else:
            songs_dict["url_note"].append(None)
        
    return pd.DataFrame(songs_dict)


def get_note(url_song):
    html = urlopen(url_song).read()
    bsObj = BeautifulSoup(html, "html.parser")

    note = bsObj.find("pre")
    notes = [note.string for note in note.find_all("b")]

    return  notes

def get_all_song_notes(df):
    
    notes_list = []

    for i in df.index:
        song_name, url = df.iloc[i]
        if url == None:
            pass
        else:
            song_note = get_note( url )
            note_series = pd.Series( clt.Counter(song_note), name=song_name )
            note_df = note_series.to_frame().T
            notes_list.append( note_df )
    
    notes_df = pd.concat( notes_list ).reset_index()
    notes_df.rename(columns={"index":"song_name"}, inplace=True)
    notes_df.fillna(0, inplace=True)

    return notes_df