<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Amzar</title>
    <description>The latest articles on DEV Community by Amzar (@amzar96).</description>
    <link>https://dev.to/amzar96</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1017419%2F795b195d-8ef6-4aae-8ce2-d56526e36848.jpeg</url>
      <title>DEV Community: Amzar</title>
      <link>https://dev.to/amzar96</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amzar96"/>
    <language>en</language>
    <item>
      <title>How to Connect Hevy to Your OpenCode Agent (Using the Hevy API)</title>
      <dc:creator>Amzar</dc:creator>
      <pubDate>Sun, 22 Nov 2026 14:57:16 +0000</pubDate>
      <link>https://dev.to/amzar96/how-to-connect-hevy-to-your-opencode-agent-using-the-hevy-api-ai7</link>
      <guid>https://dev.to/amzar96/how-to-connect-hevy-to-your-opencode-agent-using-the-hevy-api-ai7</guid>
      <description>&lt;p&gt;Hevy now works inside ChatGPT. Type &lt;code&gt;@hevy&lt;/code&gt; and it reads your training history and builds routines for you. But if you live in OpenCode like I do, you can wire Hevy's public API to your own agent and get the same thing, with full control over what it knows about you. That includes the stuff ChatGPT won't ask you about, like an injury or surgery.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the Hevy API gives you
&lt;/h2&gt;

&lt;p&gt;Hevy has a public REST API at &lt;code&gt;api.hevyapp.com&lt;/code&gt;. It needs a Hevy Pro account. The API key is a UUID you grab from the site.&lt;/p&gt;

&lt;p&gt;With it you can read and write almost everything in the app:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Endpoint&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET /v1/user/info&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Account, units, Pro status&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET /v1/workouts&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Every logged session with sets and weights&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET /v1/exercise_templates&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;The full exercise library&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET /v1/routines&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Your saved routines and folders&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;POST /v1/routines&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Create a routine&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET /v1/exercise_history/{id}&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;History for one exercise&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;GET/POST/PUT /v1/body_measurements&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Weight, waist, thigh, body fat&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That's the whole story. Your agent reads what you've done and writes back routines you can follow.&lt;/p&gt;

&lt;h2&gt;
  
  
  The official way first
&lt;/h2&gt;

&lt;p&gt;If you want zero setup, use the official path. Hevy built it on top of MCP.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open ChatGPT and install the Hevy app (or use the Claude connector).&lt;/li&gt;
&lt;li&gt;Type &lt;code&gt;@hevy&lt;/code&gt; and ask for a plan or an analysis of your last sessions.&lt;/li&gt;
&lt;li&gt;It saves routines straight to your Hevy account.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's free on both sides. The catch for me is it lives inside ChatGPT. My training notes, my injury history, and my agent all live somewhere else. So I went the API route.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I connect Hevy to OpenCode
&lt;/h2&gt;

&lt;p&gt;My setup is one small script plus one skill. The script talks to the API. The skill tells the agent who I am.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Get your API key
&lt;/h3&gt;

&lt;p&gt;Hevy Pro only. Sign in at &lt;code&gt;api.hevyapp.com&lt;/code&gt;, grab the key. It's a UUID.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Auth is &lt;code&gt;api-key&lt;/code&gt;, not Bearer
&lt;/h3&gt;

&lt;p&gt;This is the first gotcha. Most APIs want &lt;code&gt;Authorization: Bearer&lt;/code&gt;. Hevy wants a custom header:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -s https://api.hevyapp.com/v1/user/info \
  -H &lt;span class="s2"&gt;"api-key: $HEVY_TOKEN"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That returns your username, units, and Pro status. If you send &lt;code&gt;Bearer&lt;/code&gt; it 401s.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Everything is paginated, pageSize caps at 10
&lt;/h3&gt;

&lt;p&gt;This one will bite you. Hevy caps &lt;code&gt;pageSize&lt;/code&gt; at 10 on every endpoint. A year of training is hundreds of workouts, so you page through them and cache the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -s &lt;span class="s2"&gt;"https://api.hevyapp.com/v1/workouts?page=1&amp;amp;pageSize=10"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"api-key: $HEVY_TOKEN"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My script loops until &lt;code&gt;page &amp;gt;= page_count&lt;/code&gt;, then writes everything to a local cache so the agent isn't hammering the API every time it wants to think.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Writing a routine
&lt;/h3&gt;

&lt;p&gt;Creating a routine is a POST with the exercises as template IDs. You look up the ID from &lt;code&gt;/v1/exercise_templates&lt;/code&gt;, you never invent one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -s -X POST https://api.hevyapp.com/v1/routines \
  -H &lt;span class="s2"&gt;"api-key: $HEVY_TOKEN"&lt;/span&gt; \
  -H &lt;span class="s2"&gt;"Content-Type: application/json"&lt;/span&gt; \
  -d &lt;span class="s1"&gt;'{
    "routine": {
      "title": "Lower A - Quad Bias",
      "folder_id": 0,
      "notes": "Example block. Surgical leg leads.",
      "exercises": [
        {
          "exercise_template_id": "{TEMPLATE_ID}",
          "superset_id": null,
          "rest_seconds": 120,
          "notes": "Surgical leg leads.",
          "sets": [
            { "type": "normal", "weight_kg": 40, "reps": 10 }
          ]
        }
      ]
    }
  }'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That creates a reusable routine in Hevy, same as if you built it in the app.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: updating a routine is a PUT that replaces the whole exercise list. Read it first, change what you need, send it back. Don't PUT half an object, or you'll wipe the exercises.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What I share with my agent
&lt;/h2&gt;

&lt;p&gt;The API gives the agent my history. But the agent can't program me properly unless it also knows the things Hevy doesn't store. So I hand it a short profile:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;What I share&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;th&gt;Why the agent needs it&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Goal&lt;/td&gt;
&lt;td&gt;Lean bulk, gain 3 kg&lt;/td&gt;
&lt;td&gt;Sets calorie and volume targets&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Injury&lt;/td&gt;
&lt;td&gt;ACL reconstruction, right knee&lt;/td&gt;
&lt;td&gt;No plyos, surgical leg leads, slower progression&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Body&lt;/td&gt;
&lt;td&gt;170 cm, 70 kg, 85 cm waist&lt;/td&gt;
&lt;td&gt;Tracks the bulk against a waist budget&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Equipment&lt;/td&gt;
&lt;td&gt;Kettlebell + dumbbells, no bench&lt;/td&gt;
&lt;td&gt;Builds routines you can actually do at home&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Schedule&lt;/td&gt;
&lt;td&gt;3 gym days + 1 home day&lt;/td&gt;
&lt;td&gt;Splits volume across the week&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The injury line is the important one. A generic AI coach would hand me box jumps a few months after an ACL reconstruction. My agent knows the surgery and the graft type, so it programs around the knee instead of through it.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the agent does with it
&lt;/h2&gt;

&lt;p&gt;On every run it pulls my history and answers with real numbers, not memory. This is the kind of thing I get back for a week:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight markdown"&gt;&lt;code&gt;&lt;span class="gu"&gt;## Weekly working sets per muscle
&lt;/span&gt;| Muscle  | Total sets | Per week | vs 10-20 target |
|---------|-----------|----------|-----------------|
| quads   | 12        | 6.0      | under           |
| chest   | 16        | 8.0      | in range        |
| back    | 8         | 4.0      | well under      |
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That table is how I spot that one muscle is getting skipped while another is trained twice. I can ask it to review a week, spot a weak point, then build a routine to fix it, all saved back to Hevy.&lt;/p&gt;

&lt;p&gt;Here's the flow:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hevy app (you log) ──&amp;gt; Hevy API ──&amp;gt; your script ──&amp;gt; OpenCode + skill ──&amp;gt; writes routines back to Hevy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Why bother when ChatGPT does it?
&lt;/h2&gt;

&lt;p&gt;The official integration is the right tool for most people. It's free and it's one click. The API route is for when you want the agent to know more than Hevy logs. My injury, my graft type, my home equipment, my calorie target. That context is what turns a workout generator into an actual coach.&lt;/p&gt;

&lt;p&gt;The other reason is control. The API gives you body measurements, exercise history, and your full workout log, not just routines. You decide what the agent sees and what it writes. Nothing is shared unless you wire it up yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://api.hevyapp.com/docs" rel="noopener noreferrer"&gt;https://api.hevyapp.com/docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.hevyapp.com/hevy-mcp" rel="noopener noreferrer"&gt;https://www.hevyapp.com/hevy-mcp&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.hevyapp.com/features/hevy-chatgpt" rel="noopener noreferrer"&gt;https://www.hevyapp.com/features/hevy-chatgpt&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.hevyapp.com/community-updates/october-26/" rel="noopener noreferrer"&gt;https://www.hevyapp.com/community-updates/october-26/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://opencode.ai" rel="noopener noreferrer"&gt;https://opencode.ai&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>hevy</category>
      <category>api</category>
      <category>ai</category>
      <category>opencode</category>
    </item>
    <item>
      <title>How to Share Environment Variables Between Local and Server</title>
      <dc:creator>Amzar</dc:creator>
      <pubDate>Sun, 22 Nov 2026 14:39:52 +0000</pubDate>
      <link>https://dev.to/amzar96/how-to-share-environment-variables-between-local-and-server-1j3a</link>
      <guid>https://dev.to/amzar96/how-to-share-environment-variables-between-local-and-server-1j3a</guid>
      <description>&lt;p&gt;Copying &lt;code&gt;.env&lt;/code&gt; files between your laptop and the server is how secrets leak into Slack, git, and support tickets. Run one self-hosted secrets manager and both sides pull from the same place. No more copying.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;Your app needs the same variables in two places. So you copy the file, or paste a value into chat, or commit it "just this once".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;laptop/.env ──copy──&amp;gt; server/.env
     │                    │
     └──── drift ─────────┘   (leaks, git risk, "works on my machine")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two copies drift. One gets a new key, the other doesn't. Then a deploy fails because &lt;code&gt;DATABASE_URL&lt;/code&gt; is stale.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Infisical is
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Infisical&lt;/strong&gt; is a secrets manager you can run yourself. It's open-source at the core, with a UI, a CLI, and the concept of &lt;strong&gt;environments&lt;/strong&gt;: &lt;code&gt;dev&lt;/code&gt;, &lt;code&gt;prod&lt;/code&gt;, &lt;code&gt;staging&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You store secrets once. Then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Your &lt;strong&gt;laptop&lt;/strong&gt; pulls them with the CLI.&lt;/li&gt;
&lt;li&gt;Your &lt;strong&gt;server&lt;/strong&gt; pulls them with a machine identity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Secrets stay encrypted at rest. A real dashboard replaces a folder full of &lt;code&gt;.env&lt;/code&gt; files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Set it up: Docker Compose
&lt;/h2&gt;

&lt;p&gt;Docker Compose is the fastest way to get one running on a small server.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Download the Compose file and a starter env:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;curl -o docker-compose.prod.yml https://raw.githubusercontent.com/Infisical/infisical/main/docker-compose.prod.yml
curl -o .env https://raw.githubusercontent.com/Infisical/infisical/main/.env.example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pulls the stack definition (backend + Postgres + Redis) and a template of the required settings.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open &lt;code&gt;.env&lt;/code&gt; and set the URL your CLI and browser will use:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SITE_URL=https://{YOUR_DOMAIN}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This must match how you actually reach Infisical. Get it wrong and login redirects break.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Lock the file down, then start:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;chmod 600 .env
docker compose -f docker-compose.prod.yml up -d
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;chmod 600&lt;/code&gt; stops other users on the box from reading the file. &lt;code&gt;up -d&lt;/code&gt; starts the three containers in the background.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open &lt;code&gt;https://{YOUR_DOMAIN}&lt;/code&gt; and create the first account: it becomes the admin.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Back up your &lt;code&gt;ENCRYPTION_KEY&lt;/code&gt;&lt;/strong&gt; from &lt;code&gt;.env&lt;/code&gt;. Losing it means you can't decrypt secrets, even after restoring the database. It's the one value you can't regenerate.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Set it up: Kubernetes + Helm
&lt;/h2&gt;

&lt;p&gt;Running a homelab or cluster? Use the Helm chart instead of Compose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;helm repo add infisical https://dl.cloudsmith.io/public/infisical/helm-charts/helm/charts/
helm install infisical infisical/infisical-standalone \
  --namespace infisical --create-namespace
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This deploys the same backend with bundled Postgres and Redis. Point your ingress at it and set &lt;code&gt;SITE_URL&lt;/code&gt; the same way.&lt;/p&gt;

&lt;h2&gt;
  
  
  Share with your laptop
&lt;/h2&gt;

&lt;p&gt;Install the CLI, then log in against your instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;infisical login --domain https://&lt;span class="o"&gt;{&lt;/span&gt;YOUR_DOMAIN&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This writes a token to your machine so later commands know where to talk to.&lt;/p&gt;

&lt;p&gt;Create a project and link it to your repo folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /path/to/your/project
infisical init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;infisical init&lt;/code&gt; creates a project (or links an existing one) and drops a small config file that remembers the project and environment.&lt;/p&gt;

&lt;p&gt;Now import the &lt;code&gt;.env&lt;/code&gt; you already have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;infisical secrets &lt;span class="nb"&gt;set&lt;/span&gt; --env=dev --file .env
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every line of your old &lt;code&gt;.env&lt;/code&gt; becomes a secret in Infisical. You can delete the file.&lt;/p&gt;

&lt;p&gt;The two commands you'll use every day:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;What it does&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;infisical run --env=dev -- npm start&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Injects secrets as env vars, runs the command&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;infisical export --env=dev &amp;gt; .env&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Writes secrets back to a file (for tools that demand one)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;infisical run&lt;/code&gt; is the nice part: the secrets exist only for the life of that process. Nothing sits on disk.&lt;/p&gt;

&lt;h2&gt;
  
  
  Share with your server
&lt;/h2&gt;

&lt;p&gt;A server shouldn't hold a &lt;code&gt;.env&lt;/code&gt; either. Give it a &lt;strong&gt;machine identity&lt;/strong&gt; instead of a user login, then let it fetch at deploy time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        ┌─────────────┐
        │  Infisical  │   one source of truth
        └──────┬──────┘
      ┌────────┴─────────┐
      │                  │
 laptop (CLI)      server (machine identity)
 infisical run     fetches secrets at deploy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In CI, you set the machine identity's credentials as secret env vars, then:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;infisical run --env=prod -- ./deploy.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The server gets the same secrets your laptop does, without anyone pasting them.&lt;/p&gt;

&lt;p&gt;On Kubernetes, &lt;strong&gt;External Secrets Operator&lt;/strong&gt; can sync Infisical straight into native &lt;code&gt;Secret&lt;/code&gt; objects, so your pods never see a &lt;code&gt;.env&lt;/code&gt; file at all.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://infisical.com/docs/self-hosting/deployment-options/docker-compose" rel="noopener noreferrer"&gt;https://infisical.com/docs/self-hosting/deployment-options/docker-compose&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://infisical.com/docs/self-hosting/deployment-options/kubernetes-helm" rel="noopener noreferrer"&gt;https://infisical.com/docs/self-hosting/deployment-options/kubernetes-helm&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://infisical.com/docs/cli/overview" rel="noopener noreferrer"&gt;https://infisical.com/docs/cli/overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://infisical.com/docs/documentation/platform/identities/overview" rel="noopener noreferrer"&gt;https://infisical.com/docs/documentation/platform/identities/overview&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://external-secrets.io/latest/provider/infisical/" rel="noopener noreferrer"&gt;https://external-secrets.io/latest/provider/infisical/&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>homelab</category>
      <category>infisical</category>
      <category>devops</category>
      <category>selfhosted</category>
    </item>
    <item>
      <title>How to Re-tag an Image in AWS ECR</title>
      <dc:creator>Amzar</dc:creator>
      <pubDate>Fri, 19 Jul 2024 20:18:32 +0000</pubDate>
      <link>https://dev.to/amzar96/how-to-re-tag-an-image-in-aws-ecr-2npi</link>
      <guid>https://dev.to/amzar96/how-to-re-tag-an-image-in-aws-ecr-2npi</guid>
      <description>&lt;p&gt;Are you managing Docker images in AWS ECR? Sometimes, you need to retag them to keep things organized.  In this quick guide, we'll show you how to retag your Docker images in AWS ECR Let'sy. Let’s get started!&lt;/p&gt;

&lt;p&gt;Let's consider an image named &lt;code&gt;hello-world-program&lt;/code&gt; with the following tags.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;current tag&lt;/th&gt;
&lt;th&gt;new tag&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;[untagged]&lt;/td&gt;
&lt;td&gt;0.0.2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Steps
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Setup &lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html" rel="noopener noreferrer"&gt;AWS CLI&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Navigate to AWS ECR and choose the images you want to update. Copy the image digest (e.g., &lt;em&gt;sha256:xxx&lt;/em&gt;), which is a unique and unchangeable identifier. Each image's content has its own distinct digest.&lt;/li&gt;
&lt;li&gt;Open terminal&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the following command to retrieve the image manifest and store it in the MANIFEST variable:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;MANIFEST&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nv"&gt;$(&lt;/span&gt;aws ecr batch-get-image --repository-name &lt;span class="o"&gt;{&lt;/span&gt;REPO_NAME&lt;span class="o"&gt;}&lt;/span&gt; --image-ids &lt;span class="nv"&gt;imageDigest&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="o"&gt;{&lt;/span&gt;IMAGE_DIGEST&lt;span class="o"&gt;}&lt;/span&gt; --output json &lt;span class="o"&gt;|&lt;/span&gt; jq --raw-output --join-output &lt;span class="s1"&gt;'.images[0].imageManifest'&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Run the following command to update the image:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;aws ecr put-image --repository-name &lt;span class="o"&gt;{&lt;/span&gt;REPO_NAME&lt;span class="o"&gt;}&lt;/span&gt; --image-tag &lt;span class="o"&gt;{&lt;/span&gt;NEW_TAG&lt;span class="o"&gt;}&lt;/span&gt; --image-manifest &lt;span class="s2"&gt;"$MANIFEST"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Replace &lt;strong&gt;{REPO_NAME}&lt;/strong&gt; with the name of your repository, &lt;strong&gt;{IMAGE_DIGEST}&lt;/strong&gt; with the actual digest you copied from AWS ECR, and &lt;strong&gt;{NEW_TAG}&lt;/strong&gt; with your desired tag&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Refresh the page, and you’ll see that the existing [untagged] image is now updated to 0.0.2.&lt;/p&gt;

&lt;p&gt;If you're on macOS and encounter the issue jq: command not found, check &lt;a href="https://stackoverflow.com/a/49261843" rel="noopener noreferrer"&gt;this guide&lt;/a&gt; for help.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Reference
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://docs.aws.amazon.com/AmazonECR/latest/userguide/image-retag.html" rel="noopener noreferrer"&gt;https://docs.aws.amazon.com/AmazonECR/latest/userguide/image-retag.html&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/a/59675086" rel="noopener noreferrer"&gt;https://stackoverflow.com/a/59675086&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>aws</category>
      <category>ecr</category>
    </item>
    <item>
      <title>Guide to Validating Sign-In with Apple Tokens in Python</title>
      <dc:creator>Amzar</dc:creator>
      <pubDate>Sat, 23 Dec 2023 16:19:25 +0000</pubDate>
      <link>https://dev.to/amzar96/guide-to-validating-sign-in-with-apple-tokens-in-python-13fm</link>
      <guid>https://dev.to/amzar96/guide-to-validating-sign-in-with-apple-tokens-in-python-13fm</guid>
      <description>&lt;h3&gt;
  
  
  Background
&lt;/h3&gt;

&lt;p&gt;The integration of Sign In with Apple into applications has become increasingly popular due to its convenience and privacy-focused approach. However, navigating the token validation process and retrieving user information can be a complex task for developers. This article aims to demystify this process specifically for Python developers, offering a step-by-step guide on how to validate tokens obtained through Sign In with Apple and efficiently access user data within your application. By delving into the intricacies of token validation and user information retrieval, this comprehensive guide aims to equip developers with the knowledge and tools necessary to seamlessly implement and utilize Sign In with Apple functionality in their Python-based applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Prerequisite
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;This article is not focussing on how to get the value from Apple Developer page.&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Client ID (client_id)&lt;/li&gt;
&lt;li&gt;Client Secret (client_secret) - A JSON Web Token (JWT) generated by the developer&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Generate client secret
&lt;/h3&gt;

&lt;p&gt;Generate a signed token to identify your client application. [2]&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;key_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
&lt;span class="n"&gt;team_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
&lt;span class="n"&gt;client_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;
&lt;span class="n"&gt;private_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;  &lt;span class="c1"&gt;# .p8 key file&lt;/span&gt;

&lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"kid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;key_id&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;open_private_key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;private_key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;private_key_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;open_private_key&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"iss"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;team_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"iat"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"exp"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nf"&gt;timedelta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;days&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;180&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"aud"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"https://appleid.apple.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"sub"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;client_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;client_secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;private_key_data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;algorithm&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"ES256"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Generate and validate tokens
&lt;/h3&gt;

&lt;p&gt;Validate an authorization grant code originating from your frontend interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"content-type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"application/x-www-form-urlencoded"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"client_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;client_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"client_secret"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;client_secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;id_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"grant_type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"authorization_code"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the response you'll get after success make request&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"access_token"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"adg61...67Or9"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"token_type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Bearer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"expires_in"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"refresh_token"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"rca7...lABoQ"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"id_token"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"eyJra...96sZg"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;access_token&lt;/strong&gt;: token from frontend&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;token_type&lt;/strong&gt;: type for OAuth 2.0 framework&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;expires_in&lt;/strong&gt;: lifespan or validity duration of an access token&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;refresh_token&lt;/strong&gt;: renews access without re-login&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;id_token&lt;/strong&gt;: token to get the user details (next action)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Get the user details
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;decoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;jwt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;id_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;verify&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"verify_signature"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;algorithms&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"HS256"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://developer.apple.com/documentation/sign_in_with_apple/generate_and_validate_tokens" rel="noopener noreferrer"&gt;https://developer.apple.com/documentation/sign_in_with_apple/generate_and_validate_tokens&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.apple.com/documentation/accountorganizationaldatasharing/creating-a-client-secret" rel="noopener noreferrer"&gt;https://developer.apple.com/documentation/accountorganizationaldatasharing/creating-a-client-secret&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>apple</category>
      <category>jwt</category>
    </item>
    <item>
      <title>Basic Kafka Stream in Python</title>
      <dc:creator>Amzar</dc:creator>
      <pubDate>Sat, 11 Mar 2023 19:25:02 +0000</pubDate>
      <link>https://dev.to/amzar96/basic-kafka-stream-in-python-3i3n</link>
      <guid>https://dev.to/amzar96/basic-kafka-stream-in-python-3i3n</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Kafka is an open-source distributed streaming platform developed by the Apache Software Foundation. It is designed to handle large volumes of real-time data streams and is used for &lt;strong&gt;building real-time data pipelines and streaming applications&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For instance, if you would like to have a real-time event log fetch from the source (Twitter, etc) and insert it into the target (CSV, SQLite, etc), you can leverage the Kafka library in Python. So, you need to know what are producer and consumer in Kafka.&lt;/p&gt;

&lt;h3&gt;
  
  
  Producer and Consumer
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhaeeggx890re937suv4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhaeeggx890re937suv4g.png" alt="Producer &amp;amp; Consumer in Kafka" width="601" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Producer&lt;/strong&gt; is a program or application that sends data or messages to a Kafka topic. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consumer&lt;/strong&gt; is a program or application that reads data or messages from a Kafka topic.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  How to setup Kafka in Python?
&lt;/h1&gt;

&lt;p&gt;Now, let's start with requirements to configure Kafka in Python.&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Kafka server
&lt;/h3&gt;

&lt;p&gt;You can start with local installation or AWS and GCP for advanced method). In this tutorial, I am launching Kafka server using &lt;a href="https://docs.docker.com/compose/" rel="noopener noreferrer"&gt;Docker Compose&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Assume Docker is installed and running or run &lt;code&gt;docker --version&lt;/code&gt; to confirm&lt;/p&gt;

&lt;p&gt;Create a docker compose file (&lt;strong&gt;docker-compose.yaml&lt;/strong&gt;) and copy below snippet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;version&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'2'&lt;/span&gt;
&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
  &lt;span class="na"&gt;zookeeper&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;confluentinc/cp-zookeeper:latest&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;ZOOKEEPER_CLIENT_PORT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2181&lt;/span&gt;
      &lt;span class="na"&gt;ZOOKEEPER_TICK_TIME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s"&gt;22181:2181&lt;/span&gt;

  &lt;span class="na"&gt;kafka&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;confluentinc/cp-kafka:latest&lt;/span&gt;
    &lt;span class="na"&gt;depends_on&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s"&gt;zookeeper&lt;/span&gt;
    &lt;span class="na"&gt;ports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="s"&gt;29092:29092&lt;/span&gt;
    &lt;span class="na"&gt;environment&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;KAFKA_BROKER_ID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
      &lt;span class="na"&gt;KAFKA_ZOOKEEPER_CONNECT&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;zookeeper:2181&lt;/span&gt;
      &lt;span class="na"&gt;KAFKA_ADVERTISED_LISTENERS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092&lt;/span&gt;
      &lt;span class="na"&gt;KAFKA_LISTENER_SECURITY_PROTOCOL_MAP&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT&lt;/span&gt;
      &lt;span class="na"&gt;KAFKA_INTER_BROKER_LISTENER_NAME&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;PLAINTEXT&lt;/span&gt;
      &lt;span class="na"&gt;KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, we could build the Docker&lt;br&gt;
&lt;code&gt;docker-compose up --build --force-recreate -d&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To confirm the containers are up, we need to check the status.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Docker Desktop&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbczuj7edexvqlaz41y98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbczuj7edexvqlaz41y98.png" alt="Docker container status" width="800" height="247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Command&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;╰─➤  docker-compose ps
      Name                   Command            State                      Ports
---------------------------------------------------------------------------------------------------
kafka_kafka_1       /etc/confluent/docker/run   Up      0.0.0.0:29092-&amp;gt;29092/tcp, 9092/tcp
kafka_zookeeper_1   /etc/confluent/docker/run   Up      0.0.0.0:22181-&amp;gt;2181/tcp, 2888/tcp, 3888/tcp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, now it shows the container is successfully built and running. Next step, how to create a topic and send the message?&lt;/p&gt;

&lt;h3&gt;
  
  
  Kafka producer (Python)
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Create a topic
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;confluent_kafka&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;admin&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;AdminClient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;NewTopic&lt;/span&gt;

&lt;span class="n"&gt;admin_client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AdminClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"bootstrap.servers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"localhost:29092"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;topic_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"my_first_topic"&lt;/span&gt;
&lt;span class="n"&gt;num_partitions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

&lt;span class="n"&gt;new_topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NewTopic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;topic_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;num_partitions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;admin_client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_topics&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;new_topic&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{topic_name} is created!!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{topic_name} is not created!!"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I am using &lt;code&gt;confluent_kafka&lt;/code&gt; to integrate with Kafka server.&lt;/p&gt;

&lt;p&gt;Also, we can define the number of partitions for the topic. Important to know that number of partitions cannot be changed after the topic has been created.&lt;/p&gt;

&lt;p&gt;If you have a topic with 10 partitions, you can have up to 10 consumers processing messages from that topic in parallel, each handling messages from its assigned partition.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;╰─➤  python kafka_topic.py
my_first_topic is created!!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, the topic has been created!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: If you first forgot to create a topic, no worries. It should create when you post the message to the new topic.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Post a message
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;uuid&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;confluent_kafka&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Producer&lt;/span&gt;


&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;post_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;produce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"my_first_topic"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dumps&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"message posted!! --&amp;gt; {data['comment']}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;except&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"failed to post message"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"user_session_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uuid4&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"user_name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"John Doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"comment"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Malaysia Boleh!"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;producer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Producer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"bootstrap.servers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"localhost:29092"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;post_message&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;producer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you run the Python script, it could post the message&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;╰─➤  python producer.py
message posted!! --&amp;gt; Malaysia Boleh!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Kafka consumer (Python)
&lt;/h3&gt;

&lt;p&gt;Okay, let's move to the consumer to check whether the data is really posted.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="n"&gt;confluent_kafka&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nc"&gt;Consumer&lt;/span&gt;

&lt;span class="n"&gt;consumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Consumer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="s2"&gt;"bootstrap.servers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"localhost:29092"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"group.id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"python-consumer"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="s2"&gt;"auto.offset.reset"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"earliest"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"my_first_topic"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;consumer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;poll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Error: {}"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;continue&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"utf-8"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;topic&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;ts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;topic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, here is the output when you run the script&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;╰─➤  python consumer.py
{"user_session_id": "5321202b-694a-4d82-9d2b-97174303595e", "user_name": "John Doe", "comment": "Malaysia Boleh!"} my_first_topic (1, 1678561082329)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the demo when you post a message, the consumer can print it out within a second.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://#" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto/https%3A%2F%2Fshare.gifyoutube.com%2FlR9DVl.gif" alt="Demo" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Enjoy ~&lt;/p&gt;

</description>
      <category>python</category>
      <category>kafka</category>
      <category>programming</category>
    </item>
    <item>
      <title>What Python libraries should I learn? [2023 UPDATED]</title>
      <dc:creator>Amzar</dc:creator>
      <pubDate>Sun, 05 Feb 2023 05:01:03 +0000</pubDate>
      <link>https://dev.to/amzar96/what-python-libraries-should-i-learn-2023-updated-1bch</link>
      <guid>https://dev.to/amzar96/what-python-libraries-should-i-learn-2023-updated-1bch</guid>
      <description>&lt;p&gt;Python is one of the most popular programming languages mentioned by &lt;a href="https://www.coursera.org/articles/popular-programming-languages" rel="noopener noreferrer"&gt;Coursera&lt;/a&gt;. As you know, Python can do anything from development to statistical analyses, and also, many standard libraries are provided.&lt;/p&gt;

&lt;h2&gt;
  
  
  Popular Python libraries for Starter
&lt;/h2&gt;

&lt;p&gt;Here is my recommendation of Python libraries that starter should know. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;a href="https://docs.python.org/3/library/os.html" rel="noopener noreferrer"&gt;os&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Let say you want to interact with operating system by code, os help a lot. You can list of the directory and also rename, delete or anything by the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getcwd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# get current directory&lt;/span&gt;
&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# list current directory&lt;/span&gt;
&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listdir&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'/Users/mohamadamzar/code'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# list specific directory&lt;/span&gt;
&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old_filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# rename file&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. &lt;a href="https://requests.readthedocs.io/en/latest/" rel="noopener noreferrer"&gt;requests&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;You can use &lt;code&gt;requests&lt;/code&gt; to interact with the API. It provide REST API method such as &lt;a href="https://dev.to/amzar/requestsget-python-435p"&gt;GET&lt;/a&gt;, POST, PUT and DELETE. If you are into the development, must know how to use the library.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/" rel="noopener noreferrer"&gt;BeautifulSoup&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;You can do data extraction (HTML and XML) easily by using BeautifulSoup. It parses the data with beautiful formatting; you can extract it by tag and div. You don't need to worry about the complicated tag; it can be solved using the library. Javascript? It will only be workable with simple Javascript 😉&lt;/p&gt;

&lt;h3&gt;
  
  
  4. &lt;a href="https://pypi.org/project/opencv-python/" rel="noopener noreferrer"&gt;cv2/opencv&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Interesting library to try. It can interact with the image; read and display the image, turn it to greyscale, save the image from the link, create a circle in the image, and many more.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;
&lt;span class="n"&gt;img&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;imread&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'range-rover.png'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cv2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;IMREAD_COLOR&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. &lt;a href="https://pandas.pydata.org/" rel="noopener noreferrer"&gt;pandas&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Pandas is a library to interact with multiple data sources. You not only can read the data, but you can analyze and manipulate the data. For the data analyst, it can be used for data wrangling and cleaning. Also, you can explore the simple ETL using pandas.&lt;/p&gt;




&lt;p&gt;There's a lot more to explore; I will update the post from time to time!&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>Keep your Windows always on!</title>
      <dc:creator>Amzar</dc:creator>
      <pubDate>Tue, 31 Jan 2023 18:00:05 +0000</pubDate>
      <link>https://dev.to/amzar96/keep-your-windows-always-on-2hn6</link>
      <guid>https://dev.to/amzar96/keep-your-windows-always-on-2hn6</guid>
      <description>&lt;p&gt;When you're working on something else that needs away from the keyboard, your Windows always automatically locks due to the setting (because you don't have admin access to change the setting due to company restrictions).&lt;/p&gt;

&lt;p&gt;Usually, people will open YouTube repeatedly, which is tiring since YouTube also has a policy that if you're idle, it will auto-pause.&lt;/p&gt;

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;With Python, you can code a script to press the key for a certain time range.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;progname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="s2"&gt;"\nRunning with [bold cyan]{}[/bold cyan] key."&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nb"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;no_lock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;KeyboardInterrupt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\n[Process Stopped]"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;flush&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;except&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Only works on Windows :)&lt;/p&gt;

&lt;h3&gt;
  
  
  Full Code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/amzar96/prevent_lock_screen" rel="noopener noreferrer"&gt;Github&lt;/a&gt; follow the installation step in README.md&lt;/p&gt;

</description>
      <category>crypto</category>
      <category>blockchain</category>
      <category>offers</category>
    </item>
    <item>
      <title>I build my own Shopee discount alert on Slack channel!</title>
      <dc:creator>Amzar</dc:creator>
      <pubDate>Tue, 31 Jan 2023 17:32:13 +0000</pubDate>
      <link>https://dev.to/amzar96/i-build-my-own-shopee-discount-alert-for-slack-channel-2peg</link>
      <guid>https://dev.to/amzar96/i-build-my-own-shopee-discount-alert-for-slack-channel-2peg</guid>
      <description>&lt;p&gt;I love discounts and I bet you are too. My fav online shopping platform is &lt;a href="https://shopee.com.my/" rel="noopener noreferrer"&gt;Shopee MY&lt;/a&gt;. But, how come I can always 24/7 checking on the App to get notified when the seller promotes the products with DISCOUNT? It is not works for me.&lt;/p&gt;

&lt;p&gt;So, I build my own Shopee alert.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to get the information?
&lt;/h3&gt;

&lt;p&gt;The first thing is I need to get the information from the Shopee. But, do I have the Shopee API? No. I don't. My next option is &lt;strong&gt;web scraping&lt;/strong&gt;. By using &lt;code&gt;requests&lt;/code&gt; and &lt;code&gt;BeautifulSoup&lt;/code&gt;, it is possible to do so with a few tricks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;headers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="s2"&gt;"User-Agent"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Mozilla/5.0 (X11; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"X-Requested-With"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"XMLHttpRequest"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;"Referer"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;URL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;soup&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;BeautifulSoup&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"html.parser"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Where do I store the information?
&lt;/h3&gt;

&lt;p&gt;Next, I must &lt;strong&gt;store the data&lt;/strong&gt; to compare the price/discount before sending the alert. Why do we always need the alert when the price is not the lowest?&lt;/p&gt;

&lt;p&gt;I build for my own use; I just need a simple database. Below is the database in my list:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Google Sheet &lt;/li&gt;
&lt;li&gt;Baserow ✅&lt;/li&gt;
&lt;li&gt;Excel and store in AWS S3&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After few consideration, I choose to use &lt;a href="https://baserow.io/" rel="noopener noreferrer"&gt;Baserow&lt;/a&gt;. But why?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;open-source with API capability&lt;/li&gt;
&lt;li&gt;backend using Postgres&lt;/li&gt;
&lt;li&gt;better admin panel
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="s2"&gt;"{base_url}database/rows/table/{table_id}/?user_field_names=true"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status_code&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;res_data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"results"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;df&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pd&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DataFrame&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;res_data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Notification or alert
&lt;/h3&gt;

&lt;p&gt;If the price is the lowest (compared with the historical data), it should alert me.&lt;/p&gt;

&lt;p&gt;For this project, I am using Slack channel to get the alert. Slack is one of the easiest alert integration compared to Telegram or Discord.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sendToSlack&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;itemRes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;no_data&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;False&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;no_data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getPayloadEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getPayload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;itemRes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="n"&gt;slack_webhook&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SLACK_URL"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;requests&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;slack_webhook&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;json&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's see how the alert looks like&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F779cmdz2gbcqtjrpnxd9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F779cmdz2gbcqtjrpnxd9.png" alt="Slack alert" width="800" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's contain the discount and link to the product.&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploy the script
&lt;/h3&gt;

&lt;p&gt;I am using &lt;strong&gt;cron job&lt;/strong&gt; running in the AWS Lightsail instance. Schedule to run the &lt;code&gt;bash&lt;/code&gt; script every 3rd hour from 1 AM to 11 PM.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;PYTHONPATH&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/home/amzar/Documents/python_env/shopee-tracker/bin/python3 

/home/amzar/Documents/python_env/shopee-tracker/bin/python3 

/home/amzar/Documents/shopee-tracker/app.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cron expression &lt;code&gt;0 1-23/3 * * * /home/amzar/scripts/run_shopee_tracker.sh&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Full Code
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/amzar96/shopee-tracker" rel="noopener noreferrer"&gt;Github&lt;/a&gt; follow the installation step in README.md&lt;/p&gt;

</description>
      <category>gratitude</category>
    </item>
    <item>
      <title>requests.get (python)</title>
      <dc:creator>Amzar</dc:creator>
      <pubDate>Tue, 31 Jan 2023 16:55:06 +0000</pubDate>
      <link>https://dev.to/amzar96/requestsget-python-435p</link>
      <guid>https://dev.to/amzar96/requestsget-python-435p</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4kvldo8yw3miwvxk049.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr4kvldo8yw3miwvxk049.png" alt="python script" width="800" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;get&lt;/strong&gt; is the method to retrieve information from the API. You can choose the result in JSON or raw text format. &lt;/p&gt;

</description>
      <category>python</category>
      <category>api</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
