zmoog / public-notes

Apache License 2.0
0 stars 1 forks source link

Automate information delivery using Telegram #5

Closed zmoog closed 1 year ago

zmoog commented 1 year ago
┌ ─ ─ ─ ─ ─ ─ ┐     ┌─────────────┐    ┌─────────────┐    ┌─────────────┐     ┌ ─ ─ ─ ─ ─ ─ ┐
    source     ────▶│   adapter   │───▶│ transformer │───▶│   adapter   │────▶  destination  
└ ─ ─ ─ ─ ─ ─ ┘     └─────────────┘    └─────────────┘    └─────────────┘     └ ─ ─ ─ ─ ─ ─ ┘
zmoog commented 1 year ago

Requirements

Install required adapters:

pip install toggl-track telegram-cli refurbished

and the transfrormers:

pip install jinja-cli

This is a low-tech solution.

zmoog commented 1 year ago

1 list today's Pro Max iPhones

Collect

Fetch info from the source and adapt it to our JSON-based data model:

$ rfrb it iphones --name "13 Pro Max" --format json
[
  {
    "name": "iPhone 13 Pro Max 512GB ricondizionato - Grafite (senza SIM)",
    "family": "iphone",
    "store": "it",
    "url": "https://www.apple.com/it/shop/product/FLLF3QL/A/iphone-13-pro-max-512gb-ricondizionato-grafite-senza-sim",
    "price": 1389.0,
    "previous_price": 1639.0,
    "savings_price": 250.0,
    "saving_percentage": 0.1525320317266626,
    "model": "FLLF3QL"
  },
  {
    "name": "iPhone 13 Pro Max 1TB ricondizionato - Grafite (senza SIM)",
    "family": "iphone",
    "store": "it",
    "url": "https://www.apple.com/it/shop/product/FLLK3QL/A/iphone-13-pro-max-1tb-ricondizionato-grafite-senza-sim",
    "price": 1589.0,
    "previous_price": 1869.0,
    "savings_price": 280.0,
    "saving_percentage": 0.149812734082397,
    "model": "FLLK3QL"
  }
]%                                                                                                                                                                                                                    

Transform

Refurbished returns a list of objects, but jinja-cli expects a map. Let's wrap the list into a map using jq:

$ jq '{"entries": .}' iphones.json
{
  "entries": [
    {
      "name": "iPhone 13 Pro Max 512GB ricondizionato - Grafite (senza SIM)",
      "family": "iphone",
      "store": "it",
      "url": "https://www.apple.com/it/shop/product/FLLF3QL/A/iphone-13-pro-max-512gb-ricondizionato-grafite-senza-sim",
      "price": 1389,
      "previous_price": 1639,
      "savings_price": 250,
      "saving_percentage": 0.1525320317266626,
      "model": "FLLF3QL"
    },
    {
      "name": "iPhone 13 Pro Max 1TB ricondizionato - Grafite (senza SIM)",
      "family": "iphone",
      "store": "it",
      "url": "https://www.apple.com/it/shop/product/FLLK3QL/A/iphone-13-pro-max-1tb-ricondizionato-grafite-senza-sim",
      "price": 1589,
      "previous_price": 1869,
      "savings_price": 280,
      "saving_percentage": 0.149812734082397,
      "model": "FLLK3QL"
    }
  ]
}

With this template templates/refurbished/iphones.j2:

Today's iPhones Pro Maxes:

{% for product in products -%}
- {{ "€{:,.0f}".format(product.price) }} for {{ product.name }}
{% endfor %}

we can turn the list into a nice summary:

$ cat iphones.json | jq '{"products": .}' | jinja -d - -f json templates/refurbished/iphones.j2
Today's iPhones Pro Maxes:

- €1,389 for iPhone 13 Pro Max 512GB ricondizionato - Grafite (senza SIM)
- €1,589 for iPhone 13 Pro Max 1TB ricondizionato - Grafite (senza SIM)

Deliver

$ cat iphones.json | jq '{"products": .}' | jinja -d - -f json templates/refurbished/iphones.j2 | tgm message send --chat-id <ID>
message-id: 740
zmoog commented 1 year ago

Schedule

I am going to use cron for scheduling the command execution (it's a low-tech solution).

Putting together a proof-of-concept automator crap at https://github.com/zmoog/automator

zmoog commented 1 year ago

Small refactoring to make automation scripts more simple by moving set up, checks, and execution logic into a run.sh file; see https://github.com/zmoog/automator/commit/d441ef083f1cec61c9ac9c910ffc58a19afd7b58 for more details.

zmoog commented 1 year ago

Now we have a base to add more automations using this simple framework.