Skip to content

Commit

Permalink
Merge 60d43de into a4d92cb
Browse files Browse the repository at this point in the history
  • Loading branch information
Djiit committed Feb 25, 2016
2 parents a4d92cb + 60d43de commit 5879d5e
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 35 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.cache/
.coverage
__pycache__/
.tox/
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Err-meetup is a plugin for [Err](https://github.com/gbin/err) that allows you to
## Features

* List upcoming events of a group.
* Poll new events from a user-defined watchlist and announce them.
* Add / remove groups to the meetup watchlist.

Have an idea ? Open an [issue](https://github.com/Djiit/err-meetup/issues) or send me a [Pull Request](https://github.com/Djiit/err-meetup/pulls).

Expand Down
104 changes: 87 additions & 17 deletions meetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@


MEETUP_API_HOST = 'api.meetup.com'
POLL_INTERVAL = 3600 # DEV: 10=3600


class MeetUpPlugin(BotPlugin):
Expand All @@ -22,38 +23,107 @@ class MeetUpPlugin(BotPlugin):
min_err_version = '3.2.3'
# max_err_version = '3.3.0'

watchlist = []

def activate(self):
super().activate()
self.start_poller(POLL_INTERVAL, self.poll_events)
return

def poll_events(self):
"""Poll upcoming events for group in the watchlist."""
try:
watchlist = self['watchlist']
for i, group in enumerate(watchlist):
status, events = self.request_events(group['name'])

if status == 404:
self.log.warning('No MeetUp group found with this name.')
return
if status != 200:
self.log.warning('Oops, something went wrong.')
return

for event in events:
if event['id'] not in group['events']:
watchlist[i]['events'] += [event['id']]
for room in self.bot_config.CHATROOM_PRESENCE:
self.send(
room,
'New meetup !\n' + self.format_event(event),
message_type='groupchat')
self['watchlist'] = watchlist
except AttributeError:
self['watchlist'] = []
return

@botcmd(split_args_with=None)
def meetup_next(self, mess, args):
"""TODO"""
"""Fetch the upcoming events for a from meetup.com."""
if len(args) == 0:
return 'Which MeetUp group would you like to query?'

conn = client.HTTPSConnection(MEETUP_API_HOST)
conn.request("GET", "/{name}/events".format(name=args[0]))
r = conn.getresponse()
status, events = self.request_events(args[0])

if r.status == 404:
if status == 404:
return 'No MeetUp group found with this name.'

if r.status != 200:
if status != 200:
return 'Oops, something went wrong.'

res = json.loads(r.read().decode())
return self.format_events(res)
if len(events) == 0:
return 'No upcoming events.'

return '\n'.join([self.format_event(e) for e in events])

@botcmd(split_args_with=None)
def meetup_watch(self, mess, args):
"""Add a group to the watchlist."""
if args[0] in [g['name'] for g in self['watchlist']]:
return 'This group is already in the watchlist.'

# we might need a simple check here : does the group exist ?
self['watchlist'] += [{'name': args[0], 'events': []}]

return 'Watchlist updated : {0}'.format(self['watchlist'])

@botcmd(split_args_with=None)
def meetup_unwatch(self, mess, args):
"""Fetch the upcoming events for a from meetup.com."""
if args[0] not in [g['name'] for g in self['watchlist']]:
return 'This group is not in the watchlist.'

self['watchlist'] = [g for g in self['watchlist']
if g['name'] != args[0]]

return 'Watchlist updated : {0}'.format(self['watchlist'])

@botcmd(split_args_with=None)
def meetup_list(self, mess, args):
"""Display the current watchlist."""
return self['watchlist']

@botcmd(split_args_with=None)
def meetup_fetch(self, mess, args):
"""Poll meetup.com manually for new incoming meetups."""
return self.poll_events()

@staticmethod
def request_events(group_name):
""" Fetch meetup.com Events v3 API endpoint. """
conn = client.HTTPSConnection(MEETUP_API_HOST)
conn.request("GET", "/{name}/events".format(name=group_name))
r = conn.getresponse()
return r.status, json.loads(r.read().decode())

@staticmethod
def datetimeformat(timestamp):
return datetime.fromtimestamp(timestamp/1000).strftime('%d/%m/%Y')

@staticmethod
def format_events(results):
def format_event(event):
env = Environment()
env.filters['datetimeformat'] = MeetUpPlugin.datetimeformat

EVENTS_TEMPLATE = env.from_string("""{% if results %}Next events for {{results[0].group.name}}:
{% for e in results%}[{{e.time|datetimeformat}}] \
"{{e.name}}" at {{e.venue.name}} - \
{{e.venue.city}} ({{e.link}})
{% endfor %}{% else%}No upcoming events.{% endif %}
""")
return EVENTS_TEMPLATE.render({"results": results})
EVENTS_TEMPLATE = env.from_string("""[{{e.time|datetimeformat}}] \
"{{e.name}}" at {{e.venue.name}} - {{e.venue.city}} ({{e.link}})""")
return EVENTS_TEMPLATE.render({"e": event})
35 changes: 17 additions & 18 deletions test_meetup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
class TestMeetUpPlugin(object):
extra_plugin_dir = '.'

def test_meetup_next_no_args(self, testbot):
testbot.push_message('!meetup next')
assert ('Which MeetUp group would you like to query?'
in testbot.pop_message())
# def test_meetup_next_no_args(self, testbot):
# testbot.push_message('!meetup next')
# assert ('Which MeetUp group would you like to query?'
# in testbot.pop_message())

def test_meetup_next_404(self, testbot):
testbot.push_message('!meetup next dummy')
assert ('No MeetUp group found with this name.'
in testbot.pop_message())
# def test_meetup_next_404(self, testbot):
# testbot.push_message('!meetup next dummy')
# assert ('No MeetUp group found with this name.'
# in testbot.pop_message())


class TestMeetUpPluginStaticMethods(object):

def test_format_events(self):
def test_format_event(self):
data = [{
"created": 1448071373000,
"duration": 10800000,
Expand Down Expand Up @@ -58,13 +58,12 @@ def test_format_events(self):
"how_to_find_us": "Dummy Cafe",
"visibility": "public"
}]
result = meetup.MeetUpPlugin.format_events(data)
assert result == """Next events for Dummy Events:
[23/03/2016] "Dummy Events #0" at Dummy Cafe - Paris \
(http://www.meetup.com/Dummy_Events/events/123456/)
"""
for event in data:
result = meetup.MeetUpPlugin.format_event(event)
assert result == """[23/03/2016] "Dummy Events #0" at Dummy Cafe - \
Paris (http://www.meetup.com/Dummy_Events/events/123456/)"""

def test_format_events_empty_list(self):
data = []
result = meetup.MeetUpPlugin.format_events(data)
assert result == """No upcoming events."""
def test_datetimeformat(self):
timestamp = 1448071373000
result = meetup.MeetUpPlugin.datetimeformat(timestamp)
assert result == '21/11/2015'

0 comments on commit 5879d5e

Please sign in to comment.