-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsupercluster.py
50 lines (39 loc) · 1.93 KB
/
supercluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from typing import List
import requests
import os
from datetime import datetime
from logger import Logger
from dotenv import load_dotenv
load_dotenv()
# Set up logging
logging = Logger.setup_logger(__name__)
# Retrieve environment variables
SANITY_API_URL = os.getenv('SANITY_API_URL')
TODAY_DATE = datetime.utcnow().strftime("%Y-%m-%d")
def pull_from_supercluster(already_pushed: List[str]):
logging.info("Starting pull from Supercluster")
query = f'\n*[\n _type == "launch"\n && !(_id in path("drafts.**"))\n && launchInfo.launchDate.utc match "{TODAY_DATE}*"\n] | order(launchInfo.launchDate.utc desc) {{\n ...\n}}\n'
# query = f'\n*[\n _type == "launch"\n && !(_id in path("drafts.**"))\n && launchInfo.launchDate.utc match "2024*"\n] | order(launchInfo.launchDate.utc desc) {{\n ...\n}}\n'
params = {'query': query}
supercluster_queue = []
try:
logging.info(f"Sending request to SANITY API at {SANITY_API_URL} with params: {params}")
response = requests.get(SANITY_API_URL, params=params)
response.raise_for_status()
logging.info("Successfully received response from SANITY API")
response_json = response.json()
for launch in response_json["result"]:
if launch["_id"] in already_pushed:
logging.info(f"Skipping already pushed launch with ID: {launch['_id']}")
continue
logging.info(f"Adding launch with headline: {launch['headline']}")
supercluster_queue.append(f"{launch['headline']}")
already_pushed.append(launch["_id"])
logging.info(f"Number of launches added to the queue: {len(supercluster_queue)}")
return supercluster_queue
except requests.exceptions.RequestException as req_err:
logging.error(f"Request error: {req_err}")
return supercluster_queue
except Exception as e:
logging.error(f"An error occurred: {e}")
return supercluster_queue