2024-03-02 14:50:16 +00:00
|
|
|
|
require "carddav"
|
2024-03-03 14:47:25 +00:00
|
|
|
|
require "icalendar"
|
2024-03-02 14:50:16 +00:00
|
|
|
|
require "thor"
|
|
|
|
|
require "yaml"
|
|
|
|
|
|
|
|
|
|
class Birthdaze < Thor
|
|
|
|
|
desc "generate", "Generate calendars"
|
|
|
|
|
def generate
|
2024-03-03 14:52:12 +00:00
|
|
|
|
puts "Writing ical file to #{config['ical_output']}"
|
|
|
|
|
File.open(config["ical_output"], 'w') { |file| file.write(calendar.to_ical) }
|
2024-03-03 14:47:25 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
desc "list", "List birthdays"
|
|
|
|
|
def list
|
2024-03-03 20:19:33 +00:00
|
|
|
|
bdays = birthdays.sort do |a, b|
|
|
|
|
|
a[:month] == b[:month] ? a[:day] <=> b[:day] : a[:month] <=> b[:month]
|
2024-03-03 14:47:25 +00:00
|
|
|
|
end
|
2024-03-03 20:19:33 +00:00
|
|
|
|
bdays.each { |d| puts "🎂 #{d[:day]}/#{d[:month]} - #{d[:name]}" }
|
2024-03-02 14:50:16 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2024-03-03 10:33:24 +00:00
|
|
|
|
def config(config_file = "#{ENV["HOME"]}/.config/birthdaze.yaml")
|
2024-03-02 14:50:16 +00:00
|
|
|
|
unless File.file?(config_file)
|
|
|
|
|
puts "Please add a configuration file"
|
|
|
|
|
return
|
|
|
|
|
end
|
2024-03-03 10:33:24 +00:00
|
|
|
|
|
2024-03-02 14:50:16 +00:00
|
|
|
|
@config ||= YAML.load_file(config_file)
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-03 10:33:24 +00:00
|
|
|
|
def client(url: config["url"], username: config["username"], password: config["password"])
|
|
|
|
|
@client ||= Carddav::Client.new(url, username, password)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def birthdays
|
2024-03-03 14:47:25 +00:00
|
|
|
|
return @birthdays if defined? @birthdays
|
|
|
|
|
|
|
|
|
|
@birthdays = []
|
2024-03-03 10:33:24 +00:00
|
|
|
|
client.cards.each do |card|
|
|
|
|
|
card = card.parsed.to_s
|
|
|
|
|
birthday = birthday_regex.match(card)[1] if birthday_regex.match?(card)
|
|
|
|
|
name = name_regex.match(card)[1] if name_regex.match?(card)
|
2024-03-03 20:19:33 +00:00
|
|
|
|
if name && birthday
|
|
|
|
|
@birthdays << {
|
|
|
|
|
name: name,
|
|
|
|
|
month: month_of(birthday),
|
|
|
|
|
day: day_of(birthday),
|
|
|
|
|
birth_year: birth_year_of(birthday)
|
|
|
|
|
}
|
|
|
|
|
end
|
2024-03-03 14:47:25 +00:00
|
|
|
|
end
|
|
|
|
|
@birthdays
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def calendar
|
|
|
|
|
return @calendar if defined? @calendar
|
|
|
|
|
|
|
|
|
|
@calendar = Icalendar::Calendar.new
|
2024-03-03 20:19:33 +00:00
|
|
|
|
birthdays.each do |birthday|
|
2024-03-03 14:47:25 +00:00
|
|
|
|
@calendar.event do |event|
|
2024-03-03 20:38:27 +00:00
|
|
|
|
event.dtstart = Icalendar::Values::Date.new(start_date(birthday))
|
|
|
|
|
event.dtend = Icalendar::Values::Date.new(end_date(birthday))
|
2024-03-03 20:19:33 +00:00
|
|
|
|
event.summary = summary(birthday)
|
2024-03-03 20:38:27 +00:00
|
|
|
|
event.description = description(birthday)
|
|
|
|
|
event.rrule = "FREQ=YEARLY;"
|
2024-03-03 14:47:25 +00:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
@calendar.publish
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Takes a birthday string, with or without a year, and returns a start date
|
2024-03-03 20:38:27 +00:00
|
|
|
|
def start_date(birthday)
|
|
|
|
|
year = birthday[:birth_year] || Date.today.year
|
2024-03-03 20:19:33 +00:00
|
|
|
|
"#{year}#{birthday[:month]}#{birthday[:day]}"
|
2024-03-03 14:47:25 +00:00
|
|
|
|
end
|
|
|
|
|
|
2024-03-03 20:38:27 +00:00
|
|
|
|
def end_date(birthday)
|
|
|
|
|
date = Date.parse(start_date(birthday)).next_day
|
2024-03-03 20:19:33 +00:00
|
|
|
|
date.strftime("%Y%m%d")
|
2024-03-03 14:47:25 +00:00
|
|
|
|
end
|
|
|
|
|
|
2024-03-03 20:19:33 +00:00
|
|
|
|
def summary(birthday)
|
2024-03-03 20:38:27 +00:00
|
|
|
|
"🎂 #{birthday[:name]}’s birthday"
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def description(birthday)
|
|
|
|
|
return "" unless birthday[:birth_year]
|
|
|
|
|
|
|
|
|
|
"#{birthday[:name]} was born in #{birthday[:birth_year]}"
|
2024-03-03 14:47:25 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_reminders
|
2024-03-03 10:33:24 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def birthday_regex
|
|
|
|
|
# We need the dash for dates which don’t specify a year
|
|
|
|
|
/.*BDAY.*:([\d-]*).*/
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def name_regex
|
|
|
|
|
/FN.*:(.*)/
|
2024-03-02 14:50:16 +00:00
|
|
|
|
end
|
2024-03-03 14:47:25 +00:00
|
|
|
|
|
|
|
|
|
def month_of(date)
|
2024-03-03 20:19:33 +00:00
|
|
|
|
date.tr("-", "")[-4..-3]
|
2024-03-03 14:47:25 +00:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def day_of(date)
|
2024-03-03 20:19:33 +00:00
|
|
|
|
date.tr("-", "")[-2..-1]
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def birth_year_of(date)
|
|
|
|
|
return nil if date.length < 8
|
|
|
|
|
|
|
|
|
|
birth_year = date[0..3]
|
|
|
|
|
return nil if birth_year == "1604" # This is set (for some reason) by DAVx⁵
|
|
|
|
|
birth_year
|
2024-03-03 14:47:25 +00:00
|
|
|
|
end
|
2024-03-02 14:50:16 +00:00
|
|
|
|
end
|