Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
mxrch
GitHub Repository: mxrch/GHunt
Path: blob/master/ghunt/apis/calendar.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.calendar import Calendar, CalendarEvents
6
7
import httpx
8
9
from typing import *
10
import inspect
11
import json
12
from datetime import datetime, timezone
13
14
15
class CalendarHttp(GAPI):
16
def __init__(self, creds: GHuntCreds, headers: Dict[str, str] = {}):
17
super().__init__()
18
19
if not headers:
20
headers = gb.config.headers
21
22
base_headers = {}
23
24
headers = {**headers, **base_headers}
25
26
self.hostname = "clients6.google.com"
27
self.scheme = "https"
28
29
self._load_api(creds, headers)
30
31
async def get_calendar(self, as_client: httpx.AsyncClient, calendar_id: str) -> Tuple[bool, Calendar]:
32
endpoint = EndpointConfig(
33
name = inspect.currentframe().f_code.co_name,
34
verb = "GET",
35
data_type = None, # json, data or None
36
authentication_mode = "sapisidhash", # sapisidhash, cookies_only, oauth or None
37
require_key = "calendar", # key name, or None
38
)
39
self._load_endpoint(endpoint)
40
41
base_url = f"/calendar/v3/calendars/{calendar_id}"
42
43
req = await self._query(endpoint.name, as_client, base_url)
44
45
# Parsing
46
data = json.loads(req.text)
47
48
calendar = Calendar()
49
if "error" in data:
50
return False, calendar
51
52
calendar._scrape(data)
53
54
return True, calendar
55
56
async def get_events(self, as_client: httpx.AsyncClient, calendar_id: str, params_template="next_events",
57
time_min=datetime.today().replace(tzinfo=timezone.utc).isoformat(), max_results=250, page_token="") -> Tuple[bool, CalendarEvents]:
58
endpoint = EndpointConfig(
59
name = inspect.currentframe().f_code.co_name,
60
verb = "GET",
61
data_type = None, # json, data or None
62
authentication_mode = "sapisidhash", # sapisidhash, cookies_only, oauth or None
63
require_key = "calendar", # key name, or None
64
)
65
self._load_endpoint(endpoint)
66
67
base_url = f"/calendar/v3/calendars/{calendar_id}/events"
68
69
params_templates = {
70
"next_events": {
71
"calendarId": calendar_id,
72
"singleEvents": True,
73
"maxAttendees": 1,
74
"maxResults": max_results,
75
"timeMin": time_min # ISO Format
76
},
77
"from_beginning": {
78
"calendarId": calendar_id,
79
"singleEvents": True,
80
"maxAttendees": 1,
81
"maxResults": max_results
82
},
83
"max_from_beginning": {
84
"calendarId": calendar_id,
85
"singleEvents": True,
86
"maxAttendees": 1,
87
"maxResults": 2500 # Max
88
}
89
}
90
91
if not params_templates.get(params_template):
92
raise GHuntParamsTemplateError(f"The asked template {params_template} for the endpoint {endpoint.name} wasn't recognized by GHunt.")
93
94
params = params_templates[params_template]
95
if page_token:
96
params["pageToken"] = page_token
97
98
req = await self._query(endpoint.name, as_client, base_url, params=params)
99
100
# Parsing
101
data = json.loads(req.text)
102
103
events = CalendarEvents()
104
if not data:
105
return False, events
106
107
events._scrape(data)
108
109
return True, events
110