Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meetup json output, minor fixes #6339

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions tools/events/meetup-automation/event.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import string
from dataclasses import dataclass
from datetime import datetime

Expand All @@ -12,6 +13,32 @@ class Location:
state: None | str
country: None | str

def __post_init__(self):
""" Normalize our location strings here """
if self.city:
# capwords does the "heavy lifting" of our formatting here https://docs.python.org/3/library/string.html#string.capwords
self.city = string.capwords(self.city)
else:
self.city = None

if self.state:
self.state = self.state.strip()
self.state = self.state.upper()
else:
self.state = None

if self.country:
self.country = self.country.strip()
self.country = self.country.upper()
else:
self.country = None

if self.country == "GB":
# looks like in GB meetup considers part of the post code as the "state", which is not a common way to write
# locations in GB (or that's my understanding at least)
self.state = None


def fields_present(self) -> int:
""" Check how many fields are present, used to determine which Location has more information when comparing """
c = 0
Expand All @@ -29,13 +56,13 @@ def to_str(self) -> str:
s = ''

if self.city:
s += self.city.lower().capitalize()
s += self.city
if self.state:
s += ', '
s += self.state.upper()
s += self.state
if self.country:
s += ', '
s += self.country.upper()
s += self.country

return s

Expand All @@ -50,6 +77,23 @@ class Event:
organizer_name: str
organizer_url: str

def __post_init__(self):
""" Normalize the event data here """
self.name = self.name.strip()
self.organizer_name = self.organizer_name.strip()

def to_dict(self) -> dict:
""" Method for serializing to a dict which can be further serialized to json """
return {
"name": self.name,
"location": self.location.to_str(),
"date": self.date.strftime("%Y-%m-%d"),
"url": self.url,
"virtual": self.virtual,
"organizer_name": self.organizer_name,
"organizer_url": self.organizer_url
}

def to_markdown_string(self) -> str:
location = f"Virtual ({self.location.to_str()})" if self.virtual else self.location.to_str()

Expand Down
11 changes: 9 additions & 2 deletions tools/events/meetup-automation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
# print to console / output to file formatted markdown

import argparse
import json
import logging
from typing import List

Expand Down Expand Up @@ -48,15 +49,21 @@ def main():
# Group by virtual or by continent.
events = group_virtual_continent(events)

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


def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description='Fetches meetup events for TWIR')
parser.add_argument("-d", "--debug", action="store_true", dest="debug", help="Enable debug logging")
parser.add_argument("-g", "--groups", action="store", type=str, dest="groups_file", required=True, help="File with a JSON array of meetup group URLS")
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")
parser.add_argument("-j", "--json", action="store_true", dest="json", help="Output events as JSON rather than TWIR-formatted")

return parser.parse_args()

Expand Down