|
1
|
|
|
"""The module describes the VirusTotalAPI base class |
|
2
|
|
|
|
|
3
|
|
|
Author: Evgeny Drobotun (c) 2019 |
|
4
|
|
|
License: MIT (https://github.com/drobotun/virustotalapi3/blob/master/LICENSE) |
|
5
|
|
|
|
|
6
|
|
|
More information: https://virustotalapi3.readthedocs.io/en/latest/base_class.html |
|
7
|
|
|
""" |
|
8
|
|
|
|
|
9
|
|
|
import requests |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class VirusTotalAPI: |
|
13
|
|
|
"""A base class for subclasses that implement methods for working with files, URLs, domain |
|
14
|
|
|
names, and IP addresses. |
|
15
|
|
|
|
|
16
|
|
|
Attributes: |
|
17
|
|
|
base_url: The base URL for sending requests (str). |
|
18
|
|
|
headers: Request header containing API key (dict). |
|
19
|
|
|
timeout: Server response timeout. A tuple that includes a timeout value for 'connect' and |
|
20
|
|
|
a timeout value for 'read'. If specify a single timeout value, it will be applied to |
|
21
|
|
|
both timeout 'connect' and timeout 'read'. |
|
22
|
|
|
proxies: The Protocol and the URL of the proxy server (dict). |
|
23
|
|
|
_version_api: VirusTotal API version (str). |
|
24
|
|
|
_last_http_error: HTTP status code of last operation (int). |
|
25
|
|
|
_last_result: Result of the last execution of a subclass method of this class. |
|
26
|
|
|
|
|
27
|
|
|
Constants: HTTP error codes constants. |
|
28
|
|
|
|
|
29
|
|
|
Methods: |
|
30
|
|
|
get_version_api(): Return the API version values. |
|
31
|
|
|
get_last_http_error(): Return the HTTP status code of last operation. |
|
32
|
|
|
get_last_result(): Return the result of executing methods of subclasses of this class. |
|
33
|
|
|
""" |
|
34
|
|
|
|
|
35
|
|
|
HTTP_OK = requests.codes['ok'] |
|
36
|
|
|
HTTP_BAD_REQUEST_ERROR = requests.codes['bad_request'] |
|
37
|
|
|
HTTP_AUTHENTICATION_REQUIRED_ERROR = requests.codes['unauthorized'] |
|
38
|
|
|
HTTP_FORBIDDEN_ERROR = requests.codes['forbidden'] |
|
39
|
|
|
HTTP_NOT_FOUND_ERROR = requests.codes['not_found'] |
|
40
|
|
|
HTTP_ALREADY_EXISTS_ERROR = requests.codes['conflict'] |
|
41
|
|
|
HTTP_QUOTA_EXCEEDED_ERROR = requests.codes['too_many_requests'] |
|
42
|
|
|
HTTP_TRANSIENT_ERROR = requests.codes['service_unavailable'] |
|
43
|
|
|
|
|
44
|
|
|
def __init__(self, api_key=None, timeout=None, proxies=None): |
|
45
|
|
|
"""Inits VirusTotalAPI. |
|
46
|
|
|
|
|
47
|
|
|
Args: |
|
48
|
|
|
api_key: your API key to access the functions of the service VirusTotal (str). |
|
49
|
|
|
timeout: Server response timeout (int). Optional. |
|
50
|
|
|
proxies: The Protocol and the URL of the proxy server (dict). Optional. |
|
51
|
|
|
""" |
|
52
|
|
|
self.base_url = 'https://www.virustotal.com/api/v3' |
|
53
|
|
|
self.headers = {'x-apikey' : api_key} |
|
54
|
|
|
self.timeout = timeout |
|
55
|
|
|
self.proxies = proxies |
|
56
|
|
|
self._version_api = 'version 3' |
|
57
|
|
|
self._last_http_error = None |
|
58
|
|
|
self._last_result = None |
|
59
|
|
|
|
|
60
|
|
|
def get_version_api(self): |
|
61
|
|
|
"""Return the API version values. |
|
62
|
|
|
|
|
63
|
|
|
Return: |
|
64
|
|
|
String containing API version ('version 3'). |
|
65
|
|
|
""" |
|
66
|
|
|
return self._version_api |
|
67
|
|
|
|
|
68
|
|
|
def get_last_http_error(self): |
|
69
|
|
|
"""Return the HTTP status code of last operation. |
|
70
|
|
|
|
|
71
|
|
|
Return: |
|
72
|
|
|
HTTP status code of last operation. |
|
73
|
|
|
""" |
|
74
|
|
|
return self._last_http_error |
|
75
|
|
|
|
|
76
|
|
|
def get_last_result(self): |
|
77
|
|
|
"""Return the result of executing methods of subclasses of this class. |
|
78
|
|
|
|
|
79
|
|
|
Return: |
|
80
|
|
|
Result of the last execution of a subclass method of this class. |
|
81
|
|
|
""" |
|
82
|
|
|
return self._last_result |
|
83
|
|
|
|