| Total Complexity | 3 |
| Total Lines | 60 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | import gzip |
||
| 2 | import json |
||
| 3 | |||
| 4 | import requests |
||
| 5 | |||
| 6 | """ |
||
| 7 | Copyright (c) 2020 Star Inc.(https://starinc.xyz) |
||
| 8 | |||
| 9 | This Source Code Form is subject to the terms of the Mozilla Public |
||
| 10 | License, v. 2.0. If a copy of the MPL was not distributed with this |
||
| 11 | file, You can obtain one at http://mozilla.org/MPL/2.0/. |
||
| 12 | """ |
||
| 13 | |||
| 14 | |||
| 15 | class PhishTank: |
||
| 16 | """ |
||
| 17 | OpenDNS PhishTank Client |
||
| 18 | https://www.phishtank.com/ |
||
| 19 | """ |
||
| 20 | |||
| 21 | def __init__(self, username: str, api_key: str): |
||
| 22 | """ |
||
| 23 | Initialization |
||
| 24 | |||
| 25 | :param username: PhishTank Username |
||
| 26 | :param api_key: PhishTank API Key |
||
| 27 | """ |
||
| 28 | self.username = username |
||
| 29 | self.api_key = api_key |
||
| 30 | |||
| 31 | self.api_url = "https://checkurl.phishtank.com/checkurl/" |
||
| 32 | self.db_url = "http://data.phishtank.com/data/{}/online-valid.json.gz".format(api_key) |
||
| 33 | |||
| 34 | def lookup(self, url: str): |
||
| 35 | """ |
||
| 36 | To check URLs from PhishTank |
||
| 37 | |||
| 38 | :param url: URL |
||
| 39 | :return: dict |
||
| 40 | """ |
||
| 41 | header = {"user-agent": "phishtank/{}".format(self.username)} |
||
| 42 | |||
| 43 | query_data = { |
||
| 44 | "url": url, |
||
| 45 | "format": "json", |
||
| 46 | "app_key": self.api_key |
||
| 47 | } |
||
| 48 | |||
| 49 | return requests.post(self.api_url, headers=header, params=query_data) |
||
| 50 | |||
| 51 | def get_database(self): |
||
| 52 | """ |
||
| 53 | Get database from PhishTank |
||
| 54 | |||
| 55 | :return: dict |
||
| 56 | """ |
||
| 57 | data = requests.get(self.db_url) |
||
| 58 | decompressed_data = gzip.decompress(data.content) |
||
| 59 | return json.loads(decompressed_data) |
||
| 60 |