Last active
June 30, 2020 18:07
-
-
Save alinradut/7be5b1bd4375a868125bea9254d47074 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# coding: utf-8 | |
# <bitbar.title>GeoIPWeather</bitbar.title> | |
# <bitbar.version>v0.1.1</bitbar.version> | |
# <bitbar.author>Taylor Zane</bitbar.author> | |
# <bitbar.author.github>taylorzane</bitbar.author.github> | |
# <bitbar.desc>Your weather in the menu bar 🌤</bitbar.desc> | |
# <bitbar.image>http://i.imgur.com/vrT6vfb.png</bitbar.image> | |
# <bitbar.dependencies>ruby</bitbar.dependencies> | |
# <bitbar.abouturl>https://github.com/taylorzane</bitbar.abouturl> | |
### USER VARIABLES | |
UNITS = 'C' # This can be: (F)ahrenheit, (C)elsius, (K)elvin | |
API_KEY = '8b4824b451d5db1612156837df880f55' # you can also get your own at http://openweathermap.org/ | |
require 'json' | |
require 'net/http' | |
def no_data(message = nil) | |
if message | |
puts message | |
else | |
puts 'Cannot get weather.' | |
end | |
exit | |
end | |
def getWeatherEmoji(weatherID) | |
# Openweathermap Weather codes and corressponding emojis | |
thunderstorm = "\u{1F4A8}" # Code: 200's, 900, 901, 902, 905 | |
drizzle = "\u{1F4A7}" # Code: 300's | |
rain = "\u{02614}" # Code: 500's | |
snowflake = "\u{02744}" # Code: 600's snowflake | |
snowman = "\u{026C4}" # Code: 600's snowman, 903, 906 | |
atmosphere = "\u{1F301}" # Code: 700's foogy | |
clearSky = "\u{02600}" # Code: 800 clear sky | |
fewClouds = "\u{026C5}" # Code: 801 sun behind clouds | |
clouds = "\u{02601}" # Code: 802-803-804 clouds general | |
hot = "\u{1F525}" # Code: 904 | |
defaultEmoji = "\u{1F300}" # default emojis | |
if (weatherID = weatherID.to_s) | |
if weatherID.chars.first == '2' or weatherID == '900' or weatherID == '901' or weatherID == '902' or weatherID == '905' | |
return thunderstorm | |
elsif weatherID.chars.first == '3' | |
return drizzle | |
elsif weatherID.chars.first == '5' | |
return rain | |
elsif weatherID.chars.first == '6' or weatherID == '903' or weatherID == '906' | |
return snowflake + ' ' + snowman | |
elsif weatherID.chars.first == '7' | |
return atmosphere | |
elsif weatherID == '800' | |
return clearSky | |
elsif weatherID == '801' | |
return fewClouds | |
elsif weatherID == '802' or weatherID == '803' or weatherID == '803' | |
return clouds | |
elsif weatherID == '904' | |
return hot | |
else | |
return defaultEmoji | |
end | |
else | |
return defaultEmoji | |
end | |
end | |
def location | |
location_uri = URI('http://ipinfo.io/json') | |
begin | |
location_data = Net::HTTP.get location_uri | |
rescue | |
no_data | |
end | |
location_json = JSON.parse location_data | |
lat = nil | |
lon = nil | |
if location_json['loc'] | |
loc = location_json['loc'].split(/,/) | |
lat = loc.first() | |
lon = loc.last() | |
else | |
no_data | |
end | |
[lat, lon] | |
end | |
def weather(lat, lon) | |
temperature_unit = | |
case UNITS.upcase | |
when 'F' | |
'&units=imperial' | |
when 'C' | |
'&units=metric' | |
else | |
'' | |
end | |
temperature_symbol = | |
case UNITS.upcase | |
when 'F' | |
'℉' | |
when 'C' | |
'℃' | |
else | |
'K' | |
end | |
weather_uri = | |
URI("http://api.openweathermap.org/data/2.5/weather?lat=#{lat}&lon=#{lon}&appid=8b4824b451d5db1612156837df880f55&units=metric") | |
weather_data = Net::HTTP.get(weather_uri) | |
no_data unless weather_data | |
weather_json = JSON.parse weather_data | |
no_data weather_json['message'] if weather_json['cod'] == '404' | |
temperature = weather_json['main']['temp'].round | |
city = weather_json['name'] | |
country = weather_json['sys']['country'] | |
city_id = weather_json['id'] | |
puts getWeatherEmoji(weather_json['weather'][0]['id']) + " #{temperature}#{temperature_symbol}" | |
puts "---" | |
puts "#{city}, #{country} | href=https://openweathermap.org/city/#{city_id}" | |
puts weather_json['weather'][0]['description'].capitalize() | |
puts "---" | |
puts "Temperature: " + "%d" % weather_json['main']['temp'].round + " #{temperature_symbol}" | |
puts "Feels like: " + "%d" % weather_json['main']['feels_like'].round + " #{temperature_symbol}" | |
puts "Min temp: " + "%d" % weather_json['main']['temp_min'].round + " #{temperature_symbol}" | |
puts "Max temp: " + "%d" % weather_json['main']['temp_max'].round + " #{temperature_symbol}" | |
puts "Humidity: " + "%d" % weather_json['main']['humidity'].round + " %" | |
puts "Pressure: " + "%d" % weather_json['main']['pressure'].round + " hpa" | |
end | |
weather(*location) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment