<commit_before># Copyright (C) 2010-2012 Cuckoo Sandbox Developers.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.

import os
import json
import urllib
import urllib2

from lib.cuckoo.common.utils import File
from lib.cuckoo.common.abstracts import Processing
from lib.cuckoo.common.exceptions import CuckooProcessingError

VIRUSTOTAL_URL = "https://www.virustotal.com/vtapi/v2/file/report"
VIRUSTOTAL_KEY = ""

class VirusTotal(Processing):
    def run(self):
        self.key = "virustotal"
        virustotal = []

        if not os.path.exists(self.file_path):
            raise CuckooProcessingError("File %s not found. Skipping." % self.file_path)

        if not VIRUSTOTAL_KEY:
            raise CuckooProcessingError("API key not configured. Skipping.")

        try:
            md5 = File(self.file_path).get_md5()
        except IOError as e:
            raise CuckooProcessingError("Unable to open %s: %s" % (self.file_path, e.message))
        parameters = {"resource" : md5, "apikey" : VIRUSTOTAL_KEY}
        data = urllib.urlencode(parameters)
        try:
            req = urllib2.Request(VIRUSTOTAL_URL, data)
            response = urllib2.urlopen(req)
        except urllib2.HTTPError as e:
            raise CuckooProcessingError("Error in request to %s: HTTP error code %s" %(VIRUSTOTAL_URL, e.code))
        virustotal = json.loads(response.read())

        return virustotal
<commit_msg>Polished code<commit_after># Copyright (C) 2010-2012 Cuckoo Sandbox Developers.
# This file is part of Cuckoo Sandbox - http://www.cuckoosandbox.org
# See the file 'docs/LICENSE' for copying permission.

import os
import json
import urllib
import urllib2

from lib.cuckoo.common.utils import File
from lib.cuckoo.common.abstracts import Processing
from lib.cuckoo.common.exceptions import CuckooProcessingError

VIRUSTOTAL_URL = "https://www.virustotal.com/vtapi/v2/file/report"
VIRUSTOTAL_KEY = ""

class VirusTotal(Processing):
    def run(self):
        self.key = "virustotal"
        virustotal = []

        if not os.path.exists(self.file_path):
            raise CuckooProcessingError("File %s not found, skip" % self.file_path)

        if not VIRUSTOTAL_KEY:
            raise CuckooProcessingError("VirusTotal API key not configured, skip")

        try:
            md5 = File(self.file_path).get_md5()
        except IOError as e:
            raise CuckooProcessingError("Unable to open \"%s\": %s" % (self.file_path, e.message))

        data = urllib.urlencode({"resource" : md5, "apikey" : VIRUSTOTAL_KEY})

        try:
            req = urllib2.Request(VIRUSTOTAL_URL, data)
            response = urllib2.urlopen(req)
            virustotal = json.loads(response.read())
        except urllib2.HTTPError as e:
            raise CuckooProcessingError("Failed performing request to VirusTotal (http code=%s)" % e.code)

        return virustotal

{'fraction_non_alphanumeric': 0.09857723577235772, 'fraction_numerical': 0.01084010840108401, 'mean_word_length': 2.9584450402144773, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '224350', 'n_tokens_mistral': 974, 'n_tokens_neox': 937, 'n_words': 235}