diff --git a/tools/README.md b/tools/README.md index 12a5c824..0a49cc65 100644 --- a/tools/README.md +++ b/tools/README.md @@ -48,15 +48,13 @@ sh run_download_automation.sh ``` #### C) `meetup_import.py` -**Before running the script, make sure** to download the most recent iCal feed using [this link](https://www.meetup.com/women-coding-community/events/ical/). - -Place the downloaded `.ics` file inside the `tools/files` folder and make sure it is renamed to `meetup.ics`. - -Afterwards, run the command below: +Run the command below from the *tools* directory. ```shell sh run_meetup_import.sh ``` +Note: this will overwrite `tools/files/meetup.ics` if it already exists. + **Note:** - New data will be appended to [`events.yml`](../_data/events.yml). Verify that all events details are formatted correctly, manually update if needed. diff --git a/tools/download_meetup_ics.py b/tools/download_meetup_ics.py new file mode 100644 index 00000000..83ef7432 --- /dev/null +++ b/tools/download_meetup_ics.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 +"""Download the Meetup iCal feed and save as `tools/files/meetup.ics`. + +This script prefers `requests` if available but falls back to the +standard-library `urllib` so it has no hard dependency. +""" +from pathlib import Path +import sys + +URL = "https://www.meetup.com/women-coding-community/events/ical/" +OUT_PATH = Path(__file__).parent / "files" / "meetup.ics" +OUT_PATH.parent.mkdir(parents=True, exist_ok=True) + + +def download_with_requests(): + import requests + + headers = {"User-Agent": "wcc-ical-downloader/1.0 (+https://github.com/silkenodwell)"} + resp = requests.get(URL, headers=headers, stream=True, timeout=30) + resp.raise_for_status() + with open(OUT_PATH, "wb") as f: + for chunk in resp.iter_content(8192): + if chunk: + f.write(chunk) + return resp.headers.get("Content-Type", "") + + +def download_with_urllib(): + import urllib.request + + req = urllib.request.Request(URL, headers={"User-Agent": "wcc-ical-downloader/1.0"}) + with urllib.request.urlopen(req, timeout=30) as r, open(OUT_PATH, "wb") as f: + f.write(r.read()) + return r.getheader("Content-Type") or "" + + +def main(): + try: + try: + content_type = download_with_requests() + except Exception as e: # fallback to stdlib + print(f"requests download failed ({e}), falling back to urllib", file=sys.stderr) + content_type = download_with_urllib() + except Exception as e: + print(f"Failed to download iCal: {e}", file=sys.stderr) + sys.exit(2) + + print(f"Saved iCal to {OUT_PATH} (Content-Type: {content_type})") + + +if __name__ == "__main__": + main() diff --git a/tools/run_meetup_import.sh b/tools/run_meetup_import.sh index c27751aa..2adfd6f5 100644 --- a/tools/run_meetup_import.sh +++ b/tools/run_meetup_import.sh @@ -1,5 +1,13 @@ -#Create the virtual environment On macOS/Linux -python3.12 -m venv myenv +# Check we are in the tools directory else exit with warning message +if [ "$(basename "$PWD")" != "tools" ]; then + echo "Please run this script from the repository's tools/ directory." + exit 1 +fi + +#Create the virtual environment On macOS/Linux if it doesn't exist: +if [ ! -d "myenv" ]; then + python3.12 -m venv myenv +fi #Activate the virtual environment: source myenv/bin/activate @@ -7,4 +15,6 @@ source myenv/bin/activate # Install packages pip install -r requirements.txt +python3.12 download_meetup_ics.py + python3.12 meetup_import.py \ No newline at end of file