Skip to content

Commit c867ff9

Browse files
authored
Meetup json output, minor fixes (#6339)
* add some data normalization and formatting for event names and locations * add support for outputting events as json
1 parent 77922a8 commit c867ff9

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

tools/events/meetup-automation/event.py

+47-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import string
23
from dataclasses import dataclass
34
from datetime import datetime
45

@@ -12,6 +13,32 @@ class Location:
1213
state: None | str
1314
country: None | str
1415

16+
def __post_init__(self):
17+
""" Normalize our location strings here """
18+
if self.city:
19+
# capwords does the "heavy lifting" of our formatting here https://docs.python.org/3/library/string.html#string.capwords
20+
self.city = string.capwords(self.city)
21+
else:
22+
self.city = None
23+
24+
if self.state:
25+
self.state = self.state.strip()
26+
self.state = self.state.upper()
27+
else:
28+
self.state = None
29+
30+
if self.country:
31+
self.country = self.country.strip()
32+
self.country = self.country.upper()
33+
else:
34+
self.country = None
35+
36+
if self.country == "GB":
37+
# looks like in GB meetup considers part of the post code as the "state", which is not a common way to write
38+
# locations in GB (or that's my understanding at least)
39+
self.state = None
40+
41+
1542
def fields_present(self) -> int:
1643
""" Check how many fields are present, used to determine which Location has more information when comparing """
1744
c = 0
@@ -29,13 +56,13 @@ def to_str(self) -> str:
2956
s = ''
3057

3158
if self.city:
32-
s += self.city.lower().capitalize()
59+
s += self.city
3360
if self.state:
3461
s += ', '
35-
s += self.state.upper()
62+
s += self.state
3663
if self.country:
3764
s += ', '
38-
s += self.country.upper()
65+
s += self.country
3966

4067
return s
4168

@@ -50,6 +77,23 @@ class Event:
5077
organizer_name: str
5178
organizer_url: str
5279

80+
def __post_init__(self):
81+
""" Normalize the event data here """
82+
self.name = self.name.strip()
83+
self.organizer_name = self.organizer_name.strip()
84+
85+
def to_dict(self) -> dict:
86+
""" Method for serializing to a dict which can be further serialized to json """
87+
return {
88+
"name": self.name,
89+
"location": self.location.to_str(),
90+
"date": self.date.strftime("%Y-%m-%d"),
91+
"url": self.url,
92+
"virtual": self.virtual,
93+
"organizer_name": self.organizer_name,
94+
"organizer_url": self.organizer_url
95+
}
96+
5397
def to_markdown_string(self) -> str:
5498
location = f"Virtual ({self.location.to_str()})" if self.virtual else self.location.to_str()
5599

tools/events/meetup-automation/main.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# print to console / output to file formatted markdown
55

66
import argparse
7+
import json
78
import logging
89
from typing import List
910

@@ -48,15 +49,21 @@ def main():
4849
# Group by virtual or by continent.
4950
events = group_virtual_continent(events)
5051

51-
# Output Sorted Event List.
52-
output_to_screen(events)
52+
# if json is specified, output as json. Otherwise print in a nice TWIR-formatted way
53+
if args.json:
54+
# convert our events to a json-friendly format
55+
formatted = {continent: [event.to_dict() for event in event_list] for continent, event_list in events.items()}
56+
print(json.dumps(formatted))
57+
else:
58+
output_to_screen(events)
5359

5460

5561
def parse_args() -> argparse.Namespace:
5662
parser = argparse.ArgumentParser(description='Fetches meetup events for TWIR')
5763
parser.add_argument("-d", "--debug", action="store_true", dest="debug", help="Enable debug logging")
5864
parser.add_argument("-g", "--groups", action="store", type=str, dest="groups_file", required=True, help="File with a JSON array of meetup group URLS")
5965
parser.add_argument("-w", "--weeks", action="store", type=int, dest="weeks", default=5, help="Number of weeks to search for events from, starting next Wednesday")
66+
parser.add_argument("-j", "--json", action="store_true", dest="json", help="Output events as JSON rather than TWIR-formatted")
6067

6168
return parser.parse_args()
6269

0 commit comments

Comments
 (0)