Skip to content

Commit

Permalink
Merge pull request #3 from valvesss/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
valvesss authored Aug 16, 2018
2 parents 338d686 + e5092ba commit 428ca37
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 31 deletions.
7 changes: 2 additions & 5 deletions vagalume_wrapper/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,13 @@

# Start connection to object with the given name and song
connection = vagalumewp.ApiRequest(name, song)

# Initialize constructors
connection.main()

# Give informations about the artist
print("Informations about {} !\n".format(name.title()))

# Print the number of music most acessed
print("Set a number to retrieve musics most acessed!")
pprint(connection.get_n_music_acessed(int(input("Number: "))))
print("Set a number to retrieve musics most acessed!", connection.get_n_music_acessed(int(input("Number: "))))

# Print the artist position in vagalume rank
print("\nArtist position in vagalume rank:", connection.get_artist_position())
Expand All @@ -31,4 +28,4 @@
print("\nArtist \"{}\" music most frequent word : {}".format(song,connection.get_frequent_word()))

# Print most frequent words for given last albums
# print("\nArtist \"{}\" music most frequent word : {}".format(song,connection.get_frequent_word()))
print("\nArtist {} albums most frequent word : {}".format(name.title(),connection.get_frequent_words()))
1 change: 0 additions & 1 deletion vagalume_wrapper/vagalume_objects.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import sys
class Song(object):
def __init__(self, song):
# Available keys: id, lang, name, text, url
Expand Down
70 changes: 45 additions & 25 deletions vagalume_wrapper/vagalumewp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import requests, os
import requests, os, sys
import vagalume_objects as vgo
from pprint import pprint

"""
Wrapper for vagalume lyrics search
Expand All @@ -13,30 +14,33 @@ def __init__(self, artist, song):

def main(self):
# API Urls
api_url_v1 = r'https://api.vagalume.com.br/search.php'
api_url_v2 = r'https://vagalume.com.br/' + self.artist + '/index.js'
self.api_url_v1 = r'https://api.vagalume.com.br/search.php'
self.api_url_v2 = r'https://vagalume.com.br/' + self.artist.replace(' ','-') + '/index.js'

# API Params
params = {
'art': self.artist,
'mus': self.song
'art': self.artist,
'mus': self.song
}

response1 = api_request(api_url_v1, params)
response1 = api_request(self.api_url_v1, params)
self.conn_song = vgo.Song(response1['mus'][0])
response2 = api_request(api_url_v2)

response2 = api_request(self.api_url_v2)
self.conn_artist = vgo.Artist(response2['artist'])

# Return most acessed musics by artist
def get_n_music_acessed(self, number):
lyrics = self.conn_artist.__dict__['toplyrics']['item']
if number == "all":
return lyrics
else:
if isinstance(number, int) and number >= 1:
return lyrics[:number]
# If zero, return all
if isinstance(number, int):
if number == 0:
return lyrics
else:
raise ValueError('Please, use a number higher than 0.')
self.acessed_lyrics = lyrics[:int(number)]
return self.acessed_lyrics
else:
raise ValueError('Please, use a number.')

def get_artist_position(self):
rank = self.conn_artist.__dict__['rank']
Expand All @@ -47,18 +51,34 @@ def get_artist_last_album(self):
return albums[0]

def get_frequent_word(self):
song = self.conn_song.__dict__['name']
song = self.conn_song.__dict__['text']
wordlist = song.split()

file = open('../stop-words/portuguese.txt', 'r')
stopword_br = file.readlines()
file.close()

stopword_br = get_stopword("portuguese")
result = list(set(wordlist) - set(stopword_br))
common = max(set(result), key=result.count)
return common

def get_frequent_words(self):
frequent_word = {}
stopword_br = get_stopword("portuguese")
params = {
'art': self.artist,
}
for item in self.acessed_lyrics:
params['mus'] = item['desc']
response = api_request(self.api_url_v1, params)
split_song = response['mus'][0]['text'].split()
result = list(set(split_song) - set(stopword_br))
frequent_word[item['desc']] = max(set(result), key=result.count)
return frequent_word

def api_request(url, params=None):
response = requests.get(url, params=params).json()
return check_response(response)

def check_response(response):
if not isinstance(response, dict):
raise ValueError('Artist not found! Try again.')

if 'type' in response.keys() and response['type'] == 'notfound':
raise ValueError('Artist not found! Try again.')
Expand All @@ -67,10 +87,10 @@ def check_response(response):
raise ValueError('Song not found! Try again.')

else:
return True
return response

def api_request(url, params=None):

response = requests.get(url, params=params).json()
check_response(response)
return response
def get_stopword(lang):
file = open('../stop-words/' + lang + '.txt', 'r')
stopword = file.readlines()
file.close()
return stopword

0 comments on commit 428ca37

Please sign in to comment.