about summary refs log tree commit diff
diff options
context:
space:
mode:
authorManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2019-08-18 17:27:48 +0200
committerManuel Palenzuela <manuelpalenzuelamerino@gmail.com>2019-08-18 17:27:48 +0200
commita68a654ab7e24a4c7c5346f4f205feb3cc272ab7 (patch)
tree8ba308ffd7120f808b38e04aa3172267c761df24
parentChanged order of wifi icon in dwm-status (diff)
downloaddwm-a68a654ab7e24a4c7c5346f4f205feb3cc272ab7.tar.gz
dwm-a68a654ab7e24a4c7c5346f4f205feb3cc272ab7.tar.bz2
dwm-a68a654ab7e24a4c7c5346f4f205feb3cc272ab7.zip
Refracted weather info in status bar
-rwxr-xr-xscripts/dwm-status51
-rw-r--r--scripts/weather.py46
2 files changed, 44 insertions, 53 deletions
diff --git a/scripts/dwm-status b/scripts/dwm-status
index c16d1d5..c416f86 100755
--- a/scripts/dwm-status
+++ b/scripts/dwm-status
@@ -20,6 +20,43 @@ checkinternet() {
 	fi
 }
 
+weather(){
+	if [ "$internet" = true ]; then
+		LOCATION=$(geolocate)
+		LANG="en"
+		UNITS="Metric"
+		API_KEY="756edce7e9d4c385ef9499a53492678c"
+
+		LOCATION_FORMATTED_2=$(echo $LOCATION | cut -d ':' -f2)
+		LOCATION_FORMATTED_1=$(echo $LOCATION | cut -d ':' -f1)
+
+		OUTPUT=$(curl -s "http://api.openweathermap.org/data/2.5/weather?lat=$LOCATION_FORMATTED_1&lon=$LOCATION_FORMATTED_2&lang=$LANG&appid=$API_KEY&units=$UNITS")
+		STATUS=$(echo $OUTPUT | jq '.weather' | tr '[' ' ' | tr ']' ' ' | jq '.main' | sed 's/"//g')
+		TEMP=$(echo $OUTPUT | jq '.main' | jq '.temp' | xargs printf "%.*f\n" 0)
+
+		case $STATUS in
+			"Clear" )
+				echo "";;
+			"Cloud" )	
+				echo "摒";;
+			"Rain" )
+				echo "歹";;
+			"Thunder" )
+				echo "朗";;
+			"Snow" )
+				echo "流";;
+			"Mist" )
+				echo "敖";;
+			"*" )
+				echo "摒";;
+		esac
+
+		echo "$STATUS, "
+		echo "$TEMP°C"
+		echo "$delim"
+	fi
+}
+
 setaurupdates(){
 	if [ "$internet" = true ]; then
 		if (( $counter % 30 == 0 )); then
@@ -79,10 +116,12 @@ network() {
 
 		echo $delim
 	fi
-
-	if [ "$(cat /sys/class/net/eth?/carrier)" == "1" ]; then
-		echo "" # 
-		echo "$delim"
+	
+	if [ -d /sys/class/net/eth? ]; then
+		if [ "$(cat /sys/class/net/eth?/carrier)" == "1" ]; then
+			echo "" # 
+			echo "$delim"
+		fi
 	fi
 
 	if [ "$internet" = true ]; then
@@ -176,9 +215,7 @@ status() { \
 
 	tor
 
-	# Directs to the path of the weather script.
-	echo $(python $( cd "$(dirname "$0")" ; pwd -P )/weather.py)
-	#	echo "$delim" (done in the weather script)
+	weather
 
 	updates
 
diff --git a/scripts/weather.py b/scripts/weather.py
deleted file mode 100644
index f6be64a..0000000
--- a/scripts/weather.py
+++ /dev/null
@@ -1,46 +0,0 @@
-#!/bin/python
-
-import requests
-import os
-
-delim="|"
-
-# Check for an internet connection
-url='http://www.google.com/'
-timeout=5
-try:
-    _ = requests.get(url, timeout=timeout)
-    internet = True
-except requests.ConnectionError:
-    internet = False
-
-#COORDINATES = open("/usr/share/geolocate/.location", 'r').read()
-#LOCATION = "lat=" + COORDINATES.replace(":","&lon=")
-
-API_KEY = "756edce7e9d4c385ef9499a53492678c"
-UNITS = "Metric"
-UNIT_KEY = "C"
-#UNIT_KEY = "F"
-LANG = "en"
-#LANG = "nl"
-#LANG = "hu"
-if internet == True:
-    COORDINATES = os.popen("geolocate").read()
-    LOCATION = "lat=" + COORDINATES.replace(":","&lon=")
-
-    API="http://api.openweathermap.org/data/2.5/weather?{}&lang={}&appid={}&units={}".format(LOCATION.strip(), LANG,  API_KEY, UNITS)
-    REQ = requests.get(API)
-    #REQ = requests.get("http://api.openweathermap.org/data/2.5/weather?lat=50.84660&lon=4.35280&lang=en&appid=756edce7e9d4c385ef9499a53492678c&units=Metric")
-    try:
-    # HTTP CODE = OK
-        if REQ.status_code == 200:
-            CURRENT = REQ.json()["weather"][0]["description"].capitalize()
-            TEMP = int(float(REQ.json()["main"]["temp"]))
-            print(" {}, {} °{}".format(CURRENT, TEMP, UNIT_KEY))
-            print(delim)
-        else:
-            print("Error: BAD HTTP STATUS CODE " + str(REQ.status_code))
-    except (ValueError, IOError):
-            print("Error: Unable print the data")
-else:
-    print("")