erikw / tmux-powerline

⚡️ A tmux plugin giving you a hackable status bar consisting of dynamic & beautiful looking powerline segments, written purely in bash.
BSD 3-Clause "New" or "Revised" License
3.41k stars 514 forks source link

yahoo-Weather isn't working #219

Closed int2001 closed 2 years ago

int2001 commented 8 years ago

Yahoo changed its API to OAuth... Please Fix. #

y9c commented 8 years ago

@int2001

change

weather_data=$(curl --max-time 4 -s "http://weather.yahooapis.com/forecastrss?w=${TMUX_POWERLINE_SEG_WEATHER_LOCATION}&u=${TMUX_POWERLINE_SEG_WEATHER_UNIT}")

to

weather_data=$(curl --max-time 4 -s "https://query.yahooapis.com/v1/public/yql?format=xml&q=SELECT%20*%20FROM%20weather.forecast%20WHERE%20u=%27${TMUX_POWERLINE_SEG_WEATHER_UNIT}%27%20AND%20woeid%20=%20%27${TMUX_POWERLINE_SEG_WEATHER_LOCATION}%27")

220

simbalion commented 8 years ago

seems to be broken again, says no location specified.

PauloDanielCarneiro commented 8 years ago

Any change? Here shows "No weather location specified". Is broken?

y9c commented 8 years ago

@simbalion @PauloDanielCarneiro please edit the file segments/weather.sh before use.

# Your location. Find a code that works for you:
# 1. Go to Yahoo weather http://weather.yahoo.com/
# 2. Find the weather for you location
# 3. Copy the last numbers in that URL. e.g. "http://weather.yahoo.com/united-states/california/newport-beach-12796587/" has the numbers "12796587"
export TMUX_POWERLINE_SEG_WEATHER_LOCATION=""
y9c commented 8 years ago

screenshot from 2016-08-23 14-16-45

PauloDanielCarneiro commented 8 years ago

I tried export TMUX_POWERLINE_SEG_WEATHER_LOCATION="12796587" and export TMUX_POWERLINE_SEG_WEATHER_LOCATION="455827". The result are the same: screenshot from 2016-08-23 05-55-45

y9c commented 8 years ago

@PauloDanielCarneiro Would you please replace the whole file with the script below, and try to source tmux.conf again.

# Prints the current weather in Celsius, Fahrenheits or lord Kelvins. The forecast is cached and updated with a period of $update_period.

# The update period in seconds.
update_period=600

TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT="yahoo"
TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT="c"
TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT="600"
TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT="grep"
TMUX_POWERLINE_SEG_WEATHER_LOCATION_DEFAULT="2161838"

generate_segmentrc() {
    read -d '' rccontents  << EORC
# The data provider to use. Currently only "yahoo" is supported.
export TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER="${TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT}"
# What unit to use. Can be any of {c,f,k}.
export TMUX_POWERLINE_SEG_WEATHER_UNIT="${TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT}"
# How often to update the weather in seconds.
export TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD="${TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT}"
# Name of GNU grep binary if in PATH, or path to it.
export TMUX_POWERLINE_SEG_WEATHER_GREP="${TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT}"

# Your location. Find a code that works for you:
# 1. Go to Yahoo weather http://weather.yahoo.com/
# 2. Find the weather for you location
# 3. Copy the last numbers in that URL. e.g. "http://weather.yahoo.com/united-states/california/newport-beach-12796587/" has the numbers "12796587"
export TMUX_POWERLINE_SEG_WEATHER_LOCATION="${TMUX_POWERLINE_SEG_WEATHER_LOCATION_DEFAULT}"
EORC
    echo "$rccontents"
}

run_segment() {
    __process_settings
    local tmp_file="${TMUX_POWERLINE_DIR_TEMPORARY}/weather_yahoo.txt"
    local weather
    case "$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER" in
        "yahoo") weather=$(__yahoo_weather) ;;
        *)
            echo "Unknown weather provider [${$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER}]";
            return 1
    esac
    if [ -n "$weather" ]; then
        echo "$weather"
    fi
}

__process_settings() {
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER="${TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_UNIT" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_UNIT="${TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD="${TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_GREP" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_GREP="${TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_LOCATION" ]; then
        echo "No weather location specified.";
        exit 8
    fi
}

__yahoo_weather() {
    degree=""
    if [ -f "$tmp_file" ]; then
        if shell_is_osx || shell_is_bsd; then
            last_update=$(stat -f "%m" ${tmp_file})
        elif shell_is_linux; then
            last_update=$(stat -c "%Y" ${tmp_file})
        fi
        time_now=$(date +%s)

        up_to_date=$(echo "(${time_now}-${last_update}) < ${update_period}" | bc)
        if [ "$up_to_date" -eq 1 ]; then
            __read_tmp_file
        fi
    fi

    if [ -z "$degree" ]; then
        #weather_data=$(curl --max-time 4 -s "http://weather.yahooapis.com/forecastrss?w=${TMUX_POWERLINE_SEG_WEATHER_LOCATION}&u=${TMUX_POWERLINE_SEG_WEATHER_UNIT}")
        weather_data=$(curl --max-time 4 -s "https://query.yahooapis.com/v1/public/yql?format=xml&q=SELECT%20*%20FROM%20weather.forecast%20WHERE%20u=%27${TMUX_POWERLINE_SEG_WEATHER_UNIT}%27%20AND%20woeid%20=%20%27${TMUX_POWERLINE_SEG_WEATHER_LOCATION}%27")
        if [ "$?" -eq "0" ]; then
            error=$(echo "$weather_data" | grep "problem_cause\|DOCTYPE");
            if [ -n "$error" ]; then
                echo "error"
                exit 1
            fi

            # Assume latest grep is in PATH
            gnugrep="${TMUX_POWERLINE_SEG_WEATHER_GREP}"

            # <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
            unit=$(echo "$weather_data" | "$gnugrep" -Zo "<yweather:units [^<>]*/>" | sed 's/.*temperature="\([^"]*\)".*/\1/')
            condition=$(echo "$weather_data" | "$gnugrep" -Zo "<yweather:condition [^<>]*/>")
            # <yweather:condition  text="Clear"  code="31"  temp="66"  date="Mon, 01 Oct 2012 8:00 pm CST" />
            degree=$(echo "$condition" | sed 's/.*temp="\([^"]*\)".*/\1/')
            condition=$(echo "$condition" | sed 's/.*text="\([^"]*\)".*/\1/')
            # Pull the times for sunrise and sunset so we know when to change the day/night indicator
            # <yweather:astronomy sunrise="6:56 am"   sunset="6:21 pm"/>
            if shell_is_osx || shell_is_bsd; then
                date_arg='-j -f "%H:%M %p "'
            else
                date_arg='-d'
            fi
            sunrise=$(date ${date_arg}"$(echo "$weather_data" | "$gnugrep" "yweather:astronomy" | sed 's/^\(.*\)sunset.*/\1/' | sed 's/^.*sunrise="\(.*m\)".*/\1/')" +%H%M)
            sunset=$(date ${date_arg}"$(echo "$weather_data" | "$gnugrep" "yweather:astronomy" | sed 's/^.*sunset="\(.*m\)".*/\1/')" +%H%M)
        elif [ -f "${tmp_file}" ]; then
            __read_tmp_file
        fi
    fi

    if [ -n "$degree" ]; then
        if [ "$TMUX_POWERLINE_SEG_WEATHER_UNIT" == "k" ]; then
            degree=$(echo "${degree} + 273.15" | bc)
        fi
        condition_symbol=$(__get_condition_symbol "$condition" "$sunrise" "$sunset") 
        echo "${condition_symbol} ${degree}°$(echo "$TMUX_POWERLINE_SEG_WEATHER_UNIT" | tr '[:lower:]' '[:upper:]')" | tee "${tmp_file}"
    fi
}

# Get symbol for condition. Available conditions: http://developer.yahoo.com/weather/#codes
__get_condition_symbol() {
    local condition=$(echo "$1" | tr '[:upper:]' '[:lower:]')
    local sunrise="$2"
    local sunset="$3"
    case "$condition" in
        "sunny" | "hot")
            hourmin=$(date +%H%M)
            if [ "$hourmin" -ge "$sunset" -o "$hourmin" -le "$sunrise" ]; then
                #echo "☽"
                echo "☾"
            else
                #echo "☀"
                echo "☼"
            fi
            ;;
        "rain" | "mixed rain and snow" | "mixed rain and sleet" | "freezing drizzle" | "drizzle" | "light drizzle" | "freezing rain" | "showers" | "mixed rain and hail" | "scattered showers" | "isolated thundershowers" | "thundershowers" | "light rain with thunder" | "light rain" | "rain and snow")
            #echo "☂"
            echo "☔"
            ;;
        "snow" | "mixed snow and sleet" | "snow flurries" | "light snow showers" | "blowing snow" | "sleet" | "hail" | "heavy snow" | "scattered snow showers" | "snow showers" | "light snow" | "snow/windy" | "snow grains" | "snow/fog")
            #echo "☃"
            echo "❅"
            ;;
        "cloudy" | "mostly cloudy" | "partly cloudy" | "partly cloudy/windy")
            echo "☁"
            ;;
        "tornado" | "tropical storm" | "hurricane" | "severe thunderstorms" | "thunderstorms" | "isolated thunderstorms" | "scattered thunderstorms")
            #echo "⚡"
            echo "☈"
            ;;
        "dust" | "foggy" | "fog" | "haze" | "smoky" | "blustery" | "mist")
            #echo "♨"
            #echo "﹌"
            echo "〰"
            ;;
        "windy" | "fair/windy")
            #echo "⚐"
            echo "⚑"
            ;;
        "clear" | "fair" | "cold")
            hourmin=$(date +%H%M)
            if [ "$hourmin" -ge "$sunset" -o "$hourmin" -le "$sunrise" ]; then
                echo "☾"
            else
                echo "〇"
            fi
            ;;
        *)
            echo "?"
            ;;
    esac
}

__read_tmp_file() {
    if [ ! -f "$tmp_file" ]; then
        return
    fi
    cat "${tmp_file}"
    exit

*BTW, please copy the output of [this link](https://query.yahooapis.com/v1/public/yql?format=xml&q=SELECT%20%20FROM%20weather.forecast%20WHERE%20u=%27c%27%20AND%20woeid%20=%20%272161838%27) from your browser, and paste them here.**

PauloDanielCarneiro commented 8 years ago

Same problem. The output:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2016-08-24T10:59:15Z" yahoo:lang="en-US">
<results>
<channel>
<yweather:units xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" distance="km" pressure="mb" speed="km/h" temperature="C"/>
<title>Yahoo! Weather - Guangzhou, Guangdong, CN</title>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2161838/
</link>
<description>Yahoo! Weather for Guangzhou, Guangdong, CN</description>
<language>en-us</language>
<lastBuildDate>Wed, 24 Aug 2016 06:59 PM CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" city="Guangzhou" country="China" region=" Guangdong"/>
<yweather:wind xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" chill="86" direction="113" speed="11.27"/>
<yweather:atmosphere xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" humidity="80" pressure="33897.76" rising="0" visibility="25.91"/>
<yweather:astronomy xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" sunrise="6:7 am" sunset="6:51 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>
http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif
</url>
</image>
<item>
<title>
Conditions for Guangzhou, Guangdong, CN at 06:00 PM CST
</title>
<geo:lat xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">23.107389</geo:lat>
<geo:long xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">113.267616</geo:long>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2161838/
</link>
<pubDate>Wed, 24 Aug 2016 06:00 PM CST</pubDate>
<yweather:condition xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="4" date="Wed, 24 Aug 2016 06:00 PM CST" temp="30" text="Thunderstorms"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="4" date="24 Aug 2016" day="Wed" high="33" low="27" text="Thunderstorms"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="4" date="25 Aug 2016" day="Thu" high="33" low="27" text="Thunderstorms"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="4" date="26 Aug 2016" day="Fri" high="32" low="27" text="Thunderstorms"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="4" date="27 Aug 2016" day="Sat" high="30" low="26" text="Thunderstorms"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="4" date="28 Aug 2016" day="Sun" high="31" low="25" text="Thunderstorms"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="32" date="29 Aug 2016" day="Mon" high="30" low="23" text="Sunny"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="30 Aug 2016" day="Tue" high="31" low="23" text="Partly Cloudy"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="28" date="31 Aug 2016" day="Wed" high="31" low="25" text="Mostly Cloudy"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="47" date="01 Sep 2016" day="Thu" high="28" low="26" text="Scattered Thunderstorms"/>
<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="47" date="02 Sep 2016" day="Fri" high="28" low="25" text="Scattered Thunderstorms"/>
<description>
<![CDATA[<img src="http://l.yimg.com/a/i/us/we/52/4.gif"/> <BR /> <b>Current Conditions:</b> <BR />Thunderstorms <BR /> <BR /> <b>Forecast:</b> <BR /> Wed - Thunderstorms. High: 33Low: 27 <BR /> Thu - Thunderstorms. High: 33Low: 27 <BR /> Fri - Thunderstorms. High: 32Low: 27 <BR /> Sat - Thunderstorms. High: 30Low: 26 <BR /> Sun - Thunderstorms. High: 31Low: 25 <BR /> <BR /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-2161838/">Full Forecast at Yahoo! Weather</a> <BR /> <BR /> (provided by <a href="http://www.weather.com" >The Weather Channel</a>) <BR /> ]]>
</description>
<guid isPermaLink="false"/>
</item>
</channel>
</results>
</query>
<!--  total: 8  -->
<!--
 prod_gq1_1;paas.yql;queryyahooapiscomproductiongq1;eb814965-6634-11e6-bfb9-f0921c12e67c 
-->

Could it be a mistake in my .tmux.conf? Here:

set -g status-bg colour235 
set -g xterm-keys on
set-window-option -g xterm-keys on

set-option -g status on
set-option -g status-interval 2
set-option -g status-justify "centre"
set-option -g status-left-length 60
set-option -g status-right-length 90
set-option -g status-left "#(~/tmuxPowerline001/tmux-powerline/powerline.sh left)"
set-option -g status-right "#(~/tmuxPowerline001/tmux-powerline/powerline.sh right)"
set-window-option -g window-status-current-format "#[fg=colour7, bg=colour237]⮀#[fg=colour7, bg=colour237] #I ⮁ #W #[fg=colour7, bg=colour237]⮀ "
y9c commented 8 years ago

try

echo $TMUX_POWERLINE_SEG_WEATHER_LOCATION
PauloDanielCarneiro commented 8 years ago

Returns an empty line.

On Wed, Aug 24, 2016 at 10:13 AM, yech notifications@github.com wrote:

try

echo $TMUX_POWERLINE_SEG_WEATHER_LOCATION

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/erikw/tmux-powerline/issues/219#issuecomment-242058309, or mute the thread https://github.com/notifications/unsubscribe-auth/AOU2U4hQZInXEpT5OF5E2E2W58msSk_oks5qjENtgaJpZM4IGOWU .

VladimirGavrikov commented 8 years ago

Need change TMUX_POWERLINE_SEGWEATHERLOCATION_DEFAULT="2161838"
to TMUX_POWERLINE_SEGWEATHERLOCATION="2161838"

remove _DEFAULT

PauloDanielCarneiro commented 8 years ago

PERFECT! Thanks!

On Thu, Aug 25, 2016 at 11:31 AM, VladimirGavrikov <notifications@github.com

wrote:

Need change TMUX_POWERLINE_SEG_WEATHER__LOCATIONDEFAULT="2161838"

to TMUX_POWERLINE_SEG_WEATHER_LOCATION="2161838"

remove _DEFAULT

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/erikw/tmux-powerline/issues/219#issuecomment-242408525, or mute the thread https://github.com/notifications/unsubscribe-auth/AOU2UyiZ5nZWdfDhp127ZrRm_BMIUFVXks5qjadWgaJpZM4IGOWU .

romanhaa commented 7 years ago

I'd like to chime in here because also for me there is an issue with the weather. Same as for Paulo

echo $TMUX_POWERLINE_SEG_WEATHER_LOCATION

gives me an empty line. But different from him, I don't have TMUX_POWERLINE_SEG_WEATHER__LOCATION_DEFAULT in my weather.sh file. Strangely enough, if I define it in the terminal with

export TMUX_POWERLINE_SEG_WEATHER_LOCATION="12796587"

I still don't get weather information and shortly later, echoing the parameter again gives an empty line. So it looks to me like it's actively overwritten somewhere, but when I search for 'TMUX_POWERLINE_SEG_WEATHER_LOCATION' in the whole tmux-powerline folder, I only find 3 instances, all of them in weather.sh. I'll post the content of weather.sh here so that maybe someone can help me resolve this?

# Prints the current weather in Celsius, Fahrenheits or lord Kelvins. The forecast is cached and updated with a period of $update_period.

# The update period in seconds.
update_period=600

TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT="yahoo"
TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT="c"
TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT="600"
if shell_is_bsd; then
    TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT="/usr/local/bin/grep"
else
    TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT="grep"
fi

generate_segmentrc() {
    read -d '' rccontents  << EORC
# The data provider to use. Currently only "yahoo" is supported.
export TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER="${TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT}"
# What unit to use. Can be any of {c,f,k}.
export TMUX_POWERLINE_SEG_WEATHER_UNIT="${TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT}"
# How often to update the weather in seconds.
export TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD="${TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT}"
# Name of GNU grep binary if in PATH, or path to it.
export TMUX_POWERLINE_SEG_WEATHER_GREP="${TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT}"

# Your location. Find a code that works for you:
# 1. Go to Yahoo weather http://weather.yahoo.com/
# 2. Find the weather for you location
# 3. Copy the last numbers in that URL. e.g. "http://weather.yahoo.com/united-states/california/newport-beach-12796587/" has the numbers "12796587"
export TMUX_POWERLINE_SEG_WEATHER_LOCATION="12796587"
EORC
    echo "$rccontents"
}

run_segment() {
    __process_settings
    local tmp_file="${TMUX_POWERLINE_DIR_TEMPORARY}/weather_yahoo.txt"
    local weather
    case "$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER" in
        "yahoo") weather=$(__yahoo_weather) ;;
        *)
            echo "Unknown weather provider [${$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER}]";
            return 1
    esac
    if [ -n "$weather" ]; then
        echo "$weather"
    fi
}

__process_settings() {
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER="${TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_UNIT" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_UNIT="${TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD="${TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_GREP" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_GREP="${TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_LOCATION" ]; then
        echo "No weather location specified.";
        exit 8
    fi
}

__yahoo_weather() {
    degree=""
    if [ -f "$tmp_file" ]; then
        if shell_is_osx || shell_is_bsd; then
            last_update=$(stat -f "%m" ${tmp_file})
        elif shell_is_linux; then
            last_update=$(stat -c "%Y" ${tmp_file})
        fi
        time_now=$(date +%s)

        up_to_date=$(echo "(${time_now}-${last_update}) < ${update_period}" | bc)
        if [ "$up_to_date" -eq 1 ]; then
            __read_tmp_file
        fi
    fi

    if [ -z "$degree" ]; then
        weather_data=$(curl --max-time 4 -s "https://query.yahooapis.com/v1/public/yql?format=xml&q=SELECT%20*%20FROM%20weather.forecast%20WHERE%20u=%27${TMUX_POWERLINE_SEG_WEATHER_UNIT}%27%20AND%20woeid%20=%20%27${TMUX_POWERLINE_SEG_WEATHER_LOCATION}%27")
        if [ "$?" -eq "0" ]; then
            error=$(echo "$weather_data" | grep "problem_cause\|DOCTYPE");
            if [ -n "$error" ]; then
                echo "error"
                exit 1
            fi

            # Assume latest grep is in PATH
            gnugrep="${TMUX_POWERLINE_SEG_WEATHER_GREP}"

            # <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
            unit=$(echo "$weather_data" | "$gnugrep" -Zo "<yweather:units [^<>]*/>" | sed 's/.*temperature="\([^"]*\)".*/\1/')
            condition=$(echo "$weather_data" | "$gnugrep" -Zo "<yweather:condition [^<>]*/>")
            # <yweather:condition  text="Clear"  code="31"  temp="66"  date="Mon, 01 Oct 2012 8:00 pm CST" />
            degree=$(echo "$condition" | sed 's/.*temp="\([^"]*\)".*/\1/')
            condition=$(echo "$condition" | sed 's/.*text="\([^"]*\)".*/\1/')
            # Pull the times for sunrise and sunset so we know when to change the day/night indicator
            # <yweather:astronomy sunrise="6:56 am"   sunset="6:21 pm"/>
            if shell_is_osx || shell_is_bsd; then
                date_arg='-j -f "%H:%M %p "'
            else
                date_arg='-d'
            fi
            sunrise=$(date ${date_arg}"$(echo "$weather_data" | "$gnugrep" "yweather:astronomy" | sed 's/^\(.*\)sunset.*/\1/' | sed 's/^.*sunrise="\(.*m\)".*/\1/')" +%H%M)
            sunset=$(date ${date_arg}"$(echo "$weather_data" | "$gnugrep" "yweather:astronomy" | sed 's/^.*sunset="\(.*m\)".*/\1/')" +%H%M)
        elif [ -f "${tmp_file}" ]; then
            __read_tmp_file
        fi
    fi

    if [ -n "$degree" ]; then
        if [ "$TMUX_POWERLINE_SEG_WEATHER_UNIT" == "k" ]; then
            degree=$(echo "${degree} + 273.15" | bc)
        fi
        condition_symbol=$(__get_condition_symbol "$condition" "$sunrise" "$sunset") 
        echo "${condition_symbol} ${degree}°$(echo "$TMUX_POWERLINE_SEG_WEATHER_UNIT" | tr '[:lower:]' '[:upper:]')" | tee "${tmp_file}"
    fi
}

# Get symbol for condition. Available conditions: http://developer.yahoo.com/weather/#codes
__get_condition_symbol() {
    local condition=$(echo "$1" | tr '[:upper:]' '[:lower:]')
    local sunrise="$2"
    local sunset="$3"
    case "$condition" in
        "sunny" | "hot")
            hourmin=$(date +%H%M)
            if [ "$hourmin" -ge "$sunset" -o "$hourmin" -le "$sunrise" ]; then
                #echo "☽"
                echo "☾"
            else
                #echo "☀"
                echo "☼"
            fi
            ;;
        "rain" | "mixed rain and snow" | "mixed rain and sleet" | "freezing drizzle" | "drizzle" | "light drizzle" | "freezing rain" | "showers" | "mixed rain and hail" | "scattered showers" | "isolated thundershowers" | "thundershowers" | "light rain with thunder" | "light rain" | "rain and snow")
            #echo "☂"
            echo "☔"
            ;;
        "snow" | "mixed snow and sleet" | "snow flurries" | "light snow showers" | "blowing snow" | "sleet" | "hail" | "heavy snow" | "scattered snow showers" | "snow showers" | "light snow" | "snow/windy" | "snow grains" | "snow/fog")
            #echo "☃"
            echo "❅"
            ;;
        "cloudy" | "mostly cloudy" | "partly cloudy" | "partly cloudy/windy")
            echo "☁"
            ;;
        "tornado" | "tropical storm" | "hurricane" | "severe thunderstorms" | "thunderstorms" | "isolated thunderstorms" | "scattered thunderstorms")
            #echo "⚡"
            echo "☈"
            ;;
        "dust" | "foggy" | "fog" | "haze" | "smoky" | "blustery" | "mist")
            #echo "♨"
            #echo "﹌"
            echo "〰"
            ;;
        "breezy")
            #echo "🌬"
            echo "🍃"
            ;;
        "windy" | "fair/windy")
            #echo "⚐"
            echo "⚑"
            ;;
        "clear" | "fair" | "cold")
            hourmin=$(date +%H%M)
            if [ "$hourmin" -ge "$sunset" -o "$hourmin" -le "$sunrise" ]; then
                echo "☾"
            else
                echo "〇"
            fi
            ;;
        *)
            echo "?"
            ;;
    esac
}

__read_tmp_file() {
    if [ ! -f "$tmp_file" ]; then
        return
    fi
    cat "${tmp_file}"
    exit
}
erikw commented 7 years ago

@romanhaa: You are suppose to set the weather location configuration variable in ~ /.tmux-powerlinerc. Read the README.md instructions on how to configure tmux-powerline :).

lennartkoopmann commented 7 years ago

Another hint: Make sure that the path to grep in TMUX_POWERLINE_SEG_WEATHER_GREP is correct. I had to change it for grep installed via Homebrew on OSX.

fr1sk commented 7 years ago

@romanhaa try this code, replace everything in weather.sh with this code



# The update period in seconds.
update_period=600

TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT="yahoo"
TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT="c"
TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT="600"
TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT="grep"
TMUX_POWERLINE_SEG_WEATHER_LOCATION="532697"

generate_segmentrc() {
read -d '' rccontents  << EORC
# The data provider to use. Currently only "yahoo" is supported.
export TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER="${TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT}"
# What unit to use. Can be any of {c,f,k}.
export TMUX_POWERLINE_SEG_WEATHER_UNIT="${TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT}"
# How often to update the weather in seconds.
export TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD="${TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT}"
# Name of GNU grep binary if in PATH, or path to it.
export TMUX_POWERLINE_SEG_WEATHER_GREP="${TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT}"

# Your location. Find a code that works for you:
# 1. Go to Yahoo weather http://weather.yahoo.com/
# 2. Find the weather for you location
# 3. Copy the last numbers in that URL. e.g. "http://weather.yahoo.com/united-states/california/newport-beach-12796587/" has the numbers "12796587"
export TMUX_POWERLINE_SEG_WEATHER_LOCATION="${TMUX_POWERLINE_SEG_WEATHER_LOCATION_DEFAULT}"
EORC
echo "$rccontents"
}

run_segment() {
    __process_settings
    local tmp_file="${TMUX_POWERLINE_DIR_TEMPORARY}/weather_yahoo.txt"
    local weather
    case "$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER" in
        "yahoo") weather=$(__yahoo_weather) ;;
        *)
            echo "Unknown weather provider [${$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER}]";
            return 1
    esac
    if [ -n "$weather" ]; then
        echo "$weather"
    fi
}

__process_settings() {
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER="${TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_UNIT" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_UNIT="${TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD="${TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_GREP" ]; then
        export TMUX_POWERLINE_SEG_WEATHER_GREP="${TMUX_POWERLINE_SEG_WEATHER_GREP_DEFAULT}"
    fi
    if [ -z "$TMUX_POWERLINE_SEG_WEATHER_LOCATION" ]; then
        echo "lol";
        exit 8
    fi
}

__yahoo_weather() {
    degree=""
    if [ -f "$tmp_file" ]; then
        if shell_is_osx || shell_is_bsd; then
            last_update=$(stat -f "%m" ${tmp_file})
        elif shell_is_linux; then
            last_update=$(stat -c "%Y" ${tmp_file})
        fi
        time_now=$(date +%s)

        up_to_date=$(echo "(${time_now}-${last_update}) < ${update_period}" | bc)
        if [ "$up_to_date" -eq 1 ]; then
            __read_tmp_file
        fi
    fi

    if [ -z "$degree" ]; then
        weather_data=$(curl --max-time 4 -s "https://query.yahooapis.com/v1/public/yql?format=xml&q=SELECT%20*%20FROM%20weather.forecast%20WHERE%20u=%27${TMUX_POWERLINE_SEG_WEATHER_UNIT}%27%20AND%20woeid%20=%20%27${TMUX_POWERLINE_SEG_WEATHER_LOCATION}%27")
            if [ "$?" -eq "0" ]; then
            error=$(echo "$weather_data" | grep "problem_cause\|DOCTYPE");
            if [ -n "$error" ]; then
                echo "error"
                exit 1
            fi

            # Assume latest grep is in PATH
            gnugrep="${TMUX_POWERLINE_SEG_WEATHER_GREP}"

            # <yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
            unit=$(echo "$weather_data" | "$gnugrep" -Zo "<yweather:units [^<>]*/>" | sed 's/.*temperature="\([^"]*\)".*/\1/')
            condition=$(echo "$weather_data" | "$gnugrep" -Zo "<yweather:condition [^<>]*/>")
            # <yweather:condition  text="Clear"  code="31"  temp="66"  date="Mon, 01 Oct 2012 8:00 pm CST" />
            degree=$(echo "$condition" | sed 's/.*temp="\([^"]*\)".*/\1/')
            condition=$(echo "$condition" | sed 's/.*text="\([^"]*\)".*/\1/')
            # Pull the times for sunrise and sunset so we know when to change the day/night indicator
            # <yweather:astronomy sunrise="6:56 am"   sunset="6:21 pm"/>
            if shell_is_osx || shell_is_bsd; then
                date_arg='-j -f "%H:%M %p "'
            else
                date_arg='-d'
            fi
            sunrise=$(date ${date_arg}"$(echo "$weather_data" | "$gnugrep" "yweather:astronomy" | sed 's/^\(.*\)sunset.*/\1/' | sed 's/^.*sunrise="\(.*m\)".*/\1/')" +%H%M)
            sunset=$(date ${date_arg}"$(echo "$weather_data" | "$gnugrep" "yweather:astronomy" | sed 's/^.*sunset="\(.*m\)".*/\1/')" +%H%M)
        elif [ -f "${tmp_file}" ]; then
            __read_tmp_file
        fi
    fi

    if [ -n "$degree" ]; then
        if [ "$TMUX_POWERLINE_SEG_WEATHER_UNIT" == "k" ]; then
            degree=$(echo "${degree} + 273.15" | bc)
        fi
        condition_symbol=$(__get_condition_symbol "$condition" "$sunrise" "$sunset") 
        echo "${condition_symbol} ${degree}°$(echo "$TMUX_POWERLINE_SEG_WEATHER_UNIT" | tr '[:lower:]' '[:upper:]')" | tee "${tmp_file}"
    fi
}

# Get symbol for condition. Available conditions: http://developer.yahoo.com/weather/#codes
__get_condition_symbol() {
    local condition=$(echo "$1" | tr '[:upper:]' '[:lower:]')
    local sunrise="$2"
    local sunset="$3"
    case "$condition" in
        "sunny" | "hot")
            hourmin=$(date +%H%M)
            if [ "$hourmin" -ge "$sunset" -o "$hourmin" -le "$sunrise" ]; then
                #echo "☽"
                echo "☾"
            else
                #echo "☀"
                echo "☼"
            fi
            ;;
        "rain" | "mixed rain and snow" | "mixed rain and sleet" | "freezing drizzle" | "drizzle" | "light drizzle" | "freezing rain" | "showers" | "mixed rain and hail" | "scattered showers" | "isolated thundershowers" | "thundershowers" | "light rain with thunder" | "light rain" | "rain and snow")
            #echo "☂"
            echo "☔"
            ;;
        "snow" | "mixed snow and sleet" | "snow flurries" | "light snow showers" | "blowing snow" | "sleet" | "hail" | "heavy snow" | "scattered snow showers" | "snow showers" | "light snow" | "snow/windy" | "snow grains" | "snow/fog")
            #echo "☃"
            echo "❅"
            ;;
        "cloudy" | "mostly cloudy" | "partly cloudy" | "partly cloudy/windy")
            echo "☁"
            ;;
        "tornado" | "tropical storm" | "hurricane" | "severe thunderstorms" | "thunderstorms" | "isolated thunderstorms" | "scattered thunderstorms")
            #echo "⚡"
            echo "☈"
            ;;
        "dust" | "foggy" | "fog" | "haze" | "smoky" | "blustery" | "mist")
            #echo "♨"
            #echo "﹌"
            echo "〰"
            ;;
        "breezy")
            #echo "🌬"
            echo "🍃"
            ;;
        "windy" | "fair/windy")
            #echo "⚐"
            echo "⚑"
            ;;
        "clear" | "fair" | "cold")
            hourmin=$(date +%H%M)
            if [ "$hourmin" -ge "$sunset" -o "$hourmin" -le "$sunrise" ]; then
                echo "☾"
            else
                echo "〇"
            fi
            ;;
        *)
            echo "?"
            ;;
    esac
}

__read_tmp_file() {
    if [ ! -f "$tmp_file" ]; then
        return
    fi
    cat "${tmp_file}"
    exit
}```
newdee commented 7 years ago

I met the same problem, but I solved it just by moving the order of the export sentence as follows: move the

"export TMUX_POWERLINE_SEG_WEATHER_LOCATION="12796587"(your number) "

to the location after the sentence:

TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT="600"

so, the result should be:

TMUX_POWERLINE_SEG_WEATHER_DATA_PROVIDER_DEFAULT="yahoo"                                                                                                                                      
TMUX_POWERLINE_SEG_WEATHER_UNIT_DEFAULT="c"                                                                                                                                                   
TMUX_POWERLINE_SEG_WEATHER_UPDATE_PERIOD_DEFAULT="600"                                                                                                                                        
export TMUX_POWERLINE_SEG_WEATHER_LOCATION="12796587"

and it works.

etheleon commented 5 years ago

the API has stopped working

foloinfo commented 5 years ago

It seems like the endpoint is no longer available.

Important EOL Notice: As of Thursday, Jan. 3, 2019, the weather.yahooapis.com and query.yahooapis.com for Yahoo Weather API will be retired. To continue using our free Yahoo Weather APIs, use https://weather-ydn-yql.media.yahoo.com/forecastrss. Follow below instructions to get credentials and onboard to this free Yahoo Weather API service.

Weather - Yahoo Developer Network

taishibrown3 commented 5 years ago

Can't move the weather display now?

erikw commented 2 years ago

A submission for another weather provider API would be welcome!