I wrote this function to retreive the scan result of a Virus Total scan.
async def check_virustotal_result(self, scan_id, message): url = f"https://www.virustotal.com/api/v3/analyses/{scan_id}" headers = {"x-apikey": VIRUSTOTAL_API_KEY,"accept": "application/json", } max_retries = 10 # Set a limit for retries retry_count = 0 async with aiohttp.ClientSession() as session: while retry_count < max_retries: async with session.get(url, headers=headers) as response: print(await response.json()) if response.status == 200: result = await response.json() status = result.get("data", {}).get("attributes", {}).get("status") if status == "completed": return result # Return the full result when the analysis is completed # Wait before checking again await asyncio.sleep(1) # Adjust as needed retry_count += 1 raise TimeoutError("VirusTotal analysis did not complete in a timely manner.")It works most of the time, however, sometimes it sends back an empty response:
{'data': {'id': 'TheID', 'type': 'analysis', 'links': {'self': 'https://www.virustotal.com/api/v3/analyses/TheID', 'item': 'https://www.virustotal.com/api/v3/files/00e9b5f2aba25bef484527b7efcbbd79b73f135abcfe03a8c23f25582c2e025f'}, 'attributes': {'results': {}, 'date': 1715326402, 'stats': {'malicious': 0, 'suspicious': 0, 'undetected': 0, 'harmless': 0, 'timeout': 0, 'confirmed-timeout': 0, 'failure': 0, 'type-unsupported': 0}, 'status': 'queued'}}, 'meta': {'file_info': {'sha256': '00e9b5f2aba25bef484527b7efcbbd79b73f135abcfe03a8c23f25582c2e025f', 'md5': '374363a9ab418654ceb547e341b4724d', 'sha1': 'a60a2f0057519715905280f61150d4c66a504156', 'size': 1473496}}}{'data': {'id': 'TheID', 'type': 'analysis', 'links': {'self': 'https://www.virustotal.com/api/v3/analyses/TheID', 'item': 'https://www.virustotal.com/api/v3/files/00e9b5f2aba25bef484527b7efcbbd79b73f135abcfe03a8c23f25582c2e025f'}, 'attributes': {'status': 'queued', 'date': 1715326402, 'stats': {'malicious': 0, 'suspicious': 0, 'undetected': 0, 'harmless': 0, 'timeout': 0, 'confirmed-timeout': 0, 'failure': 0, 'type-unsupported': 0}, 'results': {}}}, 'meta': {'file_info': {'sha256': '00e9b5f2aba25bef484527b7efcbbd79b73f135abcfe03a8c23f25582c2e025f', 'md5': '374363a9ab418654ceb547e341b4724d', 'sha1': 'a60a2f0057519715905280f61150d4c66a504156', 'size': 1473496}}}I thought this was due to rate limiting, but after a few hours, it still did it. It also continues to output the above (thats why I added a timeout) . Why is this happening?