From 44f0dadfc7e9f153a87a2a8d86669f189cdc4314 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sun, 21 Dec 2014 10:55:25 +0000 Subject: [PATCH] Add Meetup polling for SFVLUG --- sfvlug.awk | 140 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 112 insertions(+), 28 deletions(-) diff --git a/sfvlug.awk b/sfvlug.awk index 0426aa4..4a40a8e 100644 --- a/sfvlug.awk +++ b/sfvlug.awk @@ -1,5 +1,5 @@ # SFV Lug -function email(to, from, subj, body, sendmail) +function sfvlug_email(to, from, subj, body, sendmail) { from = NICK "" subj = "Update sfvlug.org!" @@ -14,34 +14,118 @@ function email(to, from, subj, body, sendmail) close(sendmail) } -BEGIN { pollchan = "#sfvlug" } -(CMD == "PING" && systime()-lastpoll > 60*60*24) || -(CMD == "PRIVMSG" && /^\.poll/) { - if (!TOPICS[pollchan]) { - debug("Unknown topic for " pollchan); - send("TOPIC " pollchan) - next +function sfvlug_website( curl, day, tba, ptrn, line, date, parts) +{ + debug("Polling SFVLUG Website"); + + curl = "curl -s http://sfvlug.org/" + day = "(Sun|Mon|Tue|Wed|Thu|Fri|Sat)" + tba = "next meeting is: TBA" + ptrn = "next meeting.*" day "\\w+[, ]+([A-Z]\\w+) +([0-9]+)[, ]+([0-9]+)" + while (curl | getline line) { + if (match(line, tba)) + debug("Website date is TBA"); + if (match(line, ptrn, parts)) + date = parts[1] " " parts[2] " " parts[3] + #sfvlug_email("Brian "); + } + close(curl) + return date +} + +function sfvlug_meetup( url, curl, line, text, name, where, when) +{ + debug("Polling SFVLUG Meetup"); + + # Signed API URL (this cannot be changed) + url = "http://api.meetup.com/2/events?" \ + "group_id=2575122&" \ + "status=upcoming&" \ + "order=time&" \ + "limited_events=False&" \ + "desc=false&" \ + "offset=0&" \ + "photo-host=public&" \ + "format=json&" \ + "page=500&" \ + "fields=&" \ + "sig_id=28045742&" \ + "sig=42d2f7ec48b697ba087db2d8f2c65a2f144de8b1" + + # Download JSON data + curl = "curl -s \'" url "\'" + while (curl | getline line) + text = text "\n" line + close(curl); + + # Parse JSON and save extracted data + json_decode(text, events) + if (!isarray(events) || + !isarray(events["results"]) || + !isarray(events["results"][0]) || + !isarray(events["results"][0]["venue"])) { + debug("No results from Meetup"); + return + } + for (key in events["results"][0]) + json_copy(sfvlug_event, key, events["results"][0][key]) + + # Lookup time + name = sfvlug_event["name"] + where = sfvlug_event["venue"]["name"] " " \ + sfvlug_event["venue"]["city"] + when = sfvlug_event["time"] + \ + sfvlug_event["utc_offset"] + when = strftime("%a %B %d, %l:%M%P", when/1000) + gsub(/[.!?:]+$/, "", name) + gsub(/Restaurant/, "", where) + gsub(/ +/, " ", where) + gsub(/ +/, " ", when) + debug("event: ...\n" json_encode(sfvlug_event)) + debug("name: " name) + debug("when: " when) + debug("where: " where) + return name ": " when " " where +} + +function sfvlug_update(chan) +{ + # Make sure we have the current topic + if (!TOPICS[chan]) { + debug("Unknown topic for " chan); + send("TOPIC " chan) + return } - _curl = "curl -s http://sfvlug.org/" - _day = "(Sun|Mon|Tue|Wed|Thu|Fri|Sat)" - _web_tba = "next meeting is: TBA" - _web_ptrn = "next meeting.*" _day "\\w+[, ]+([A-Z]\\w+) +([0-9]+)[, ]+([0-9]+)" - _irc_ptrn = _day "\\w*[, ]+([A-Z]\\w+) +([0-9]+)" - while (_curl | getline _line) { - #if (match(_line, _web_tba)) - # email("Brian "); - if (match(_line, _web_ptrn, _parts)) { - _date = _parts[1] " " _parts[2] " " _parts[3] - _topic = TOPICS[pollchan] - sub(_irc_ptrn, _date, _topic) - if (_topic != TOPICS[pollchan]) - topic(pollchan, TOPICS[pollchan] = _topic) - else - debug("topic is already correct") - break - } + + # Testing + #text = sfvlug_website() + text = sfvlug_meetup() + if (!text) + return + + # Update IRC + update = TOPICS[chan] + sub(/\| [^|]+ \|/, "| " text " |", update) + if (update != TOPICS[chan]) { + topic(chan, TOPICS[chan] = update) + } else { + debug("topic is already correct") } - lastpoll = systime() - close(_curl) +} + +# Main +BEGIN { + debug("Loading SFVLUG"); + sfvlug_channel = "#sfvlug" + sfvlug_polled = 0 + sfvlug_event["time"] = 0 + sfvlug_event["name"] = 0 +} + +(CMD == "PING" && systime()-sfvlug_polled > 60*60*24) || +(CMD == "PRIVMSG" && /^\.poll/) { + debug("Updating SFVLUG topic") + sfvlug_update(sfvlug_channel) + sfvlug_polled = systime() } -- 2.43.2