mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-13 22:30:37 +08:00
translate done: 20171013 Get Your Weather Forecast From the Linux CLI.md
This commit is contained in:
parent
b988cd607c
commit
b3f0125ff0
@ -1,140 +0,0 @@
|
||||
translating by lujun9972
|
||||
Get Your Weather Forecast From the Linux CLI
|
||||
======
|
||||
|
||||
### Objective
|
||||
|
||||
Display the current weather forecast in the Linux command line.
|
||||
|
||||
### Distributions
|
||||
|
||||
This will work on any Linux distribution.
|
||||
|
||||
### Requirements
|
||||
|
||||
A working Linux install with an Internet connection.
|
||||
|
||||
### Difficulty
|
||||
|
||||
Easy
|
||||
|
||||
### Conventions
|
||||
|
||||
* **#** \- requires given command to be executed with root privileges either directly as a root user or by use of `sudo` command
|
||||
* **$** \- given command to be executed as a regular non-privileged user
|
||||
|
||||
|
||||
|
||||
### Introduction
|
||||
|
||||
It's be convenient to be able to retrieve the latest weather forecast right from your terminal without opening up a web browser, wouldn't it? What about scripting it or setting a cron job? Well, you can.
|
||||
|
||||
`http://wttr.in` is a website that allows you to search for weather forecasts anywhere in the world, and it displays he results in ASCII characters. By using `cURL`, you can access `http://wttr.in`, you can get your results directly in the terminal.
|
||||
|
||||
### Get Your Local Weather
|
||||
|
||||
|
||||
|
||||
![Local weather from wttr.in][1]
|
||||
It's really simple to grab your local weather. `wttr.in` will automatically try to detect your location based on your IP address. It's reasonably accurate, unless you're using a VPN, of course.
|
||||
```
|
||||
|
||||
$ curl wttr.in
|
||||
|
||||
```
|
||||
|
||||
### Get Weather By City
|
||||
|
||||
|
||||
|
||||
![Weather by city from wttr.in][2]
|
||||
Now, if you would like the weather in a different city, you can specify that with a slash at the end of `wttr.in`. Replace any spaces in the name with a `+`.
|
||||
```
|
||||
|
||||
$ curl wttr.in/New+York
|
||||
|
||||
```
|
||||
|
||||
You can also specify cities the way they're written in Unix timezones.
|
||||
```
|
||||
|
||||
$ curl wttr.in/New_York
|
||||
|
||||
```
|
||||
|
||||
Don't use spaces unless you like strange and inaccurate results.
|
||||
|
||||
### Get Weather By Airport
|
||||
|
||||
|
||||
|
||||
![Weather by airport from wttr.in][3]
|
||||
If you're familiar with the three letter airport codes in your area, you can use those too. They might be closer to you and more accurate than the city in general.
|
||||
```
|
||||
|
||||
$ curl wttr.in/JFK
|
||||
|
||||
```
|
||||
|
||||
### Best Guess
|
||||
|
||||
|
||||
|
||||
![Weather by landmark from wttr.in][4]
|
||||
You can have `wttr.in` take a guess on the weather base on a landmark using the `~` character.
|
||||
```
|
||||
|
||||
$ curl wttr.in/~Statue+Of+Liberty
|
||||
|
||||
```
|
||||
|
||||
### Weather From A Domain Name
|
||||
|
||||
|
||||
|
||||
![Weather by domain name from wttr.in][5]
|
||||
Did you ever wonder what the weather is like where LinuxConfig is hosted? Now, now you can find out! `wttr.in` can check weather by domain name. Sure, it's probably not the most useful feature, but it's still interesting none the less.
|
||||
```
|
||||
|
||||
$ curl wttr.in/@linuxconfig.org
|
||||
|
||||
```
|
||||
|
||||
### Changing The Temperature Units
|
||||
|
||||
|
||||
|
||||
![Change unit system in wttr.in][6]
|
||||
By default, `wttr.in` will display temperatures in the units(C or F) used in your actual location. Basically, in States, you'll get Fahrenheit, and everyone else will see Celsius. You can change that by adding `?u` to see Fahrenheit or `?m` to see Celsius.
|
||||
```
|
||||
|
||||
$ curl wttr.in/New_York?m
|
||||
|
||||
$ curl wttr.in/Toronto?u
|
||||
|
||||
```
|
||||
|
||||
There's an odd bug with ZSH that prevents this from working, so you need to use Bash if you want to convert the units.
|
||||
|
||||
### Closing Thoughts
|
||||
|
||||
You can easily incorporate a call to `wttr.in` into a script, cron job, or even your MOTD. Of course, you don't need to get that involved. You can just lazily type a call in to this awesome service whenever you want to check the forecast.
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://linuxconfig.org/get-your-weather-forecast-from-the-linux-cli
|
||||
|
||||
作者:[Nick Congleton][a]
|
||||
译者:[lujun9972](https://github.com/lujun9972)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://linuxconfig.org
|
||||
[1]:https://linuxconfig.org/images/wttr-local.jpg
|
||||
[2]:https://linuxconfig.org/images/wttr-city.jpg
|
||||
[3]:https://linuxconfig.org/images/wttr-airport.jpg
|
||||
[4]:https://linuxconfig.org/images/wttr-landmark.jpg
|
||||
[5]:https://linuxconfig.org/images/wttr-url.jpg
|
||||
[6]:https://linuxconfig.org/images/wttr-units.jpg
|
@ -0,0 +1,113 @@
|
||||
在 Linux 字符界面中获取天气预报
|
||||
======
|
||||
|
||||
**目标:**使用 Linux 命令行显示天气预报。
|
||||
|
||||
**发行版:**所有 Linux 发行版。
|
||||
|
||||
**要求:**能连上因特网的 Linux
|
||||
|
||||
**难度:**容易
|
||||
|
||||
**约定:**
|
||||
|
||||
* `#` - 需要使用 root 权限来执行指定命令,可以直接使用 root 用户来执行也可以使用 `sudo` 命令
|
||||
* `$` - 可以使用普通用户来执行指定命令
|
||||
|
||||
### 简介
|
||||
|
||||
无需打开网页浏览器就能直接从终端获取最新的天气预报那该多方便啊,对吧?你还能把它写成脚本,或者设置定义定时任务。
|
||||
|
||||
`http://wttr.in` 是一个允许你搜索世界各地天气预报的网站,而且它的是以 ASCII 字符的形式来显示结果的。通过使用 `cURL` 访问 `http://wttr.in`,就能直接在终端显示查询结果了。
|
||||
|
||||
### 获取所在地的天气
|
||||
|
||||
![Local weather from wttr.in][1]
|
||||
|
||||
要抓取所在地的天气情况非常简单。`wttr.in` 会自动根据 IP 地址来探测你的所在地。除非你用了 VPN,否则它的精度还不错。
|
||||
```
|
||||
$ curl wttr.in
|
||||
```
|
||||
|
||||
### 获取指定城市的天气
|
||||
|
||||
![Weather by city from wttr.in][2]
|
||||
|
||||
你可以通过在 `wttr.in` 后加上斜杠和城市名称的方式来获得其他城市的天气情况。不过要把名字中的空格替换成`+`。
|
||||
```
|
||||
$ curl wttr.in/New+York
|
||||
```
|
||||
|
||||
你也可以以 Unix 时区的形式来填写城市名称。
|
||||
```
|
||||
$ curl wttr.in/New_York
|
||||
```
|
||||
|
||||
不要直接使用空格,否则会出现奇怪而不准确的结果。
|
||||
|
||||
### 获取机场天气
|
||||
|
||||
|
||||
|
||||
![Weather by airport from wttr.in][3]
|
||||
|
||||
若你对地区的三位机场代号很熟悉,你也可以使用机场代号来查询天气。一般来说使用机场要比使用城市更贴近你,而且更精确一些。
|
||||
```
|
||||
$ curl wttr.in/JFK
|
||||
```
|
||||
|
||||
### 猜测所在地
|
||||
|
||||
![Weather by landmark from wttr.in][4]
|
||||
|
||||
通过使用 `~` 字符,你可以让 `wttr.in` 通过地标来猜测天气情况。
|
||||
```
|
||||
$ curl wttr.in/~Statue+Of+Liberty
|
||||
```
|
||||
|
||||
### 域名所在地的天气
|
||||
|
||||
![Weather by domain name from wttr.in][5]
|
||||
|
||||
你想不想知道 LinuxConfig 托管地的天气?现在有一个方法可以知道!`wttr.in` 可以通过域名获取天气。是的,这个功能可能不那么实用,但这很有趣啊。
|
||||
```
|
||||
|
||||
$ curl wttr.in/@linuxconfig.org
|
||||
|
||||
```
|
||||
|
||||
### 更改温度单位
|
||||
|
||||
![Change unit system in wttr.in][6]
|
||||
|
||||
默认情况下,`wttr.in` 会根据你的实际地址来决定显示哪种温度单位 (C 还是 F)。基本上,在美国,使用的是华氏度,而其他地方显示的是摄氏度。你可以指定显示的温度单位,在 URL 后添加 `?u` 会显示华氏度,而添加 `?m` 会显示摄氏度。
|
||||
```
|
||||
$ curl wttr.in/New_York?m
|
||||
|
||||
$ curl wttr.in/Toronto?u
|
||||
```
|
||||
|
||||
在 ZSH 上有一个很奇怪的 bug,会使得这两条语句不能正常工厂,如果你需要更换单位,恐怕需要改成使用 Bash 了。
|
||||
|
||||
### 总结
|
||||
|
||||
你可以很方便地在脚本,定时任务,甚至 MOTD(LCTT 注:Message Of The Day 每日消息)中访问 `wttr.in`。当然,你完全没有必要这么做。当你需要查看天气预报的时候只需要访问一下这个超棒的网站就行了。
|
||||
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://linuxconfig.org/get-your-weather-forecast-from-the-linux-cli
|
||||
|
||||
作者:[Nick Congleton][a]
|
||||
译者:[lujun9972](https://github.com/lujun9972)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:https://linuxconfig.org
|
||||
[1]:https://linuxconfig.org/images/wttr-local.jpg
|
||||
[2]:https://linuxconfig.org/images/wttr-city.jpg
|
||||
[3]:https://linuxconfig.org/images/wttr-airport.jpg
|
||||
[4]:https://linuxconfig.org/images/wttr-landmark.jpg
|
||||
[5]:https://linuxconfig.org/images/wttr-url.jpg
|
||||
[6]:https://linuxconfig.org/images/wttr-units.jpg
|
Loading…
Reference in New Issue
Block a user