Last active
November 23, 2016 21:05
-
-
Save igneus/67f76da3031b4113e8569666756a36c4 to your computer and use it in GitHub Desktop.
On feast days display feast title
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
# 2016-11-18 | |
# connects to the Church Calendar API | |
# If there is a celebration of rank feast or higher, prints it's title | |
# For me to fill a textbox widget in awesome: | |
# In rc.lua: | |
# | |
# calendarbox = widget({ type = "textbox" }) | |
# calendarbox.text = awful.util.pread("/path/to/feast --calendar=czech-pha") | |
require "http/client" | |
require "json" | |
require "option_parser" | |
api_uri = "http://calapi.inadiutorium.cz/api/v0" | |
calendar = "default" | |
$verbose = false | |
rank = 3 | |
day = "today" | |
def verbose_puts(s, io=STDOUT) | |
return unless $verbose | |
io.puts s | |
end | |
OptionParser.parse! do |parser| | |
parser.banner = "Usage: feast [options]" | |
parser.on("-c", "--calendar=CAL", "API ID of the calendar to query") {|c| calendar = c } | |
parser.on("-v", "--verbose", "Enable verbose output") { $verbose = true } | |
parser.on("-u", "--uri=URI", "API base URI - default: #{api_uri}") {|u| api_uri = u } | |
parser.on("-r", "--rank=RANK", "Maximum (the smaller number, the greater rank) celebration rank to print - default: #{rank}") {|r| rank = r.to_f } | |
parser.on("-d", "--day=DAY", "Day") {|d| day = d } | |
parser.on("-h", "--help", "Show this help") { puts parser } | |
end | |
endpoint_uri = api_uri + "/en/calendars/" + calendar + "/" + day | |
verbose_puts "GET #{endpoint_uri}" | |
response = HTTP::Client.get endpoint_uri | |
if response.status_code != 200 | |
verbose_puts "Error: API returned unexpected HTTP status #{response.status_code}. Exiting.", STDERR | |
exit(1) | |
end | |
json = response.body.lines.join("") | |
verbose_puts "Received:\n#{json}" | |
verbose_puts "Parsing JSON" | |
data = JSON.parse(json) | |
verbose_puts "Searching for a celebration with rank of less than #{rank}" | |
feast = data["celebrations"].find do |c| | |
c["rank_num"].as_f < rank && | |
!c["title"].as_s.empty? | |
end | |
if feast | |
verbose_puts "Found celebration of rank #{feast["rank_num"]}:" | |
puts feast["title"] | |
else | |
verbose_puts "No celebration matching conditions found." | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment