diff --git a/.github/workflows/houseseat-notify-on-event.yml b/.github/workflows/houseseat-notify-on-event.yml index 031ef5c8..e940484e 100644 --- a/.github/workflows/houseseat-notify-on-event.yml +++ b/.github/workflows/houseseat-notify-on-event.yml @@ -36,6 +36,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v4 + with: + ref: ${{ github.ref }} - name: Setup run: | pip install slack-sdk @@ -43,3 +45,13 @@ jobs: run: | cd ./houseseats.com/notify-on-event python3 search.py + - name: Commit and push + run: | + git config --local user.email "action@github.com" + git config --local user.name "GitHub Action" + git add . + git commit -m "[houseseat-notify-on-event action] Updating the state.yaml file" + git push origin main + env: + ## This is the personal access token (PAT) for the GitHub bot user + GITHUB_TOKEN: ${{ secrets.PLAYGROUND_REPO }} \ No newline at end of file diff --git a/houseseats.com/notify-on-event/README.md b/houseseats.com/notify-on-event/README.md index 3738e72f..f705011d 100644 --- a/houseseats.com/notify-on-event/README.md +++ b/houseseats.com/notify-on-event/README.md @@ -43,3 +43,26 @@ new shows or one time shows that comes up sometimes and these are the shows we a ## scratch + +New process: +* Open the state.yaml file and read it the yaml to a list of dictionaries +* Go and get a list of all the events +* Loop through the new events list +* If the event is in the exclude file list, then dont process this event anymore, else continue +* If the event is NOT in the previous state dictionary list + * then send a notification to slack + * add it into the new state list dictionary +* If the event is in the previous state dictionary list + * add it into the new state list dictionary +* Output the new state list dictionary to the state.yaml file + + +What this should do based on how events shows up and leaves: +* 7:00pm battle bot comes up +* alert goes to slack +* 7:05pm battle bots does not alert again +* 7:10pm battle bots is removed +* 7:15pm battle bots shows up again +* alert goes to slack + +This means that it will only alert on new events that freshly comes up. diff --git a/houseseats.com/notify-on-event/search.py b/houseseats.com/notify-on-event/search.py index 9418280d..ba41a44a 100644 --- a/houseseats.com/notify-on-event/search.py +++ b/houseseats.com/notify-on-event/search.py @@ -7,6 +7,7 @@ import urllib.parse from slack_sdk import WebClient from slack_sdk.errors import SlackApiError +import yaml # How many tickets are you looking for per time slot NUMBER_OF_RESERVABLE_PER_TIME_SLOT = 2 @@ -30,6 +31,17 @@ def send_to_slack(message): print('Starting program...') + +# Open the state YAML file +with open('state.yaml', 'r') as file: + # Load the YAML data from the file + previous_state = yaml.safe_load(file) + +# Now data is a list of dictionaries +print(previous_state) + +new_state = [] + # Create a session object s = requests.Session() @@ -93,12 +105,33 @@ def send_to_slack(message): found_exclude = True - ## If the event is not in the exclude list, then send a message to Slack + ## If the event is not in the exclude list, then continue to process the event if found_exclude == False: - print(f'[NOTIFY] not found in exclude list: {event[0]} | {event[1]}') - - ## Send message to Slack - if slack_enabled == 'true': - send_to_slack(f""" - *[Houseseat]* Search found: - """) + print(f'[NOT IN EXCLUDE LIST] not found in exclude list: {event[0]} | {event[1]}') + + ## Add the event to the new state dictionary list + new_state.append({ + 'event_id': event[0], + 'event_name': event[1] + }) + + ## If the event is NOT in the previous state dictionary list then send a message to Slack + ## and write it to the new state dictionary list + found_in_previous_state = False + + for item in previous_state: + if item['event_id'] == event[0]: + found_in_previous_state = True + + if not found_in_previous_state: + ## Send message to Slack + print(f'[NOTIFY] Sending slack message for: {event[0]} | {event[1]}') + if slack_enabled == 'true': + send_to_slack(f""" + *[Houseseat v2]* Search found: + """) + +# Output the new state dictionary list to the state.yaml file +with open('state.yaml', 'w') as file: + # Write the YAML data to the file + yaml.dump(new_state, file) diff --git a/houseseats.com/notify-on-event/state.yaml b/houseseats.com/notify-on-event/state.yaml new file mode 100644 index 00000000..93e4dbc4 --- /dev/null +++ b/houseseats.com/notify-on-event/state.yaml @@ -0,0 +1,3 @@ +- event_id: '1' + event_name: foo +