-
-
Save zevaverbach/d25b2cca3c057b3cae74d14d9c396411 to your computer and use it in GitHub Desktop.
Create an HTML file of all previous events for a Meetup.com group. Forked from https://gist.github.com/samatjain/5ae8860b0754eb1fa3cc.
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
#!/usr/bin/env python3 | |
""" | |
This requires a _config.py file containing a dictionary called config. | |
There should be a dictionary in config for each meetup group, the key | |
being a shorthand title for the meetup group's name, and the values | |
dicts with key 'meetup_name' and 'meetup_api_key' with corresponding | |
values for those. | |
eg.: config = {'my_cool_meetup': | |
{'meetup_name': <the value after the '/' in your meetup URL>, | |
'meetup_api_key': <your API key>}, | |
} | |
The generated HTML will be copied to your clipboard, and an HTML file | |
will be generated in the current directory. | |
""" | |
import collections | |
import datetime | |
import pprint | |
import click | |
import jinja2 | |
import pyperclip | |
import requests | |
from _config import config | |
template = ''' | |
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> | |
<meta name=viewport content="width=device-width, initial-scale=1.0"> | |
<style type="text/css"> | |
td.date_pretty { | |
text-align: right; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Previous events for <a href="http://www.meetup.com/{{ group_name }}/">{{ group_name }}</a> | |
{% for grouping_name, events in groupings.items() %} | |
<h2>{{ grouping_name }}</h2> | |
<table>{% for i in events %} | |
<tr><td class="date_pretty">{{ i.date_pretty }}</td><td><a href="{{ i.event_url }}">{{ i.name }}</a></td></tr> | |
{%- endfor %}</table> | |
{% endfor %} | |
</body> | |
</html> | |
''' | |
default_payload = { 'status': 'past' } | |
def generate_html(group_name, g): | |
global template | |
je = jinja2.Environment() | |
jt = je.from_string(template) | |
out = jt.render(group_name=group_name, groupings=g) | |
return out | |
def generate_events(group_name, api_key): | |
offset = 0 | |
while True: | |
offset_payload = { 'offset': offset, | |
'key': api_key, | |
'group_urlname': group_name } | |
payload = default_payload.copy() | |
payload.update(offset_payload) | |
# Above is the equivalent of jQuery.extend() | |
# for Python 3.5: payload = {**default_payload, **offset_payload} | |
r = requests.get('https://api.meetup.com/2/events', params=payload) | |
json = r.json() | |
results, meta = json['results'], json['meta'] | |
for item in results: | |
yield item | |
# if we no longer have more results pages, stop… | |
if not meta['next']: | |
return | |
offset = offset + 1 | |
@click.command() | |
@click.option('--groupname', 'group') | |
def go(group): | |
group_config = config[group] | |
group_name = group_config['meetup_name'] | |
print(f'the group_name is {group_name}') | |
all_events = list( | |
generate_events(group_name=group_name, | |
api_key=config[group]['meetup_api_key'])) | |
for event in all_events: | |
# convert time returned by Meetup API | |
time = int(event['time'])/1000 | |
time_obj = datetime.datetime.fromtimestamp(time) | |
# create a pretty-looking date, and group by month | |
date_pretty = time_obj.strftime('%a %b %-d') | |
grouping_name = time_obj.strftime('%b %Y') | |
event['grouping_name'] = grouping_name | |
event['date_pretty'] = date_pretty | |
# group by month | |
groupings = collections.OrderedDict() | |
for event in all_events: | |
grouping_name = event['grouping_name'] | |
grouping = groupings.get(grouping_name, []) | |
grouping.append(event) | |
groupings[grouping_name] = grouping | |
html = generate_html(group_name, groupings) | |
pyperclip.copy(html) | |
print('the generated HTML is in your clipboard!') | |
with open(group_name + '_events.html', 'w') as fout: | |
fout.write(html) | |
print('your html file is generated!') | |
if __name__ == '__main__': | |
go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment