Code Duplication    Length = 198-198 lines in 2 locations

vtapi3/vtapi3.py 2 locations

@@ 1000-1197 (lines=198) @@
997
            return response.content
998
999
1000
class VirusTotalAPIIPAddresses(VirusTotalAPI):
1001
    """The retrieving information about any IP addresses from the VirusTotal database methods are
1002
       defined in the class.
1003
1004
       Methods:
1005
          get_report(): Retrieve information about an IP address.
1006
          get_comments(): Retrieve comments for an IP address.
1007
          put_comments(): Add a comment to an IP address.
1008
          get_relationship(): Retrieve objects related to an IP address.
1009
          get_votes(): Retrieve votes for an IP address.
1010
          put_votes(): Add a vote for an IP address.
1011
    """
1012
1013
    def get_report(self, ip_address):
1014
        """Retrieve information about an IP address.
1015
1016
        Args:
1017
           ip_address: IP address (str).
1018
1019
        Return:
1020
           The response from the server as a byte sequence.
1021
1022
        Exception
1023
           VirusTotalAPIError(Connection error): In case of server connection errors.
1024
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
1025
        """
1026
        self._last_http_error = None
1027
        self._last_result = None
1028
        api_url = self.base_url + '/ip_addresses/' + ip_address
1029
        try:
1030
            response = requests.get(api_url, headers=self.headers,
1031
                                    timeout=self.timeout, proxies=self.proxies)
1032
        except requests.exceptions.Timeout:
1033
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
1034
        except requests.exceptions.ConnectionError:
1035
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
1036
        else:
1037
            self._last_http_error = response.status_code
1038
            self._last_result = response.content
1039
            return response.content
1040
1041
    def get_comments(self, ip_address, limit=10, cursor='""'):
1042
        """Retrieve comments for an IP address.
1043
1044
        Args:
1045
           ip_address: IP address (str).
1046
           limit: Maximum number of comments to retrieve (int). The default value is 10.
1047
           cursor: Continuation cursor (str). The default value is ''.
1048
1049
        Return:
1050
           The response from the server as a byte sequence.
1051
1052
        Exception
1053
           VirusTotalAPIError(Connection error): In case of server connection errors.
1054
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
1055
        """
1056
        self._last_http_error = None
1057
        self._last_result = None
1058
        query_string = {'limit': str(limit), 'cursor': cursor}
1059
        api_url = self.base_url + '/ip_addresses/' + ip_address + '/comments'
1060
        try:
1061
            response = requests.get(api_url, headers=self.headers, params=query_string,
1062
                                    timeout=self.timeout, proxies=self.proxies)
1063
        except requests.exceptions.Timeout:
1064
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
1065
        except requests.exceptions.ConnectionError:
1066
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
1067
        else:
1068
            self._last_http_error = response.status_code
1069
            self._last_result = response.content
1070
            return response.content
1071
1072
    def put_comments(self, ip_address, text):
1073
        """Add a comment to an IP address.
1074
1075
        Args:
1076
           ip_address: IP address (str).
1077
           text: Text of the comment (str).
1078
1079
        Return:
1080
           The response from the server as a byte sequence.
1081
1082
        Exception
1083
           VirusTotalAPIError(Connection error): In case of server connection errors.
1084
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
1085
        """
1086
        self._last_http_error = None
1087
        self._last_result = None
1088
        comments = {"data": {'type': 'comment', 'attributes': {'text': text}}}
1089
        api_url = self.base_url + '/ip_addresses/' + ip_address + '/comments'
1090
        try:
1091
            response = requests.post(api_url, headers=self.headers, json=comments,
1092
                                     timeout=self.timeout, proxies=self.proxies)
1093
        except requests.exceptions.Timeout:
1094
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
1095
        except requests.exceptions.ConnectionError:
1096
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
1097
        else:
1098
            self._last_http_error = response.status_code
1099
            self._last_result = response.content
1100
            return response.content
1101
1102
    def get_relationship(self, ip_address, relationship='/resolutions', limit=10, cursor='""'):
1103
        """Retrieve objects related to an IP address.
1104
1105
        Args:
1106
           ip_address: IP address (str).
1107
           relationship: Relationship name (str). The default value is '/resolutions'.
1108
           limit: Maximum number of comments to retrieve (int). The default value is 10.
1109
           cursor: Continuation cursor (str). The default value is ''.
1110
1111
        Return:
1112
           The response from the server as a byte sequence.
1113
1114
        Exception
1115
           VirusTotalAPIError(Connection error): In case of server connection errors.
1116
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
1117
        """
1118
        self._last_http_error = None
1119
        self._last_result = None
1120
        query_string = {'limit': str(limit), 'cursor': cursor}
1121
        api_url = self.base_url + '/ip_addresses/' + ip_address + relationship
1122
        try:
1123
            response = requests.get(api_url, headers=self.headers, params=query_string,
1124
                                    timeout=self.timeout, proxies=self.proxies)
1125
        except requests.exceptions.Timeout:
1126
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
1127
        except requests.exceptions.ConnectionError:
1128
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
1129
        else:
1130
            self._last_http_error = response.status_code
1131
            self._last_result = response.content
1132
            return response.content
1133
1134
    def get_votes(self, ip_address, limit=10, cursor='""'):
1135
        """Retrieve votes for an IP address.
1136
1137
        Args:
1138
           domain: Domain name (str).
1139
           limit: Maximum number of comments to retrieve (int). The default value is 10.
1140
           cursor: Continuation cursor (str). The default value is ''.
1141
1142
        Return:
1143
           The response from the server as a byte sequence.
1144
1145
        Exception
1146
           VirusTotalAPIError(Connection error): In case of server connection errors.
1147
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
1148
        """
1149
        self._last_http_error = None
1150
        self._last_result = None
1151
        query_string = {'limit': str(limit), 'cursor': cursor}
1152
        api_url = self.base_url + '/ip_addresses/' + ip_address + '/votes'
1153
        try:
1154
            response = requests.get(api_url, headers=self.headers, params=query_string,
1155
                                    timeout=self.timeout, proxies=self.proxies)
1156
        except requests.exceptions.Timeout:
1157
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
1158
        except requests.exceptions.ConnectionError:
1159
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
1160
        else:
1161
            self._last_http_error = response.status_code
1162
            self._last_result = response.content
1163
            return response.content
1164
1165
    def put_votes(self, ip_address, malicious=False):
1166
        """Add a vote for an IP address.
1167
1168
        Args:
1169
           domain: IP address (str).
1170
           malicious: Determines a malicious (True) or harmless (False) domain (bool).
1171
1172
        Return:
1173
           The response from the server as a byte sequence.
1174
1175
        Exception
1176
           VirusTotalAPIError(Connection error): In case of server connection errors.
1177
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
1178
        """
1179
        self._last_http_error = None
1180
        self._last_result = None
1181
        if malicious:
1182
            verdict = 'malicious'
1183
        else:
1184
            verdict = 'harmless'
1185
        votes = {'data': {'type': 'vote', 'attributes': {'verdict': verdict}}}
1186
        api_url = self.base_url + '/ip_addresses/' + ip_address + '/votes'
1187
        try:
1188
            response = requests.post(api_url, headers=self.headers, json=votes,
1189
                                     timeout=self.timeout, proxies=self.proxies)
1190
        except requests.exceptions.Timeout:
1191
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
1192
        except requests.exceptions.ConnectionError:
1193
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
1194
        else:
1195
            self._last_http_error = response.status_code
1196
            self._last_result = response.content
1197
            return response.content
1198
1199
1200
class VirusTotalAPIAnalyses(VirusTotalAPI):
@@ 800-997 (lines=198) @@
797
            return response.content
798
799
800
class VirusTotalAPIDomains(VirusTotalAPI):
801
    """The retrieving information about any domain from the VirusTotal database methods are defined
802
       in the class.
803
804
       Methods:
805
          get_report(): Retrieve information about an Internet domain.
806
          get_comments(): Retrieve comments for an Internet domain.
807
          put_comments(): Add a comment to an Internet domain.
808
          get_relationship(): Retrieve objects related to an Internet domain.
809
          get_votes(): Retrieve votes for a hostname or domain.
810
          put_votes(): Add a vote for a hostname or domain.
811
    """
812
813
    def get_report(self, domain):
814
        """Retrieve information about an Internet domain.
815
816
        Args:
817
           domain: Domain name (str).
818
819
        Return:
820
           The response from the server as a byte sequence.
821
822
        Exception
823
           VirusTotalAPIError(Connection error): In case of server connection errors.
824
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
825
        """
826
        self._last_http_error = None
827
        self._last_result = None
828
        api_url = self.base_url + '/domains/' + domain
829
        try:
830
            response = requests.get(api_url, headers=self.headers,
831
                                    timeout=self.timeout, proxies=self.proxies)
832
        except requests.exceptions.Timeout:
833
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
834
        except requests.exceptions.ConnectionError:
835
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
836
        else:
837
            self._last_http_error = response.status_code
838
            self._last_result = response.content
839
            return response.content
840
841
    def get_comments(self, domain, limit=10, cursor='""'):
842
        """Retrieve comments for an Internet domain.
843
844
        Args:
845
           domain: Domain name (str).
846
           limit: Maximum number of comments to retrieve (int). The default value is 10.
847
           cursor: Continuation cursor (str). The default value is ''.
848
849
        Return:
850
           The response from the server as a byte sequence.
851
852
        Exception
853
           VirusTotalAPIError(Connection error): In case of server connection errors.
854
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
855
        """
856
        self._last_http_error = None
857
        self._last_result = None
858
        query_string = {'limit': str(limit), 'cursor': cursor}
859
        api_url = self.base_url + '/domains/' + domain + '/comments'
860
        try:
861
            response = requests.get(api_url, headers=self.headers, params=query_string,
862
                                    timeout=self.timeout, proxies=self.proxies)
863
        except requests.exceptions.Timeout:
864
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
865
        except requests.exceptions.ConnectionError:
866
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
867
        else:
868
            self._last_http_error = response.status_code
869
            self._last_result = response.content
870
            return response.content
871
872
    def put_comments(self, domain, text):
873
        """Add a comment to an Internet domain.
874
875
        Args:
876
           domain: Domain name (str).
877
           text: Text of the comment (str).
878
879
        Return:
880
           The response from the server as a byte sequence.
881
882
        Exception
883
           VirusTotalAPIError(Connection error): In case of server connection errors.
884
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
885
        """
886
        self._last_http_error = None
887
        self._last_result = None
888
        comments = {"data": {'type': 'comment', 'attributes': {'text': text}}}
889
        api_url = self.base_url + '/domains/' + domain + '/comments'
890
        try:
891
            response = requests.post(api_url, headers=self.headers, json=comments,
892
                                     timeout=self.timeout, proxies=self.proxies)
893
        except requests.exceptions.Timeout:
894
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
895
        except requests.exceptions.ConnectionError:
896
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
897
        else:
898
            self._last_http_error = response.status_code
899
            self._last_result = response.content
900
            return response.content
901
902
    def get_relationship(self, domain, relationship='/resolutions', limit=10, cursor='""'):
903
        """Retrieve objects related to an Internet domain.
904
905
        Args:
906
           domain: Domain name (str).
907
           relationship: Relationship name (str). The default value is '/resolutions'.
908
           limit: Maximum number of comments to retrieve (int). The default value is 10.
909
           cursor: Continuation cursor (str). The default value is ''.
910
911
        Return:
912
           The response from the server as a byte sequence.
913
914
        Exception
915
           VirusTotalAPIError(Connection error): In case of server connection errors.
916
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
917
        """
918
        self._last_http_error = None
919
        self._last_result = None
920
        query_string = {'limit': str(limit), 'cursor': cursor}
921
        api_url = self.base_url + '/domains/' + domain + relationship
922
        try:
923
            response = requests.get(api_url, headers=self.headers, params=query_string,
924
                                    timeout=self.timeout, proxies=self.proxies)
925
        except requests.exceptions.Timeout:
926
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
927
        except requests.exceptions.ConnectionError:
928
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
929
        else:
930
            self._last_http_error = response.status_code
931
            self._last_result = response.content
932
            return response.content
933
934
    def get_votes(self, domain, limit=10, cursor='""'):
935
        """Retrieve votes for a hostname or domain.
936
937
        Args:
938
           domain: Domain name (str).
939
           limit: Maximum number of comments to retrieve (int). The default value is 10.
940
           cursor: Continuation cursor (str). The default value is ''.
941
942
        Return:
943
           The response from the server as a byte sequence.
944
945
        Exception
946
           VirusTotalAPIError(Connection error): In case of server connection errors.
947
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
948
        """
949
        self._last_http_error = None
950
        self._last_result = None
951
        query_string = {'limit': str(limit), 'cursor': cursor}
952
        api_url = self.base_url + '/domains/' + domain + '/votes'
953
        try:
954
            response = requests.get(api_url, headers=self.headers, params=query_string,
955
                                    timeout=self.timeout, proxies=self.proxies)
956
        except requests.exceptions.Timeout:
957
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
958
        except requests.exceptions.ConnectionError:
959
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
960
        else:
961
            self._last_http_error = response.status_code
962
            self._last_result = response.content
963
            return response.content
964
965
    def put_votes(self, domain, malicious=False):
966
        """Add a vote for a hostname or domain.
967
968
        Args:
969
           domain: Domain name (str).
970
           malicious: Determines a malicious (True) or harmless (False) domain (bool).
971
972
        Return:
973
           The response from the server as a byte sequence.
974
975
        Exception
976
           VirusTotalAPIError(Connection error): In case of server connection errors.
977
           VirusTotalAPIError(Timeout error): If the response timeout from the server is exceeded.
978
        """
979
        self._last_http_error = None
980
        self._last_result = None
981
        if malicious:
982
            verdict = 'malicious'
983
        else:
984
            verdict = 'harmless'
985
        votes = {'data': {'type': 'vote', 'attributes': {'verdict': verdict}}}
986
        api_url = self.base_url + '/domains/' + domain + '/votes'
987
        try:
988
            response = requests.post(api_url, headers=self.headers, json=votes,
989
                                     timeout=self.timeout, proxies=self.proxies)
990
        except requests.exceptions.Timeout:
991
            raise VirusTotalAPIError('Timeout error', errno.ETIMEDOUT)
992
        except requests.exceptions.ConnectionError:
993
            raise VirusTotalAPIError('Connection error', errno.ECONNABORTED)
994
        else:
995
            self._last_http_error = response.status_code
996
            self._last_result = response.content
997
            return response.content
998
999
1000
class VirusTotalAPIIPAddresses(VirusTotalAPI):