]> Pileus Git - ~andy/rhawk/blob - sfvlug.awk
Add limit to tied message
[~andy/rhawk] / sfvlug.awk
1 # SFV Lug
2 function sfvlug_email(to,   from, subj, body, sendmail)
3 {
4         from     = NICK "<andy@pileus.org>"
5         subj     = "Update sfvlug.org!"
6         body     = "The next SFVLUG meeting is TBA!"
7         sendmail = "/usr/sbin/sendmail '" to "'"
8         print "To: " to        | sendmail
9         print "From: " from    | sendmail
10         print "Subject: " subj | sendmail
11         print ""               | sendmail
12         print body             | sendmail
13         say("Topic out of date, emailing " to);
14         close(sendmail)
15 }
16
17 function sfvlug_website(   curl, day, tba, ptrn, line, date, parts)
18 {
19         debug("Polling SFVLUG Website");
20
21         curl = "curl -s http://sfvlug.org/"
22         day  = "(Sun|Mon|Tue|Wed|Thu|Fri|Sat)"
23         tba  = "next meeting is: TBA"
24         ptrn = "next meeting.*" day "\\w+[, ]+([A-Z]\\w+) +([0-9]+)[, ]+([0-9]+)"
25         while (curl | getline line) {
26                 if (match(line, tba))
27                         debug("Website date is TBA");
28                 if (match(line, ptrn, parts))
29                         date = parts[1] " " parts[2] " " parts[3]
30                 #sfvlug_email("Brian <brian@zimage.com>");
31         }
32         close(curl)
33         return date
34 }
35
36 function sfvlug_meetup(   url, curl, line, text, name, where, when)
37 {
38         debug("Polling SFVLUG Meetup");
39
40         # Signed API URL (this cannot be changed)
41         url = "http://api.meetup.com/2/events?" \
42                         "group_id=2575122&"     \
43                         "status=upcoming&"      \
44                         "order=time&"           \
45                         "limited_events=False&" \
46                         "desc=false&"           \
47                         "offset=0&"             \
48                         "photo-host=public&"    \
49                         "format=json&"          \
50                         "page=500&"             \
51                         "fields=&"              \
52                         "sig_id=28045742&"      \
53                         "sig=42d2f7ec48b697ba087db2d8f2c65a2f144de8b1"
54
55         # Download JSON data
56         curl = "curl -s \'" url "\'"
57         while (curl | getline line)
58                 text = text "\n" line
59         close(curl);
60
61         # Parse JSON and save extracted data
62         json_decode(text, events)
63         if (!isarray(events) ||
64             !isarray(events["results"]) ||
65             !isarray(events["results"][0]) ||
66             !isarray(events["results"][0]["venue"])) {
67                 debug("No results from Meetup");
68                 return
69         }
70         for (key in events["results"][0])
71                 json_copy(sfvlug_event, key, events["results"][0][key])
72
73         # Lookup time
74         name   = sfvlug_event["name"]
75         where  = sfvlug_event["venue"]["name"] " " \
76                  sfvlug_event["venue"]["city"]
77         when   = sfvlug_event["time"] + \
78                  sfvlug_event["utc_offset"]
79         when   = strftime("%a %B %d, %l:%M%P", when/1000)
80         gsub(/[.!?:]+$/,   "",  name)
81         gsub(/Restaurant/, "",  where)
82         gsub(/  +/,        " ", where)
83         gsub(/  +/,        " ", when)
84         debug("event: ...\n" json_encode(sfvlug_event))
85         debug("name:  " name)
86         debug("when:  " when)
87         debug("where: " where)
88         return name ": " when " " where
89 }
90
91 function sfvlug_update(chan)
92 {
93         # Make sure we have the current topic
94         if (!TOPICS[chan]) {
95                 debug("Unknown topic for " chan);
96                 send("TOPIC " chan)
97                 return
98         }
99
100         # Testing
101         #text = sfvlug_website()
102         text = sfvlug_meetup()
103         if (!text)
104                 return
105
106         # Update IRC
107         update = TOPICS[chan]
108         sub(/\| [^|]+ \|/, "| " text " |", update)
109         if (update != TOPICS[chan]) {
110                 topic(chan, TOPICS[chan] = update)
111         } else {
112                 debug("topic is already correct")
113         }
114 }
115
116 # Main
117 BEGIN {
118         debug("Loading SFVLUG");
119         sfvlug_channel       = "#sfvlug"
120         sfvlug_polled        = 0
121         sfvlug_event["time"] = 0
122         sfvlug_event["name"] = 0
123 }
124
125 (CMD == "PING"    && systime()-sfvlug_polled > 60*60*24) ||
126 (CMD == "PRIVMSG" && /^\.poll/) {
127         debug("Updating SFVLUG topic")
128         sfvlug_update(sfvlug_channel)
129         sfvlug_polled = systime()
130 }
131
132 /^.meeting/ {
133         if (!sfvlug_meetup()) {
134                 say("Error looking up meeting")
135                 next
136         }
137
138         _name  = sfvlug_event["name"]
139         _desc  = sfvlug_event["description"]
140         _where = sfvlug_event["venue"]["name"]  ", " \
141                  sfvlug_event["venue"]["city"]  ", " \
142                  sfvlug_event["venue"]["state"] ", " \
143                  sfvlug_event["venue"]["zip"]
144         _when  = sfvlug_event["time"] + \
145                  sfvlug_event["utc_offset"]
146         _when  = strftime("%a %B %d, %l:%M%P", _when/1000)
147         _desc = gensub(/<[^>]*>/, "",  "g", _desc);
148         _desc = gensub(/\\(.)/, "\\1", "g", _desc);
149
150         say("Next meeting: " _name)
151         say("Time: "         _when)
152         say("Location: "     _where)
153         say("Details: "      _desc)
154 }