Skip to content

Instantly share code, notes, and snippets.

@trappedinspacetime
Forked from meskarune/cal.lua
Created April 3, 2022 14:17
Show Gist options
  • Save trappedinspacetime/3de87edb85093dc153012fd0772a40cd to your computer and use it in GitHub Desktop.
Save trappedinspacetime/3de87edb85093dc153012fd0772a40cd 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
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))
#!/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)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment