我想从终端上查找特定城市的当前天气或我的位置。有命令行天气应用吗?
我想从终端上查找特定城市的当前天气或我的位置。有命令行天气应用吗?
Answers:
只需在终端中输入以下内容:
curl wttr.in
并从获取您的位置/etc/timezone
。否则curl wttr.in/your_location
。例如,对于德黑兰:
curl wttr.in/tehran
这给你:
您还可以比较两个城市:
diff -Naur <(curl -s http://wttr.in/london ) <(curl -s http://wttr.in/new-york )
开发人员的源代码可以在Github上找到。
devs
我们之间的关系还不错
在http://www.accuweather.com上搜索您的城市,并将以下脚本中的URL替换为您城市的URL:
#!/bin/sh
URL='http://www.accuweather.com/en/de/berlin/10178/weather-forecast/178087'
wget -q -O- "$URL" | awk -F\' '/acm_RecentLocationsCarousel\.push/{print $2": "$16", "$12"°" }'| head -1
样本输出:
Berlin, Germany: Foggy, 1°
acm_RecentLocationsCarousel.push({name:"Berlin, Germany", daypart:'day', href:'/en/de/berlin/10178/weather-forecast/178087', icon:'i-6-l', bg:'c', temp:'0', realfeel:'-6', text:"Mostly cloudy"});
。使用您的代码,awk
字段分隔符为''(空格)。是$12
(温度)第12栏?
awk
分隔符是'
。api使用2个数字,第一个是postleitzahl
或邮政编码。第二似乎是一个API代码数
我还有另外一种方法。
打开.bashrc
文件,然后将此代码粘贴到底部
weather(){ curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=${@:-<YOURZIPORLOCATION>}"|perl -ne '/<title>([^<]+)/&&printf "%s: ",$1;/<fcttext>([^<]+)/&&print $1,"\n"';}
然后保存并关闭.bashrc
文件。
现在键入bash
以更新文件。
然后输入
weather <location name>
例如
august@august-OEM:~$ weather guntur
December 14, 2013: Clear. High 31&deg;C (87&deg;F). Winds 0 kph North
December 15, 2013: Clear. High 29&deg;C (84&deg;F). Winds 10 kph NNW
December 16, 2013: Clear. High 31&deg;C (87&deg;F). Winds 10 kph North
December 17, 2013: Clear. High 29&deg;C (84&deg;F). Winds 7 kph ENE
December 18, 2013: Scattered Clouds. High 29&deg;C (84&deg;F). Winds 3 kph ENE
December 19, 2013: Scattered Clouds. High 29&deg;C (84&deg;F). Winds 3 kph ENE
YOUZIPORLOCATION
使用$1
,并通过20001
(华盛顿邮政编码),我得到的是看起来像格式化线Today: Mostly sunny and breezy. Highs in the upper 30s. Northwest winds 20 to 25 mph with gusts up to 45 mph.
当我使用"washington.dc"
,我得到表输出May 4, 2015: Clear. High 32&deg;C (89&deg;F). Winds 43 kph East
。知道为什么会有区别吗?
这是一个用Go语言编写的出色的半图形命令行实用程序:
https://github.com/schachmat/wego/
您需要安装Go并设置一些API东西,但是其中的指示信息已经存在。这是一个示例图片:
curl wttr.in
够了
curl wttr.in/Helsinki?m
首先,您需要安装weather-util软件包,只需按键盘上的Ctrl+ Alt+ T即可打开Terminal。打开时,运行以下命令:
sudo apt-get install weather-util
您需要当地的天气代码。
安装后即可运行weather -i <code>
。您从上面的链接获得的代码。有关可用选项的列表,您可以运行weather
天气信息
Last updated Dec 11, 2013 - 07:00 AM EST / 2013.12.11 1200 UTC Temperature: 86 F (30 C)
没有办法知道当前的天气。
weather YOURLOCATIONNAME
它先搜索您的位置,然后可能会提供多个结果。从这些中,您可以读取站标识符,您可以将其用作自变量,以从
weather LAX
(这应该显示了洛杉矶国际机场的天气)显示404错误weather.noaa.gov/pub/data/observations/metar/decoded/KNKX.TXT
ansiweather
AnsiWeather是一个Shell脚本,用于显示终端中的当前天气状况,并支持ANSI颜色和Unicode符号。天气数据来自OpenWeatherMap免费天气API。
sudo apt-get install ansiweather
ansiweather -l London,GB -f 3
London forecast => Sat Jan 13: 7/2 °C ☔ - Sun Jan 14: 4/1 °C ☔ - Mon Jan 15: 9/6 °C ☔
此外,预装了ubuntu的名为inxi的程序还将为您提供计算机上的所有类型的统计信息,并进行天气输出。
命令:inxi --help
命令:inxi -w
Conditions: 82 F (28 C) - Clear Time: May 13, 10:52 AM CDT
命令:inxi -wxxx
Conditions: 82 F (28 C) - Clear Wind: From the SW at 13 MPH Humidity: 60%
Pressure: 29.99 in (1016 mb) Heat Index: 84 F (29 C)
Location: city (USA) Altitude: 185 m
Time: May 13, 10:52 AM CDT (America/Chicago) Observation Time: May 13, 9:54 AM CDT
sudo apt install inxi
inxi
可用,但是您需要首先启用Universe资源库。
您可以使用以下方法比较城市:
diff -Naur <(curl -s http://wttr.in/london ) <(curl -s http://wttr.in/new-york )
如最受好评的答案所示。wttr.in
每次您打开终端时,也会显示一个很棒的“启动”屏幕。为此,请将其添加到~/.bashrc
文件中。我这样做是为了包括天气,日期,时间和分布信息,如以下答案中所述:如何获得此终端启动屏幕?
抱歉,当我捕获此映像时,我在Windows 10 WSL的Ubuntu 2018年春季更新中。保证我很快会重新启动Linux下的Ubuntu。
对于更短的天气输出,http://weather.tuxnet24.de提供了Yahoo!的天气数据。以文本和xml格式。
然后可以使用XMLlin解析器(如xmllint ie)来获取XML的内容。
我编写了一个q&d Weather-Fetch脚本,该脚本获取当前温度和天气描述,然后存储与该描述对应的Unicode符号,并将其输出为
☂6°摄氏度
我定期通过cronjob更新,然后在托盘状态下使用输出。
在yahoocom / news / weather上获取您的ID。将位置更改为所需位置,然后在URL中查找ID(即,如果URL为yahoocom / news / weather / germany / north-rhine-westphalia / bielefeld-20066057,则ID为20066057)。
XML包含其他字段,即预报数据,风等。如果需要,您也可以解析它们。
不幸的是,Ubuntufont中未显示某些天气符号……随时进行调整。
这是我的脚本(如果您不住在比勒费尔德,用您的ID替换20066057)-取决于bash,curl和xmllint。
#!/bin/sh
#☁☂☔❄❅❆☃ ☀☁☂⚡⚐☼
# write xml to variable
w_xml=$(curl -Ls "http://weather.tuxnet24.de/?id=20066057&mode=xml");
# get fields from xml via xmllint | xargs for trimming
# weather description
w_txt=$(xmllint --xpath "string(//current_text)" - <<<"$w_xml" | xargs);
# temperature | remove spaces from text (°C prepended by space)
w_tpc=$(xmllint --xpath "string(//current_temp)" - <<<"$w_xml" | xargs); w_tpc=${w_tpc//[[:blank:]]/};
# further fields not used atm
# w_tph=$(xmllint --xpath "string(//current_temp_high)" - <<<"$w_xml" | xargs);
# w_tpl=$(xmllint --xpath "string(//current_temp_low)" - <<<"$w_xml" | xargs);
# set $w_sym according to $w_txt
if [ "$w_txt" == "Sunny" ]; then w_sym="☼";
elif [ "$w_txt" == "Mostly Sunny" ]; then w_sym="☼";
elif [ "$w_txt" == "Showers" ]; then w_sym="☂";
elif [ "$w_txt" == "Clear" ]; then w_sym="☾";
elif [ "$w_txt" == "Thunderstorms" ]; then w_sym="⚡";
elif [ "$w_txt" == "Scattered Thunderstorms" ]; then w_sym="☔";
elif [ "$w_txt" == "Isolated Thundershovers" ]; then w_sym="☔";
elif [ "$w_txt" == "Cloudy" ]; then w_sym="☁";
elif [ "$w_txt" == "Mostly Cloudy" ]; then w_sym="☁";
elif [ "$w_txt" == "Partly Cloudy" ]; then w_sym="☼☁";
elif [ "$w_txt" == "Breezy" ]; then w_sym="⚐";
# if unknown text, set text instead of symbol
else w_sym=$w_txt;
fi
# output <symbol><space><temp-in-°C>
echo "$w_sym"" ""$w_tpc";
#!/usr/bin/env bash
固定版本,Ubuntu 18.04
我还有另外一种方法。
打开您的.bashrc文件,然后将此代码粘贴到底部
test -f ~/.wttr.in || curl -sk wttr.in -o ~/.wttr.in
find ~ -maxdepth 1 -name .wttr.in -cmin +5 -exec curl -sk wttr.in -o ~/.wttr.in \;
head -7 ~/.wttr.in | tail -5
W(){ find ~ -maxdepth 1 -name .wttr.in -cmin +5 -exec curl -sk wttr.in -o ~/.wttr.in \;; head -27 ~/.wttr.in; }
保存并关闭.bashrc
文件,然后运行以下命令来更新bash:
. .bashrc
然后输入W
大写
.bashrc
文件,则无需重新登录。您可以运行:source .bashrc
或者您可以运行它:. .bashrc
尽管AccuWeather curl解决方案非常好,但我需要更多信息,因此我创建了简单的bash脚本,该脚本可从weather.com网站获取接下来4个小时的信息。与前面的示例一样,您必须修改位置的链接。
Example output:
$ ./getWeather.sh
Temperature for 1 AM : 65°F and outside should be: Partly - Cloudy FEELS LIKE: 65°
Temperature for 2 AM : 65deg;F and outside should be: Partly - Cloudy FEELS LIKE: 65deg;
Temperature for 3 AM : 63deg; and outside should be: Partly - Cloudy FEELS LIKE: 63deg;
Temperature for 4 AM : 62deg; and should be: Mostly - Clear FEELS LIKE: 62deg;
完整脚本位于:https : //sites.google.com/site/wozoopalinux/home/wzbash001/get-weather-information-from-command-line-for-next-4-hrs
finger city_name@graph.no
$ finger cologne@graph.no
-= Meteogram for germany/north_rhine-westphalia/cologne =-
'C Rain
16
15 ^^^
14 ======^^^ ^^^
13 === ^^^===^^^===
12 =========
11 === ====--
10 ====== ---
9=====| --- 3 mm
8 | | 2 mm
7 | | | | | | | | | | | | | | 1 mm
_08_09_10_11_12_13_14_15_16_17_18 19 20 21 22 23 00 01 02 03 04 05 Hour
SE SE SE SE SE SW SW W W W W SW W W W W W NW NW NW N N Wind dir.
5 5 5 5 4 4 5 6 6 6 5 5 5 4 4 5 5 5 4 3 3 3 Wind(mps)
Legend left axis: - Sunny ^ Scattered = Clouded =V= Thunder # Fog
Legend right axis: | Rain ! Sleet * Snow
[Weather forecast from yr.no, delivered by the Norwegian Meteorological Institute and the NRK.]
使用方法如下:
$ finger help@graph.no
yr.no is having technical problems, or you specified an unknown location.
Usage:
* finger <city name>@graph.no (world weather forecast, no spaces)
Example: finger newyork@graph.no
Advanced usage:
* finger o:<city name>@graph.no (a one-line forecast)
Example: finger o:newyork@graph.no
* finger ^<city name>@graph.no (Imperial units)
Example: finger ^newyork@graph.no
* finger <city name>+5@graph.no (forecast from 5 hrs ahead in time (max:26))
Example: finger northpole+5@graph.no
* finger <city name>~160@graph.no (set screen width)
Example: finger southpole~160@graph.no
* finger <city name>%2@graph.no (forecast for every second hour [Norway])
Example: finger oslo%2@graph.no
* finger <post code>@graph.no (norwegian weather forecast)
Example: finger 0458@graph.no
Other:
* finger news@graph.no (latest headlines from NRK)
* finger time@graph.no (server local time)
* finger date@graph.no (server local date)
* finger about@graph.no (contact information)
International names comes from http://fil.nrk.no/yr/viktigestader/verda.txt.