-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
894efac
commit 51956c9
Showing
10 changed files
with
184 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,75 @@ | ||
from datetime import datetime | ||
|
||
import boto3 | ||
|
||
from scheduler import gmail, bitpoll, scheduler | ||
from scheduler.dynamodb import poll_table, email_table | ||
from scheduler.dynamodb.poll_table import PollItem | ||
|
||
|
||
def lambda_handler(event, context): | ||
print(event) | ||
print(event) | ||
dynamodb = boto3.resource("dynamodb") | ||
poll_item = poll_table.load(dynamodb) | ||
if is_poll_running(poll_item): | ||
schedule_next_schafkopf_event(dynamodb, poll_item.running_poll_id) | ||
return | ||
|
||
new_poll_item = start_new_poll(dynamodb) | ||
print("Store new poll item:", new_poll_item) | ||
poll_table.update(dynamodb, new_poll_item) | ||
|
||
|
||
def is_poll_running(item: PollItem) -> bool: | ||
return item.running_poll_id and datetime.now() < item.start_next_poll_date | ||
|
||
|
||
def start_new_poll(dynamodb) -> PollItem: | ||
print("Start a new poll") | ||
print("Generate csrf token") | ||
csrf_token = bitpoll.get_valid_csrf_token() | ||
print("csrf token:", csrf_token) | ||
|
||
print("Create new poll") | ||
poll_id = bitpoll.create_new_poll(csrf_token=csrf_token) | ||
print("New poll created") | ||
|
||
print("Generate dates to vote on") | ||
dates = scheduler.generate_working_days_for_next_weeks(weeks=2) | ||
|
||
print("Add dates to poll as choices") | ||
bitpoll.add_choices_to_poll(poll_id=poll_id, csrf_token=csrf_token, dates=dates) | ||
new_poll_website = bitpoll.get_website_from_poll_id(poll_id) | ||
print("Poll created:", new_poll_website) | ||
|
||
print("Send out email notifications") | ||
gmail.send_bitpoll_invitation( | ||
receivers=email_table.load_all_mails(dynamodb), | ||
bitpoll_link=new_poll_website | ||
) | ||
return PollItem( | ||
running_poll_id=poll_id, | ||
start_next_poll_date=max(dates) | ||
) | ||
|
||
|
||
def schedule_next_schafkopf_event(dynamodb, poll_id: str): | ||
poll_website = bitpoll.get_website_from_poll_id(poll_id) | ||
print("Try to schedule next schafkopf event for:", poll_website) | ||
page = bitpoll.get_poll_webpage(poll_id=poll_id) | ||
votes = bitpoll.collect_vote_dates(page) | ||
best_date = scheduler.find_best_date(votes) | ||
print("Most promising date:", best_date) | ||
|
||
if best_date: | ||
print("Found valid date, sending out invitation") | ||
gmail.send_schafkopf_meeting_invitation( | ||
receivers=email_table.load_all_mails(dynamodb), | ||
day=best_date.date, | ||
bitpoll_link=poll_website | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
from scheduler import env # ensure loading aws credentials | ||
lambda_handler({}, {}) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import json | ||
from typing import List | ||
|
||
from pydantic import BaseModel | ||
|
||
|
||
class EmailItem(BaseModel): | ||
email: str | ||
|
||
|
||
def add(dynamodb, email: EmailItem): | ||
table = dynamodb.Table("schafkopf_emails") | ||
table.put_item(Item=json.loads(email.model_dump_json())) | ||
|
||
|
||
def load_all_mails(dynamodb) -> List[str]: | ||
return [i.email for i in load_all(dynamodb)] | ||
|
||
|
||
def load_all(dynamodb) -> List[EmailItem]: | ||
try: | ||
table = dynamodb.Table("schafkopf_emails") | ||
response = table.scan() | ||
items = response["Items"] | ||
while "LastEvaluatedKey" in response: | ||
response = table.scan(ExclusiveStartKey=response["LastEvaluatedKey"]) | ||
items.extend(response["Items"]) | ||
result = [] | ||
for item in items: | ||
try: | ||
result.append(EmailItem(**item)) | ||
except Exception as e: | ||
print(f"Could not load {item}: {e}") | ||
return result | ||
except Exception as e: | ||
raise RuntimeError( | ||
f"Could not load emails: {e}" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import json | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from pydantic import BaseModel | ||
|
||
POLL_ITEM_UUID = '021dae01-ac37-4c3c-bc6c-952d3e4a57d5' | ||
|
||
|
||
class PollItem(BaseModel): | ||
running_poll_id: Optional[str] = None | ||
start_next_poll_date: Optional[datetime] = None | ||
|
||
def load(dynamodb) -> PollItem: | ||
try: | ||
table = dynamodb.Table("schafkopf_polls") | ||
response = table.query( | ||
KeyConditionExpression=f"#u = :a", | ||
ExpressionAttributeNames={'#u': "uuid"}, | ||
ExpressionAttributeValues={":a": POLL_ITEM_UUID}, | ||
Limit=1, | ||
) | ||
return PollItem(**response["Items"][0]) | ||
except Exception as e: | ||
raise ValueError( | ||
f"Could not find poll item", e | ||
) | ||
|
||
def update(dynamodb, poll_item: PollItem): | ||
table = dynamodb.Table("schafkopf_polls") | ||
item_dict = json.loads(poll_item.model_dump_json()) | ||
item_dict['uuid'] = POLL_ITEM_UUID | ||
table.put_item(Item=item_dict) |
Oops, something went wrong.