Skip to content

Latest commit

 

History

History
325 lines (247 loc) · 10 KB

File metadata and controls

325 lines (247 loc) · 10 KB

User

I need a quick script that:

where mn is the year and month in YYYYMM format.

  • Checks if there's any availability between 2 dates provided. If it's 8,9 and 10 of december for example, it should go to mn=202412 and see if those dates are available.
  • Sends me a Telegram message with the results and the relevant links
  • Can run on GitHub Actions for free , and runs everyday at 15am

The script should be in Python and use the Telegram Bot API. Also give me the github action script. I'll set up the GitHub secrets for the bot token.

Atached you can find a piece of the html of that page, the relevant table

Model

I'll help you create a Python script for scraping the Kilmainham Gaol Museum tour availability and a GitHub Actions workflow to automate it.

import os
import requests
from bs4 import BeautifulSoup
from datetime import datetime, timedelta
import telegram

def check_tour_availability(year, month, target_dates):
    """
    Check tour availability for specific dates in a given month.
    
    :param year: Year to check (4-digit)
    :param month: Month to check (1-12)
    :param target_dates: List of dates to check availability
    :return: Dict of available dates with their links
    """
    # Construct the URL for the specific month
    url = f"https://kilmainhamgaol.admit-one.eu/?p=calendar&ev=TOUR&mn={year}{month:02d}"
    
    try:
        # Send a request to the webpage
        response = requests.get(url)
        response.raise_for_status()
        
        # Parse the HTML
        soup = BeautifulSoup(response.text, 'html.parser')
        
        # Find availability for target dates
        available_dates = {}
        for date in target_dates:
            # Look for the specific date cell
            date_cell = soup.find('div', text=str(date))
            
            # Check if the date is available
            if date_cell and date_cell.parent.name == 'a':
                available_dates[date] = date_cell.parent['href']
        
        return available_dates
    
    except requests.RequestException as e:
        print(f"Error fetching availability: {e}")
        return {}

def send_telegram_message(bot_token, chat_id, message):
    """
    Send a Telegram message about tour availability.
    
    :param bot_token: Telegram Bot API token
    :param chat_id: Telegram chat ID to send message to
    :param message: Message text to send
    """
    try:
        bot = telegram.Bot(token=bot_token)
        bot.send_message(chat_id=chat_id, text=message)
    except Exception as e:
        print(f"Error sending Telegram message: {e}")

def main():
    # Get environment variables
    bot_token = os.environ.get('TELEGRAM_BOT_TOKEN')
    chat_id = os.environ.get('TELEGRAM_CHAT_ID')
    
    # Example: Check availability for dates in December 2024
    year = 2024
    month = 12
    target_dates = [9, 11, 12, 16, 17, 18, 19, 20, 21, 22, 23]
    
    # Check availability
    available_dates = check_tour_availability(year, month, target_dates)
    
    # Prepare message
    if available_dates:
        message = f"Available Kilmainham Gaol Tours in {year}-{month:02d}:\n"
        for date, link in available_dates.items():
            message += f"{date}: {link}\n"
    else:
        message = f"No available tours found for {year}-{month:02d}"
    
    # Send Telegram message
    if bot_token and chat_id:
        send_telegram_message(bot_token, chat_id, message)
    else:
        print("Telegram bot token or chat ID not set")

if __name__ == "__main__":
    main()

Now, I'll create the GitHub Actions workflow:

name: Kilmainham Gaol Tour Availability Checker

on:
  schedule:
    # Run daily at 15 PM UTC
    - cron: '0 15 * * *'
  workflow_dispatch:
    # Allow manual triggering

jobs:
  check-availability:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v3
    
    - name: Set up Python
      uses: actions/setup-python@v3
      with:
        python-version: '3.9'
    
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install requests beautifulsoup4 python-telegram-bot
    
    - name: Run Availability Checker
      env:
        TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
        TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
      run: python kilmainham_availability_checker.py

Here's what you need to do:

  1. Create a new GitHub repository
  2. Save the first script as kilmainham_availability_checker.py
  3. Save the GitHub Actions workflow as .github/workflows/check-availability.yml
  4. Set up two GitHub Secrets:
    • TELEGRAM_BOT_TOKEN: Your Telegram bot token
    • TELEGRAM_CHAT_ID: The chat ID where you want to receive messages

Requirements and notes:

  • Install python-telegram-bot library
  • The script checks all available dates in the specified month
  • Modify the target_dates list in main() to your specific dates of interest
  • You'll need to create a Telegram bot via BotFather on Telegram
  • The script runs daily at 15 PM UTC via GitHub Actions

You can manually trigger the workflow using the "Run workflow" button in GitHub Actions if you want to test it immediately. Would you like me to explain any part of the implementation?

User

don't use a 3rd party telegram library, just use "requests"

Model

I'll modify the script to use the Telegram Bot API with requests instead of a third-party library:

import os
import requests
from bs4 import BeautifulSoup
from datetime import datetime, timedelta

def check_tour_availability(year, month, target_dates):
    """
    Check tour availability for specific dates in a given month.
    
    :param year: Year to check (4-digit)
    :param month: Month to check (1-12)
    :param target_dates: List of dates to check availability
    :return: Dict of available dates with their links
    """
    # Construct the URL for the specific month
    url = f"https://kilmainhamgaol.admit-one.eu/?p=calendar&ev=TOUR&mn={year}{month:02d}"
    
    try:
        # Send a request to the webpage
        response = requests.get(url)
        response.raise_for_status()
        
        # Parse the HTML
        soup = BeautifulSoup(response.text, 'html.parser')
        
        # Find availability for target dates
        available_dates = {}
        for date in target_dates:
            # Look for the specific date cell
            date_cell = soup.find('div', text=str(date))
            
            # Check if the date is available
            if date_cell and date_cell.parent.name == 'a':
                available_dates[date] = date_cell.parent['href']
        
        return available_dates
    
    except requests.RequestException as e:
        print(f"Error fetching availability: {e}")
        return {}

def send_telegram_message(bot_token, chat_id, message):
    """
    Send a Telegram message about tour availability.
    
    :param bot_token: Telegram Bot API token
    :param chat_id: Telegram chat ID to send message to
    :param message: Message text to send
    """
    telegram_url = f"https://api.telegram.org/bot{bot_token}/sendMessage"
    payload = {
        "chat_id": chat_id,
        "text": message
    }
    
    try:
        response = requests.post(telegram_url, json=payload)
        response.raise_for_status()
    except requests.RequestException as e:
        print(f"Error sending Telegram message: {e}")

def main():
    # Get environment variables
    bot_token = os.environ.get('TELEGRAM_BOT_TOKEN')
    chat_id = os.environ.get('TELEGRAM_CHAT_ID')
    
    # Example: Check availability for dates in December 2024
    year = 2024
    month = 12
    target_dates = [9, 11, 12, 16, 17, 18, 19, 20, 21, 22, 23]
    
    # Check availability
    available_dates = check_tour_availability(year, month, target_dates)
    
    # Prepare message
    if available_dates:
        message = f"Available Kilmainham Gaol Tours in {year}-{month:02d}:\n"
        for date, link in available_dates.items():
            message += f"{date}: {link}\n"
    else:
        message = f"No available tours found for {year}-{month:02d}"
    
    # Send Telegram message
    if bot_token and chat_id:
        send_telegram_message(bot_token, chat_id, message)
    else:
        print("Telegram bot token or chat ID not set")

if __name__ == "__main__":
    main()

The GitHub Actions workflow remains the same as in the previous response. Key changes:

  • Removed python-telegram-bot dependency
  • Added send_telegram_message() function using requests to send messages via Telegram Bot API
  • No other functional changes

This version uses the standard Telegram Bot API REST endpoint to send messages, which simplifies the dependencies and removes the need for a third-party library.

User

adapt the github action to run it on push to main as well

Model

I'll update the GitHub Actions workflow to trigger on push to main as well:

name: Kilmainham Gaol Tour Availability Checker

on:
  schedule:
    # Run daily at 15 PM UTC
    - cron: '0 15 * * *'
  push:
    branches:
      - main
  workflow_dispatch:
    # Allow manual triggering

jobs:
  check-availability:
    runs-on: ubuntu-latest
    
    steps:
    - name: Checkout repository
      uses: actions/checkout@v3
    
    - name: Set up Python
      uses: actions/setup-python@v3
      with:
        python-version: '3.9'
    
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install requests beautifulsoup4
    
    - name: Run Availability Checker
      env:
        TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }}
        TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }}
      run: python kilmainham_availability_checker.py

The key change is adding the push trigger to run the workflow whenever code is pushed to the main branch. I've also removed the Telegram bot library from the dependencies since we're using requests directly. Everything else remains the same.