Search streams API causing a KeyError when looping over the "streams" list

I use the search streams API for League of Legends (just an example) and store the page.

Using Python, I iterate of each of the streams via a for loop:

	def grab_viewership_information(self,data):
	#compile data for current url
	try:
		for stream in data["streams"]:
			if stream["game"] == self.name:
				self.dictionary_of_viewers[stream["channel"]["display_name"]] = stream["viewers"]
			else:
				pass
		return self.dictionary_of_viewers
	except KeyError:
		print("STREAM ERRORRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR")
		return self.dictionary_of_viewers

I was initially not error handling and would recieve KeyErrors randomly, but frequently enough to have to do this.

Does anyone know why KeyErrors would return?

Are you testing the HTTP code response to make sure you are getting a 200 and the data you expect. You could be seeing a 5x code or a bad JSON packet and thus no parse as you didn’t get any data or malformed.

Also in some cases the display_name can be blank. For users that have not set a dispaly_name (the display_name being a user selected capitalisation of their name).

So I think thats your problem, you are not catering for blank/unset dispaly_name's

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.