Fix broken DFC support
This commit is contained in:
		
							parent
							
								
									5375a36c52
								
							
						
					
					
						commit
						ec8ffed27b
					
				
							
								
								
									
										5
									
								
								face.py
								
								
								
								
							
							
						
						
									
										5
									
								
								face.py
								
								
								
								
							|  | @ -32,3 +32,8 @@ class Face: | ||||||
| 
 | 
 | ||||||
| 	def image_uris(self, _, layout): | 	def image_uris(self, _, layout): | ||||||
| 		return self.d['image_uris'][layout] | 		return self.d['image_uris'][layout] | ||||||
|  | 	 | ||||||
|  | 	def card_faces(self): | ||||||
|  | 		# Scrython exposes this method on every card and manually raises KeyError | ||||||
|  | 		# if it's not a DFC, so unfortunately I have to replicate that here. | ||||||
|  | 		raise KeyError() | ||||||
|  | @ -45,7 +45,7 @@ async def startup(): | ||||||
| 			await t | 			await t | ||||||
| 
 | 
 | ||||||
| async def get_cards(card_names): | async def get_cards(card_names): | ||||||
| 	async def get_card_image(session, c): | 	async def get_card_image(session, c, get_oracle=True): | ||||||
| 		"""Return card image and description (text representation)""" | 		"""Return card image and description (text representation)""" | ||||||
| 
 | 
 | ||||||
| 		async def download_card_image(session, c): | 		async def download_card_image(session, c): | ||||||
|  | @ -72,8 +72,16 @@ async def get_cards(card_names): | ||||||
| 				return text | 				return text | ||||||
| 		 | 		 | ||||||
| 		try: | 		try: | ||||||
|  | 			# Scrython exposes this method for every card and just manually raises a KeyError if it's not | ||||||
|  | 			# a DFC, so this whole section has to be wrapped in a try-catch to deal with it | ||||||
|  | 			c.card_faces() | ||||||
|  | 
 | ||||||
|  | 			log(f'{c.name()} is a DFC, getting each face separately...') | ||||||
|  | 
 | ||||||
|  | 			oracle_text = download_card_text(session, c) | ||||||
|  | 
 | ||||||
| 			front, back = map(Image.open, await asyncio.gather( | 			front, back = map(Image.open, await asyncio.gather( | ||||||
| 				*(get_card_image(session, face.Face(card_face)) for card_face in c.card_faces()) | 				*(get_card_image(session, face.Face(card_face), False) for card_face in c.card_faces()) | ||||||
| 			)) | 			)) | ||||||
| 			 | 			 | ||||||
| 			new_image = Image.new('RGB', (front.width*2, front.height)) | 			new_image = Image.new('RGB', (front.width*2, front.height)) | ||||||
|  | @ -85,15 +93,17 @@ async def get_cards(card_names): | ||||||
| 			new_image.save(output, format=front.format) | 			new_image.save(output, format=front.format) | ||||||
| 			output.seek(0) | 			output.seek(0) | ||||||
| 			 | 			 | ||||||
| 			return output | 			return (output, await oracle_text) | ||||||
| 			 | 		except KeyError: | ||||||
| 		except (AttributeError, KeyError): |  | ||||||
| 			pass | 			pass | ||||||
| 		 | 		 | ||||||
|  | 		if get_oracle: | ||||||
| 			return await asyncio.gather( | 			return await asyncio.gather( | ||||||
| 				download_card_image(session, c), | 				download_card_image(session, c), | ||||||
| 				download_card_text(session, c) | 				download_card_text(session, c) | ||||||
| 			) | 			) | ||||||
|  | 		else: | ||||||
|  | 			return await download_card_image(session, c) | ||||||
| 
 | 
 | ||||||
| 	# Responses list: One entry for each [[card name]] in parent, even if the | 	# Responses list: One entry for each [[card name]] in parent, even if the | ||||||
| 	# response is just "No card named 'CARDNAME' was found." | 	# response is just "No card named 'CARDNAME' was found." | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue