In my code below, I am trying to rerun a new list which is created by removing the first three list items that were already matched to a track in the giveHref() function. I attempt to run the new list through the decrementList() function, but I receive this error: generator object decrementList at 0x104cb8050. Why am I unable to run this new list through the function again?
I'm also having issues on having this done automatically until there are no more list items, so any advice/suggestions on this would be helpful as well. I would like this program to run once and return all HREFs for matching decremented list items that match to API tracks.
import requests
import json
# initial message
message = "if i can't let it go out of my mind"
# split into list
split_message = message.split()
# get lenth of original list
size = len(split_message)
def decrementList(words):
#decrement list by 1 for each try
for w in [ words ] + [ words[:-x] for x in range(1, len(words)) ]:
url = 'http://ws.spotify.com/search/1/track.json?q='
request = requests.get(url + "%20".join(w))
json_dict = json.loads(request.content)
num_results = json_dict['info']['num_results']
if num_results > 0:
num_removed = len(words) - len(w)
track_title = ' '.join(w)
# yield number of list items removed, possible track name that
# is comprised of list items, actual track names from API and new decremented list
# all through generator
for track in json_dict["tracks"]:
if track["name"].lower().startswith(track_title.lower()):
yield num_removed, track, track["name"], w
# finds and returns HREF of first match of track name made from decremented list and API track name
def giveHref():
for num_words_removed, track, track_name, w in decrementList(split_message):
if ' '.join(w) == track_name.lower():
return track['href'], num_words_removed
track['href'], num_words_removed = giveHref()
# creates new list with items from the previously matched track removed
# in this case, first three list items are removed, as they previously matched to a track
def newMessage():
new_message_length = size - num_words_removed
new_message = split_message[new_message_length:size]
return new_message
new_message = newMessage()
decrementList(new_message)