Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mxrch
GitHub Repository: mxrch/GHunt
Path: blob/master/ghunt/apis/geolocation.py
252 views
1
from ghunt.objects.base import GHuntCreds
2
from ghunt.errors import *
3
import ghunt.globals as gb
4
from ghunt.objects.apis import GAPI, EndpointConfig
5
from ghunt.parsers.geolocate import GeolocationResponse
6
7
import httpx
8
9
from typing import *
10
import inspect
11
import json
12
13
14
class GeolocationHttp(GAPI):
15
def __init__(self, creds: GHuntCreds, headers: Dict[str, str] = {}):
16
super().__init__()
17
18
if not headers:
19
headers = gb.config.headers
20
21
base_headers = {}
22
23
headers = {**headers, **base_headers}
24
25
self.hostname = "www.googleapis.com"
26
self.scheme = "https"
27
28
self.authentication_mode = None # sapisidhash, cookies_only, oauth or None
29
self.require_key = "geolocation" # key name, or None
30
31
self._load_api(creds, headers)
32
33
async def geolocate(self, as_client: httpx.AsyncClient, bssid: str, body: dict) -> Tuple[bool, GeolocationResponse]:
34
endpoint = EndpointConfig(
35
name = inspect.currentframe().f_code.co_name,
36
verb = "POST",
37
data_type = "json", # json, data or None
38
authentication_mode = None, # sapisidhash, cookies_only, oauth or None
39
require_key = "geolocation", # key name, or None
40
)
41
self._load_endpoint(endpoint)
42
43
base_url = f"/geolocation/v1/geolocate"
44
45
if bssid:
46
payload = {
47
"considerIp": False,
48
"wifiAccessPoints": [
49
{
50
"macAddress": "00:25:9c:cf:1c:ad"
51
},
52
{
53
"macAddress": bssid
54
},
55
]
56
}
57
else:
58
payload = body
59
60
req = await self._query(endpoint.name, as_client, base_url, data=payload)
61
62
# Parsing
63
data = json.loads(req.text)
64
65
resp = GeolocationResponse()
66
if "error" in data:
67
return False, resp
68
69
resp._scrape(data)
70
71
return True, resp
72