Last active
June 11, 2024 12:31
-
-
Save meskarune/e415748a104f0479f54dd642d66011e8 to your computer and use it in GitHub Desktop.
conky calendar and weather
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 lua | |
conky_color = "${color1}%2d${color}" | |
t = os.date('*t', os.time()) | |
year, month, currentday = t.year, t.month, t.day | |
daystart = os.date("*t",os.time{year=year,month=month,day=01}).wday | |
month_name = os.date("%B") | |
days_in_month = { | |
31, 28, 31, 30, 31, 30, | |
31, 31, 30, 31, 30, 31 | |
} | |
-- check for leap year | |
-- Any year that is evenly divisible by 4 is a leap year | |
-- Any year that is evenly divisible by 100 is a leap year if | |
-- it is also evenly divisible by 400. | |
LeapYear = function (year) | |
return year % 4 == 0 and (year % 100 ~= 0 or year % 400 == 0) | |
end | |
if LeapYear(year) then | |
days_in_month[2] = 29 | |
end | |
title_start = (20 - (string.len(month_name) + 5)) / 2 | |
title = string.rep(" ", math.floor(title_start+0.5)) .. -- add padding to center the title | |
(" %s %s\n Su Mo Tu We Th Fr Sa\n"):format(month_name, year) | |
io.write(title) | |
function seq(a,b) | |
if a > b then | |
return | |
else | |
return a, seq(a+1,b) | |
end | |
end | |
days = days_in_month[month] | |
io.write( | |
string.format( | |
string.rep(" ", daystart-1) .. | |
string.rep(" %2d", days), seq(1,days) | |
):gsub(string.rep(".",21),"%0\n") | |
:gsub(("%2d"):format(currentday), | |
(conky_color):format(currentday), | |
1 | |
) .. "\n" | |
) |
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
-- vim: ts=4 sw=4 noet ai cindent syntax=lua | |
conky.config = { | |
alignment = 'top_right', | |
background = false, | |
border_width = 0, | |
cpu_avg_samples = 2, | |
default_color = 'cccccc', | |
color1 = '86b5ea', | |
default_outline_color = 'cccccc', | |
default_shade_color = '7a999c', | |
double_buffer = true, | |
draw_borders = false, | |
draw_graph_borders = false, | |
draw_outline = false, | |
draw_shades = false, | |
use_xft = true, | |
font = 'Fira Sans:normal:size=14', | |
gap_x = 10, | |
gap_y = 41, | |
minimum_height = 5, | |
minimum_width = 231, | |
maximum_width = 231, | |
net_avg_samples = 2, | |
no_buffers = true, | |
out_to_console = false, | |
out_to_stderr = false, | |
extra_newline = false, | |
own_window = true, | |
own_window_class = 'Conky', | |
own_window_transparent = true, | |
own_window_type = 'desktop', | |
stippled_borders = 0, | |
update_interval = 1.0, | |
uppercase = false, | |
use_spacer = 'none', | |
show_graph_scale = false, | |
show_graph_range = false, | |
} | |
conky.text = [[ | |
${alignc}${font :size=28} ${time %k:%M %p}${font} | |
${font Fira Mono:size=14}${execpi 3600 ~/.config/conky/cal.lua}${font} | |
${alignc}${execpi 1800 ~/.config/conky/weather.lua} | |
]] |
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/lua | |
https = require("ssl.https") | |
json = require("json") | |
-- To use edit variables below for your timezone and location then add the next line to your conky config, uncommented | |
-- ${alignc}${execpi 3600 ~/.config/conky/moonphase.lua} | |
-- Change to your timezone offset | |
tz = "-0" | |
-- Change to the lattitude and longitude you want to use | |
lat = "51.5" | |
long = "-0.116667" | |
curdate = os.date("!%Y%m%d") | |
curtime = os.date("!%Y%m%d%H%M%S") | |
api_url = ("https://api.solunar.org/solunar/%s,%s,%s,%s"):format(lat,long,curdate,tz) | |
moon = { | |
["New Moon"] = "🌑", | |
["Waxing Crescent"] = "🌒", | |
["First Quarter"] = "🌓", | |
["Waxing Gibbous"] = "🌔", | |
["Full moon"] = "🌕", | |
["Waning Gibbous"] = "🌖", | |
["Third Quarter"] = "🌗", | |
["Waning Crescent"] = "🌘" | |
} | |
file_exists = function (name) | |
f=io.open(name,"r") | |
if f~=nil then | |
f:close() | |
return true | |
else | |
return false | |
end | |
end | |
if file_exists("moonphase.json") then | |
cache = io.open("moonphase.json","r") | |
data = json.decode(cache:read()) | |
cache:close() | |
timepassed = os.difftime(curtime, data.timestamp) | |
else | |
timepassed = 6000 | |
end | |
makecache = function (s) | |
cache = io.open("moonphase.json", "w+") | |
s.timestamp = curtime | |
save = json.encode(s) | |
cache:write(save) | |
cache:close() | |
end | |
if timepassed < 3600 then | |
response = data | |
else | |
mooninfo = https.request(api_url) | |
if mooninfo then | |
response = json.decode(mooninfo) | |
makecache(response) | |
else | |
response = data | |
end | |
end | |
phase = response.moonPhase | |
conky_text = [[${font Symbola:size=20}%s${font} %s]] | |
io.write((conky_text):format(moon[phase], phase)) |
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 lua | |
-- load the http socket module | |
http = require("socket.http") | |
-- load the json module | |
json = require("json") | |
api_url = "https://api.openweathermap.org/data/2.5/onecall?" | |
-- Your lattitude and longitude | |
LAT = 51.509865 | |
LON = -0.118092 | |
-- metric or imperial | |
cf = "metric" | |
-- get an open weather map api key: http://openweathermap.org/appid | |
apikey = "api-key" | |
-- measure is °C if metric and °F if imperial | |
measure = '°' .. (cf == 'metric' and 'C' or 'F') | |
wind_units = (cf == 'metric' and 'km/h' or 'mph') | |
-- Unicode weather symbols to use | |
icons = { | |
["01"] = "☀", | |
["02"] = "🌤", | |
["03"] = "🌥", | |
["04"] = "☁", | |
["09"] = "🌧", | |
["10"] = "🌦", | |
["11"] = "🌩", | |
["13"] = "🌨", | |
["50"] = "🌫", | |
} | |
currenttime = os.date("!%Y%m%d%H%M%S") | |
file_exists = function (name) | |
f=io.open(name,"r") | |
if f~=nil then | |
f:close() | |
return true | |
else | |
return false | |
end | |
end | |
if file_exists("weather.json") then | |
cache = io.open("weather.json","r") | |
data = json.decode(cache:read()) | |
cache:close() | |
timepassed = os.difftime(currenttime, data.timestamp) | |
else | |
timepassed = 6000 | |
end | |
makecache = function (s) | |
cache = io.open("weather.json", "w+") | |
s.timestamp = currenttime | |
save = json.encode(s) | |
cache:write(save) | |
cache:close() | |
end | |
if timepassed < 3600 then | |
response = data | |
else | |
weather = http.request(("%slat=%s&lon=%s&exclude=minutely,hourly&units=%s&APPID=%s"):format(api_url, LAT, LON, cf, apikey)) | |
if weather then | |
response = json.decode(weather) | |
makecache(response) | |
else | |
response = data | |
end | |
end | |
math.round = function (n) | |
return math.floor(n + 0.5) | |
end | |
degrees_to_direction = function (d) | |
val = math.round(d/22.5) | |
directions = { [0] = "N", [1] = "NNE", [2] = "NE", [3] = "ENE", | |
[4] = "E", [5] = "ESE", [6] = "SE", [7] = "SSE", | |
[8] = "S", [9] = "SSW", [10] = "SW", [11] = "WSW", | |
[12] = "W", [13] = "WNW", [14] = "NW", [15] = "NNW" | |
} | |
return directions[val % 16] | |
end | |
temp = response.current.temp | |
min = response.daily[1].temp.min | |
max = response.daily[1].temp.max | |
conditions = response.current.weather[1].description | |
icon2 = response.current.weather[1].id | |
icon = response.current.weather[1].icon:sub(1, 2) | |
humidity = response.current.humidity | |
wind = response.current.wind_speed | |
deg = degrees_to_direction(response.current.wind_deg) | |
sunrise = os.date("%H:%M", response.current.sunrise) | |
sunset = os.date("%H:%M", response.current.sunset) | |
forcast_icon = response.daily[2].weather[1].icon:sub(1, 2) | |
forcast_temp = response.daily[2].temp.day | |
forcast_conditions = response.daily[2].weather[1].main | |
conky_text = [[ | |
${font Symbola:size=48}%s ${voffset -10}${font :size=20}${color1}%s${font}${voffset -5}%s${color} | |
${alignc}${voffset 28} %s | |
${alignc}High: ${color1}%s%s ${color}Low: ${color1}%s%s${color} | |
${alignc}Humidity: ${color1}%s%%${color} | |
${alignc}Wind: ${color1}%s%s %s${color} | |
${alignc}Tomorrow: | |
${alignc}${color1}${font Symbola:size=20}%s${font} %s%s %s${color} | |
${alignc}${font Symbola:size=20}─☀─${font} | |
${alignc}${color1}%s${color} | ${color1}%s${color} | |
]] | |
io.write((conky_text):format(icons[icon], | |
math.round(temp), | |
measure, | |
conditions, | |
math.round(max), | |
measure, | |
math.round(min), | |
measure, | |
humidity, | |
math.round(wind), | |
wind_units, | |
deg, | |
icons[forcast_icon], | |
math.round(forcast_temp), | |
measure, | |
forcast_conditions, | |
sunrise, | |
sunset) | |
) |
Hello.
I had a hard time at first to show something, but after a boot it worked.
My suggestion is to create a step-by-step for beginning users. It was not so intuitive for me to have to create an account and a API KEY at api.openweathermap.org and replace it in the wheater.lua
file, as well as inform my lat/long values. In any case, this solution has enormous potential. Congratulations!
In my case, for Ubuntu 20.04, as I never developed on the moon (I thought it was pretty cool) I had to run the commands:
sudo apt install lua5.3
sudo apt install lua-socket
sudo apt-install lua-json
chmod 755 ~/.config /conky /*.lua
This is the best conky weather script though it's not working on my end because it's unmaintained
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bumping ~5 months old post, cause these days I spent lot of useless, but very very interesting hours - top fun time, to fit these scripts to my taste and positioning of calendar was one of challenges (don't take me wrong: I like to have candy desktop and this kind of modding is very interesting for me - I enjoy it, but I have to say it is useless cause it is :) 👯 👯 :) ).
1st thing: Thanks to @meskarune for scripts and concept!!!
2nd thing: @jnyilas: unfortunately, this couldn't be accomplished with Conky alignment, cause putting any
${offset xx}
,${alignc}
,${alignr}
, etc. in front or middle of${font Fira Mono:size=14}${execpi 3600 ~/.config/conky/cal.lua}${font}
will move/align only first line, i.e. December 2020, like this:I am not developer. Usually I am able to edit finished code and fit it to my needs, but this was my first time with Lua and hadn't success to figure out could this be aligned inside cal.lua file. So i used workaround/cheat/hack whatever and piped conky exec line for cal.lua to sed, adding spaces in front to every line:
${font Ubuntu Mono:size=14:style=bold}${execpi 3600 ~/.config/conky/cal.lua | sed 's/^/ /'}${font}
(there are lot of SPACEs between two slashes above / / , but it seems that github code tag is displaying all of them as one SPACE)
Anyway, this is my final desktop and I am happy:
I would paste my edit of weather.lua here, cause I made 7 days forecast and will be glad to give my contribution here - @meskarune: please be free to implement it in your script if you want. @ALL: Please note that you will need either to revert back icons to unicode icons in weather.lua or you will need icon font I made for this, where I used someone else icons (or you will need to make that font). Reason why I'm not providing everything right now is because I'm lazy :) . I used lot of someone else work for this and if I want to publish my work, I need to credit all these people properly. It will require some more work and hours and I cannot know will this be interesting for even one person at all. I can promise, if someone wish all resources, I will immediately publish this somewhere with all proper credits and share link here - please just let me know (I subscribed to this topic :) ).
At the end, once again, I am not developer and I believe that this probably can be done better and more elegant, but it doing the job for me and I am ok with that.
weather.lua: