A Guide to Pulling Data From MeetUp’s API

MeetUp is a social media website that allows members to form virtual communities that organize events in their local geographic area. The concept MeetUp has had isn’t new, but the UI and UX has been the most innovative in recent years. Streamlining the tasks of organizing events specific to a community that might meet daily, weekly, monthly or bi-monthly has been made easier.

MeetUp’s API allows for users to collect data from the website directly. Like most API’s many domain owners may limit the amount of data you can call.  Some domains don’t do an excellent job at maintaining the documentation of their API. Some do amazing work with making their documentation as user friendly as possible. MeetUp is somewhere in the middle. There are many ways you can use their API.

My use of MeetUp’s API was to collect data from three different groups within the Dallas-Fort Worth area in North Texas.  The language I used to access the API on MeetUp’s domain was Python. Depending on what you’re comfortable with, C++ or Python can be sufficient in using a website’s API.

Below I will demonstrate, step by step how to call MeetUp’s API  using Python. I will show how  you can obtain a group’s  ID, and  how you can use that ID with your API Key to  make a Request for a group’s data in JSON format.  Finally, I will demonstrate how you can write the captured data into a JSON file that will save on your PC/Mac.

Tools Used:

  • Python 3
  • Jupyter Notebook
  • MeetUp.API

 

import pandas as pd
import meetup.api
import json
import requests
import time
import codecs
import sys
import io

First, we want to import all of the  Python libraries that will be used in our simple py script.   Please make sure you have meetup.api installed on your machine before proceeding with running this input.  A simple pip install is all that’s needed if you’re using a Linux based command prompt.

client = meetup.api.Client(‘API Key Here’)
group_info = client.GetGroup({‘urlname’: ‘Group Name Here No Spaces’})
group_info
group_info.link
group_info.id

Since Python is an interpretative language, defining  plays an important role in coding.  For example, by defining client we can now write any requests and simply use the term client to call the API key therefore having no need to continuously retype the API Key. This is an important aspect of coding with Python.

Defining group_info allows us to pull a group’s url  based upon the group’s name on MeetUp’s website.   Once this is done, we can now call group_info, and make several requests with it.  By calling group_info.link  the output will be the group’s complete URL address. Calling  group_info.id will get you the group’s ID number.  We want the Group’s ID number to be able to move forward with the next step.

df = requests.get(‘https://api.meetup.com/2/members?group_id=GROUP_ID&key=API_KEY’)

This was a point of controversy for me. Because essentially, I called my API earlier by defining ‘client’ in-practice I shouldn’t have to do it again, instead I should be able to pull my request with client.requests.get(‘group-name’) .  Unfortunately, this did not work, I spent a few hours reading through the documentation which led me to what I typed above.  I knew my group’s ID # already, and so I can pull the request by defining df and putting my group id  and API key in the url.  Again, the goal is to successfully pull a group’s data using MeetUp’s API into a JSON file.

df.status_code
df.headers[‘content-type’]
df.encoding
df.json()
data = df.json()

With df defined, you can now call the status_code, headers, encoding, and you can directly view the data by calling the json. If the output looks optimal Your output for status_code should be: 200. Output for headers: ‘application/json;charset=utf-8’. The output for encoding: utf-8.  The output for json should be the data from the group itself.

The next step is to write  the data into a json file that saves onto your PC/MAC.  This is an important step. You already do have the data, but the data is so far not saved on your local hard-drive.  The following code is what I used below.

try:
to_unicode = unicode
except NameError:
to_unicode = str
with io.open(‘dfw3.json’, ‘w’, encoding=’utf8′) as outfile:
str_ = json.dumps(data,
indent=4, sort_keys=True,
separators=(‘,’, ‘: ‘), ensure_ascii=False)
outfile.write(to_unicode(str_))

Before running that, make sure you’ve imported io library. The moment you run this code, it will write the data from the Request pull you made using MeetUp’s API into a JSON file saved onto your hard-drive directory. Specifically located in the same directory as the ipynb file you’re using,

if you’re using Jupyter Notebook. You should be able to find it immediately if you organize your directory on Jupyter Notebook in order of last modified.  Feel free to save it into a more convenient location for future use.

 

 

 

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: