0

I am trying to count the number of times the scan detects the file as being false. I tried looking at links such as Looking for a specific value in JSON file but it didn't solve my issue. In addition, I have not worked with API's that much so any help would be greatly appreciated.

This is what I have so far.

# Making the API call
url = 'https://www.virustotal.com/vtapi/v2/file/report'
params = {'apikey': api_key, 'resource': sha1_num}

response = requests.get(url, params=params)
allJson = response.json()
data = json.dumps(allJson)

count = 0

item_dict = json.loads(data)
print len(item_dict['scans']

for entry in data['scans']:
    if entry['detected'] == 'false':
        count += 1
print (count)

And, this is what the result for sublime.exe is on virustotal

{
  "scans": {
    "Bkav": {
      "detected": false,
      "version": "1.3.0.9466",
      "result": null,
      "update": "20180619"
    },
    "MicroWorld-eScan": {
      "detected": false,
      "version": "14.0.297.0",
      "result": null,
      "update": "20180619"
    },
    "CMC": {
      "detected": false,
      "version": "1.1.0.977",
      "result": null,
      "update": "20180619"
    },
    "CAT-QuickHeal": {
      "detected": false,
      "version": "14.00",
      "result": null,
      "update": "20180619"
    },
    "ALYac": {
      "detected": false,
      "version": "1.1.1.5",
      "result": null,
      "update": "20180619"
    },
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Singha22
  • 1
  • 2
  • 6
  • So, what's wrong with your current code? If you're getting an exception, post the full traceback. If you're getting an incorrect count or something, tell us what you expect and what you're getting instead. – Blckknght Jun 21 '18 at 19:43

1 Answers1

0

I'm pretty sure this loop: for entry in data['scans']: isn't going to do what you want. The data['scans'] that you're iterating on is a dictionary, and when you iterate on a dictionary you get its keys (which are the names of the different tests, it looks like, in your data). Your code expects to get a dictionary instead of a key string, so it's not going to work right.

Try iterating over the values of the dictionary, with for entry in data['scans'].values():. You probably also want to be testing for the Boolean value False, rather than the string 'false', as Python's json parser will have converted it for you (you could do if not entry['detected']:).

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • My new code segment is: `for entry in data['scans'].values(): if entry['detected'] == false: count += 1 print (count) ` The error is that it says that `line 33, in for entry in data['scans'].values(): TypeError: string indices must be integers` – Singha22 Jun 21 '18 at 20:07
  • Oh, `data` is a string. That's odd. You seem to be decoding your data into a dictionary (with `response.json()`), then re-encoding it (with `json.dumps`) and parsing it again (`json.loads`), only to go on and use the encoded version you had in the middle. Replace `data` in your loop with either `allJson` or `item_dict` (which should have the same contents). And perhaps consider getting rid of the excessive parsing and reparsing! – Blckknght Jun 21 '18 at 20:12
  • Ok, so I commented the `item_dict = json.loads(allJson)` and changed the for loop to be, `for entry in allJson['scans'].values(): if entry['detected'] == false: count += 1 print (count) `. However, I am getting the same error of `line 33, in for entry in allJson['scans'].values(): TypeError: string indices must be integers` Is there something I am not seeing. I tried to play around with it but still no success. – Singha22 Jun 21 '18 at 20:27
  • It sounds like you are still unsure which of your variables are strings and which are parsed dictionaries. I suggest printing out their `repr`s and maybe their `type`s, and making sure you're indexing and iterating on a dictionary. Stack Overflow isn't really a good place to get interactive debugging help. – Blckknght Jun 21 '18 at 21:44