Web services in the console

I spend a lot of my day in a terminal window, and I love automating stuff with bash scripts. Sometimes those scripts need to do perform actions based on external conditions: location (country), weather, bandwidth speed …

Here are a couple of terminal-ready web services that can give you external information. You can interrogate them from the command-line (CLI). For this you will need a CLI web client like curl, wget or http; at least one of those should already be installed on your MacOS/Linux/Win10 WSL terminal.

IP address/location

There is the website ifconfig.co, run by Martin Polden (Norway), based on his mpolden/echoip project (written in Go). It will give you your IP4/IP6 network address, and based on that, also your approximate geographical location.

  • curl ifconfig.co : 2a02:1811:142c:6000:e968:27b8:17d6:10bd
  • curl ifconfig.co/city : “Vilvoorde” (your nearest city)
  • curl ifconfig.co/country : “Belgium” (your country or the one your IP address seems to indicate)
  • curl ifconfig.co/country-iso : “BE” (your country code)

Alternatives: ip-api.com, ifconfig.io, ipinfo.io

Weather

Igor Chubin has developed a neat ASCII weather forecast service wttr.in that works great in CLI mode. It runs on Python and the code is on chubin/wttr.in.

  • curl wttr.in : get the weather for the location your computer is at
  • curl wttr.in/London : get the weather for London
  • curl "wttr.in/?format=4" ; “Vilvoorde, Belgium: ⛅️  🌡️+14°C 🌬️→31km/h”
  • curl "wttr.in/?format=%l+:+%d+%m" : “Vilvoorde, Belgium : 22:38:16 🌕” (sunset timing & moon phase)

Developer help

Igor also developed cheat.sh (github: chubin/cheat.sh) which allows a fast way to answer “how does XYZ work again?”

  • curl cht.sh/rsync : show usage for rsync
  • curl cht.sh/php/facade : what is a PHP facade for again?
  • curl cht.sh/go/Channels : how do Go channels work again?

Create QR code

Another genius project of Igor Chubin: qrenco.de (github)

Bandwidth speed

Well this is a bit different, because you can’t measure internet speed with just a call to an external service.

There is sindresorhus/fast-cli which uses the Fast.com (Netflix) servers, but it’s a huge npm library (with puppeteer, i.e. a full headless browser under the hood) you have to install on your local computer,

A bit lighter is sivel/speedtest-cli, which is a Python script that uses the servers from Speedtest. It is lighter in that it fetches the last version of the code from Github (with curl) and feeds this right into a python interpreter. Obviously you need Python on your machine.

$ curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python -
Retrieving speedtest.net configuration...
Testing from Telenet (94.225.186.144)...
Retrieving speedtest.net server list...
Selecting best server based on ping...
Hosted by Proximus (Schaarbeek) [7.34 km]: 19.867 ms
Testing download speed...
Download: 128.29 Mbit/s
Testing upload speed...
Upload: 18.26 Mbit/

Dictionary lookup

$ curl dict://dict.org/d:albeit
150 1 definitions retrieved
151 "Albeit" gcide "The Collaborative International Dictionary of English v.0.48"
Albeit \Al`be"it\, conj. [OE. al be although it be, where al is
   our all. Cf. {Although}.]
   Even though; although; notwithstanding. --Chaucer.
   [1913 Webster]


         Albeit so masked, Madam, I love the truth. --Tennyson.
   [1913 Webster]

Create short URL

$ curl -F shorten=https://blog.forret.com https://ttm.sh 
https://ttm.sh/hv6

Publish a file (image)

 $ curl -F'file=@test.jpg' https://0x0.st
https://0x0.st/iOC_.jpg
$ curl --upload-file ./results.txt https://transfer.sh/results.txt https://transfer.sh/66nb8/results.txt

Exchange rates

Another CLI service by Igor chubin : rate.sx for cryptocurrency exchange rates.

I had to search a bit but I also found an API for ‘regular’ currency exchange rates: exchangeratesapi.io (via alexanderepstein/Bash-Snippets)

Symbolic maths

The Newton web service (Github) by Gerald Nash allows you to do things like:

$ curl https://newton.now.sh/derive/x\^3+2x\^2
{"operation":"derive","expression":"x^3+2x^2","result":"3 x^2 + 4 x"}

$ curl https://newton.now.sh/factor/3x\^3+x\^2 
{"operation":"factor","expression":"3x^3+x^2","result":"x^2 (3 x + 1)"}

$ curl https://newton.now.sh/zeroes/x\^3\-2x\^2
{"operation":"zeroes","expression":"x^3-2x^2","result":[0,2]}      

Music APIs

$ curl https://api.lyrics.ovh/v1/prince/purple-rain
{"lyrics":"I never meant to cause you any sorrow\r\n(...)purple rain"}

$ curl "https://tastedive.com/api/similar?q=steely+dan"
{"Similar": {"Info": [{"Name": "Steely Dan", "Type": "music"}], "Results": [{"Name": "Donald Fagen", "Type": "music"}, (...){"Name": "Looking Glass", "Type": "music"}]}}

Using web services in a bash script

If you want to use these web tools in a script, you can easily put their output into a bash variable:

city=$(curl -s ifconfig.co/city)      # -s = silent
city=$(wget -q -O- ifconfig.co/city). # -q = quiet, -O- = to stdout
city=$(http --ignore-stdin ifconfig.co/city) # don't read from stdin

If the web service returns JSON instead of plain text, you will need jq to filter the data you want:

$ curl -s "https://newton.now.sh/derive/x^3+2x^2" | jq -r '.result'
3 x^2 + 4 x

$ curl -s "https://api.exchangeratesapi.io/latest?base=EUR" | jq '.rates.USD'
1.133

More console web services can be found on https://github.com/chubin/awesome-console-services

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.