Skip to content

Instantly share code, notes, and snippets.

@jnyilas
Forked from meskarune/cal.lua
Last active February 19, 2023 15:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnyilas/2ae3fd9f2d7eb402a3400e065f5524b2 to your computer and use it in GitHub Desktop.
Save jnyilas/2ae3fd9f2d7eb402a3400e065f5524b2 to your computer and use it in GitHub Desktop.
conky calendar and weather
#!/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"
)
-- 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}
]]
#!/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
--- EST -5, EDT -4
tz = "-4"
-- Change to the lattitude and longitude you want to use
lat = "XX.43"
long = "-XX.48"
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"] = "🌘"
}
cachefile = os.getenv("HOME")..("/.config/conky/moonphase.json")
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(cachefile) then
cache = io.open(cachefile,"r")
data = json.decode(cache:read())
cache:close()
timepassed = os.difftime(curtime, data.timestamp)
else
timepassed = 6000
end
makecache = function (s)
cache = io.open(cachefile, "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
transit = response.moonTransit
rise = response.moonRise
set = response.moonSet
conky_text = [[${font Symbola:size=20}${alignc}${color2}%s${font} %s
${color}Rise: ${color9}%5s
${color}Transit: ${color9}%5s
${color}Set: ${color9}%5s]]
io.write((conky_text):format(moon[phase], phase, rise, transit, set))
#!/usr/bin/env lua
-- load the http socket module
http = require("socket.http")
-- load the json module
json = require("json")
api_url = "http://api.openweathermap.org/data/2.5/weather?"
-- http://openweathermap.org/help/city_list.txt , http://openweathermap.org/find
---cityid = "6542124"
-- metric or imperial
cf = "imperial"
-- get an open weather map api key: http://openweathermap.org/appid
apikey = "XXX"
-- measure is °C if metric and °F if imperial
measure = "°" .. (cf == "metric" and "C" or "F")
wind_units = (cf == "metric" and "kph" 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")
cachefile = os.getenv("HOME")..("/.config/conky/weather.json")
---print (cachefile)
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(cachefile) then
cache = io.open(cachefile, "r")
data = json.decode(cache:read())
cache:close()
timepassed = os.difftime(currenttime, data.timestamp)
else
timepassed = 6000
end
makecache = function(s)
cache = io.open(cachefile, "w+")
s.timestamp = currenttime
save = json.encode(s)
cache:write(save)
cache:close()
end
if timepassed < 3600 then
response = data
else
weather = http.request(("%sid=%s&units=%s&APPID=%s"):format(api_url, cityid, 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.floor(d / 22.5 + 0.5)
directions = {
[00] = "N",
[01] = "NNE",
[02] = "NE",
[03] = "ENE",
[04] = "E",
[05] = "ESE",
[06] = "SE",
[07] = "SSE",
[08] = "S",
[09] = "SSW",
[10] = "SW",
[11] = "WSW",
[12] = "W",
[13] = "WNW",
[14] = "NW",
[15] = "NNW"
}
return directions[val % 16]
end
temp = response.main.temp
min = response.main.temp_min
max = response.main.temp_max
conditions = response.weather[1].description
icon2 = response.weather[1].id
icon = response.weather[1].icon:sub(1, 2)
humidity = response.main.humidity
wind = response.wind.speed
deg = degrees_to_direction(response.wind.deg)
sunrise = os.date("%H:%M %p", response.sys.sunrise)
sunset = os.date("%H:%M %p", response.sys.sunset)
conky_text =
[[
${font Symbola:size=36}${alignc}${color2}%s ${voffset -10}${font :size=10}${color9}%s${font}${voffset 0}%s
${alignc}${color2}${voffset 28}%s
${alignc}${color}High: ${color9}%s%s ${color}Low: ${color9}%s%s${color}
${alignc}Humidity: ${color9}%s%%${color}
${alignc}Wind: ${color9}%smph${color} @ ${color9}%s${color}
${alignc}${font Symbola:size=20}─⯊─${font}
${alignc}${color9}%s${color} | ${color9}%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),
deg,
sunrise,
sunset
)
)
@jnyilas
Copy link
Author

jnyilas commented May 16, 2020

Added a consistent location to all the cache files: $HOME/.config/conky.

Changed formatting to suite my conky configuration.

Added extra reporting to moonphase output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment