Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Emrys-Merlin committed Sep 7, 2024
1 parent 1b059fa commit 9c60a7a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ I am currently working on publishing the package on PyPI.

## Usage

After the installation, the intended way to invoke the framework is by writing a small runner script (which you can find [here](examples/simple_poll_example.py)):
After the installation, the intended way to invoke the framework is by writing a small runner script (which you can find [here](examples/simple_dummy_example.py)):
```Python
from playwright.sync_api import Playwright
from web_watchr import Watchr
Expand Down Expand Up @@ -57,7 +57,7 @@ The runner consists of three parts:
2. We implement the `poll` function and decorate it with `@watchr.set_poller`. The poll function contains all the website-specific logic to extract the text of interest. Most of this function can be automatically generated using [`playwright codegen`](https://playwright.dev/python/docs/codegen#running-codegen).
3. We invoke `watchr`, which will poll the website once.

By default, `watchr` will simply print the text to std out. If you want to receive alerts on your phone via telegram, we need to modify the script slightly:
By default, `watchr` will simply print the text to std out. If you want to receive alerts on your phone via telegram, we need to modify the [script](examples/simple_telegram_alerting.py) slightly:
```Python
import os

Expand Down Expand Up @@ -92,7 +92,10 @@ if __name__ == "__main__":

There are two key changes compared to the inital script:

1. We removed the `DummyComparer`. By default, `Watchr` uses an `FSComparer` which stores the old state in a file. The default location is `~/.local/share/web_watchr/cache`, which can be overwritten. This has the advantage that the runner does not need to run continously, but can be invoked periodically (e.g., via `cron`).
1. We removed the `DummyComparer`. By default, `Watchr` uses an `FSComparer` which stores the old state in a file. The default location is `~/.local/share/web_watchr/cache`, which can be adapted. This has the advantage that the runner does not need to run continously, but can be invoked periodically (e.g., via `cron`).
2. We instantiated a `TelegramAlerter` reading a `token` and a `chat_id` from some environment variables. These are secrets of your bot that you need to send messages with it. If you are unsure how to create a bot, please have a look [here](https://core.telegram.org/bots/tutorial#obtain-your-bot-token). To find out your `chat_id`, you can use the approach mentioned [here](https://stackoverflow.com/a/32572159/9685500).

> [!CAUTION]
> Keep your bot token secret. In particular, make sure to never add it to version control. Otherwise, malicious actors can use it for ther purposes.
Running the script will now send updates to your phone via telegram!
File renamed without changes.
29 changes: 29 additions & 0 deletions examples/simple_telegram_alerting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os

from playwright.sync_api import Playwright
from web_watchr import Watchr
from web_watchr.alert import TelegramAlerter

watchr = Watchr(
alerter=TelegramAlerter(
token=os.getenv("TELEGRAM_TOKEN"),
chat_id=os.getenv("TELEGRAM_CHAT_ID"),
)
)


@watchr.set_poller
def poll(playwright: Playwright) -> str:
browser = playwright.chromium.launch(headless=True)
context = browser.new_context()
page = context.new_page()
page.goto("https://www.example.com/")
text = page.get_by_role("heading").inner_text()
context.close()
browser.close()

return text


if __name__ == "__main__":
watchr()

0 comments on commit 9c60a7a

Please sign in to comment.