blog.lazkani.io/content/posts/the-story-behind-cmw.md

160 lines
7.1 KiB
Markdown
Raw Normal View History

+++
title = "The Story Behind cmw"
author = ["Elia el Lazkani"]
date = 2019-08-31
lastmod = 2019-08-31
tags = ["python", "development"]
categories = ["misc"]
draft = false
+++
A few days ago, [Kushal Das](https://kushaldas.in) shared a curl command.
The command was as follows:
```text
$ curl https://wttr.in/
```
I, obviously, was curious.
I ran it and it was interesting.
So it returns the weather right ? Pretty cool huh!
<!--more-->
## The interest {#the-interest}
That got me interested to learn how does this work exactly.
## The investigation {#the-investigation}
I looked at [https://wttr.in/](https://wttr.in/) and it seemed to have a GitHub [link](https://github.com/chubin/wttr.in) and a repository.
That is very interesting.
This is a Python application, one can tell by the code or if you prefer the GitHub bar at the top.
Anyway, one can also tell that this is a [Flask](https://palletsprojects.com/p/flask/) application from the following code in the bin/srv.py.
```python
from flask import Flask, request, send_from_directory
APP = Flask(__name__)
```
By reading the README.md of the repository one can read.
> wttr.in uses [wego](http://github.com/schachmat/wego) for
> visualization and various data sources for weather forecast
> information.
Let's jump to the _wego_ repository then.
_wego_ seems to be a command line application to graph the weather in the terminal.
Great, so what I did with [cmw](https://gitea.project42.io/Elia/cmw) is already done in Go and API'fied by a different project.
My answer to that accusation is obviously this post.
## The idea {#the-idea}
I played a bit more with [https://wttr.in/](https://wttr.in/) and I found it to an interesting API.
I am trying to work on my python development foo so to me that was a perfect little project to work on.
From my perspective this was simply an API and I am to consume it to put it back in my terminal.
## The work {#the-work}
The beginning work was very rough and hidden away in a private repository and was moved later [here](https://gitea.project42.io/Elia/cmw).
The only thing left from that work is the `--format` argument which allows you full control over what gets sent.
But again, let's not forget what the real purpose of this project was.
So I decided to make the whole API as accessible as possible from the command line tool I am writing.
```text
$ cmw --help
usage: cmw [-h] [-L LOCATION] [-f FORMAT] [-l LANG] [-m] [-u] [-M] [-z] [-o]
[-w] [-A] [-F] [-n] [-q] [-Q] [-N] [-P] [-p] [-T] [-t TRANSPARENCY]
[--v2] [--version]
Get the weather!
optional arguments:
-h, --help show this help message and exit
-L LOCATION, --location LOCATION
Location (look at epilog for more information)
-f FORMAT, --format FORMAT
Query formatting
-l LANG, --lang LANG The language to use
-m, --metric Units: Metric (SI) (default outside US)
-u, --uscs Units: USCS (default in US)
-M, --meter-second Units: Show wind speed in m/s
-z, --zero View: Only current weather
-o, --one View: Current weather & one day
-w, --two View: Current weather & two days
-A, --ignore-user-agent
View: Force ANSI output format
-F, --follow-link View: Show the 'Follow' line from upstream
-n, --narrow View: Narrow version
-q, --quiet View: Quiet version
-Q, --super-quiet View: Super quiet version
-N, --no-colors View: Switch terminal sequences off
-P, --png PNG: Generate PNG file
-p, --add-frame PNG: Add frame around output
-T, --mid-transparency
PNG: Make transparency 150
-t TRANSPARENCY, --transparency TRANSPARENCY
PNG: Set transparency between 0 and 255
--v2 v2 interface of the day
--version show program's version number and exit
Supported Location Types
------------------------
City name: Paris
Unicode name: Москва
Airport code (3 letters): muc
Domain name: @stackoverflow.com
Area code: 94107
GPS coordinates: -78.46,106.79
Special Location
----------------
Moon phase (add ,+US
or ,+France
for these cities): moon
Moon phase for a date: moon@2016-10-25
Supported languages
-------------------
Supported: af da de el et fr fa hu id it nb nl pl pt-br ro ru tr uk vi
```
```text
$ cmw --location London --lang nl --one
Weerbericht voor: London
\ / Zonnig
.-. 20 °C
― ( ) ― → 19 km/h
`- 10 km
/ \ 0.0 mm
┌─────────────┐
┌──────────────────────────────┬───────────────────────┤ za 31 aug ├───────────────────────┬──────────────────────────────┐
│ 's Ochtends │ 's Middags └──────┬──────┘ 's Avonds │ 's Nachts │
├──────────────────────────────┼──────────────────────────────┼──────────────────────────────┼──────────────────────────────┤
│ \ / Gedeeltelijk b…│ \ / Gedeeltelijk b…│ Bewolkt │ \ / Gedeeltelijk b…│
_ /"".-. 21 °C │ _ /"".-. 23..24 °C │ .--. 20 °C │ _ /"".-. 18 °C │
│ \_( ). ↗ 12-14 km/h │ \_( ). ↗ 18-20 km/h │ .-( ). ↗ 20-25 km/h │ \_( ). → 16-19 km/h │
│ /(___(__) 10 km │ /(___(__) 10 km │ (___.__)__) 10 km │ /(___(__) 10 km │
│ 0.0 mm | 0% │ 0.0 mm | 0% │ 0.0 mm | 0% │ 0.0 mm | 0% │
└──────────────────────────────┴──────────────────────────────┴──────────────────────────────┴──────────────────────────────┘
Locatie: London [51.509648,-0.099076]
```
## Conclusion {#conclusion}
All I got to say in conclusion is that it was a lot of fun working on [cmw](https://gitea.project42.io/Elia/cmw) and I learned a lot.
I'm not going to publish the package on [PyPI](https://pypi.org/) because seriously, what's the point.
But if you are interested in making changes to the repository, make an MR.