GoogleSafeBrowsing.get_database()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 32
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 32
rs 9.45
c 0
b 0
f 0
cc 1
nop 1
1
import requests
2
3
"""
4
    Copyright (c) 2020 Star Inc.(https://starinc.xyz)
5
6
    This Source Code Form is subject to the terms of the Mozilla Public
7
    License, v. 2.0. If a copy of the MPL was not distributed with this
8
    file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
"""
10
11
12
class GoogleSafeBrowsing:
13
    """
14
    Google Safe Browsing Client
15
    https://safebrowsing.google.com/
16
    """
17
18
    def __init__(self, google_api_key: str):
19
        """
20
        Initialization
21
22
        :param google_api_key: Google API Token
23
        """
24
        host = "safebrowsing.googleapis.com"
25
26
        api_ver = "v4"
27
28
        lookup_path = "threatMatches:find"
29
        update_path = "threatListUpdates:fetch"
30
31
        self.lookup_url = "https://{}/{}/{}?key={}".format(host, api_ver, lookup_path, google_api_key)
32
        self.update_url = "https://{}/{}/{}?key={}".format(host, api_ver, update_path, google_api_key)
33
34
    def lookup(self, urls: list):
35
        """
36
        To check URLs from Google Safe Browsing
37
38
        :param urls: list of URLs
39
        :return: dict
40
        """
41
        query_urls = [{"url": url} for url in urls]
42
43
        query_data = {
44
            "client": {
45
                "clientId": "Phishing Blocker Project - Analytics",
46
                "clientVersion": "0.1"
47
            },
48
            "threatInfo": {
49
                "threatTypes": ["MALWARE", "SOCIAL_ENGINEERING"],
50
                "platformTypes": ["ANY_PLATFORM"],
51
                "threatEntryTypes": ["URL"],
52
                "threatEntries": query_urls
53
            }
54
        }
55
56
        return requests.post(self.lookup_url, json=query_data).json()
57
58
    def get_database(self):
59
        """
60
        Get database from Google Safe Browsing
61
62
        :return: dict
63
        """
64
65
        def _request(request_type: str):
66
            return {
67
                "threatType": request_type,
68
                "platformType": "ANY_PLATFORM",
69
                "threatEntryType": "URL",
70
                "constraints": {
71
                    "maxUpdateEntries": 2048,
72
                    "maxDatabaseEntries": 4096,
73
                    "region": "TW",
74
                    "supportedCompressions": ["RAW"]
75
                }
76
            }
77
78
        query_data = {
79
            "client": {
80
                "clientId": "Phishing Blocker Project - Analytics",
81
                "clientVersion": "0.1"
82
            },
83
            "listUpdateRequests": [
84
                _request("MALWARE"),
85
                _request("SOCIAL_ENGINEERING")
86
            ]
87
        }
88
89
        return requests.post(self.update_url, json=query_data).json()
90