Skip to content

Instantly share code, notes, and snippets.

@justinabrahms
Forked from florapdx/meetups_indiv.js
Last active December 14, 2015 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinabrahms/5021133 to your computer and use it in GitHub Desktop.
Save justinabrahms/5021133 to your computer and use it in GitHub Desktop.
// Make a single api call to meetup.com and grab event info for all (11) PyLadies locations. Create individual objects for each so that meetups can be added to each pyladies group's div on pyladies.com/locations page.
//helper function to discover event urls and make active links
//credit to http://www.sencha.com/forum/archive/index.php/t-12379.html for code this is based on
function create_urls(input) {
return input
.replace(/(ftp|http|https|file):\/\/[\S]+(\b|$)/gim, '"$&" target="_blank"')
.replace(/([^\/])(www[\S]+(\b|$))/gim, '"http://$2" target="_blank"');
} //end url parsing helper function
//helper to convert the date of each meetup event from time in miliseconds to a readable date format
function convert_time_miliseconds_to_date(t) {
var d = new Date(t); // The 0 there is the key, which sets the date to the epoch
return String(d).slice(0, 11);
}// end convert_time..
// Make the API call to return data object containing event objects for all pyladies chapters
// *make sure variables surrounded by single quotes as below*
var group_ids = '/* ids for current pyladies chapters in comma-separated list goes here (no spaces); find by visiting: http://www.meetup.com/meetup_api/console/ and paste: /groups?group_urlname=your_group_name_here in the console */';
var meetup_key = '/* your meetup api key from: http://www.meetup.com/meetup_api/key goes here */';
// This is our primary function, which makes call to return json from meetup api and passes that data on to our other functions
function addUpcomingMeetups() {
$.ajax( {
url: 'http://api.meetup.com/2/events.json?key='+meetup_key+'&group_id='+group_ids+'&fields=group_photo&time=0m,1m&status=upcoming&sign=true',
dataType: 'jsonp',
success: function(data) {
// this is a map from group id -> a list of events for this group
var group_event_map = {};
// this is a map from group id -> the group object which says the group's name, etc.
var group_info_map = {};
for (var event in data.results) {
// if we don't already have a result for this, initialize it with a list
var group_id = event.group.id;
if (!(group_id in group_event_map)) {
group_event_map[group_id] = [];
}
group_event_map[group_id].push(event);
group_info_map[group_id] = event.group;
}
for (var group_id in group_event_map) {
var html_list = buildHtml(group_event_map[group_id]);
attach_html(group_id, html_list);
}
},
error: function(data) {
errorHtml(data);
}
});
return false; //end .ajax()
} //end getUpcomingMeetups
function attach_html(group_id, html_list) {
var $ul = $('[data-meetup-id="'+group_id+'"]').find('ul');
$ul.find('li').remove();
html_list.each(function (element) {
$ul.append('<li>' + element + '</li>');
});
}
// iterate through our new master array of groups' events in order to build and place html
// needs: group info, meetup_list,
function buildHtml(events_list) {
var event_list_html = [];
for (var event in events_list) {
var html = '';
var url_link = create_urls(event.event_url); // make active links from urls
var event_date = convert_time_miliseconds_to_date(event.time); // convert miliseconds-since-epoch to standard date format
html += '<img class="meetup_group_icon" src="'+event.group.group_photo.thumb_link+'" height="80" width="80" />';
html += '<p class="meetup_listing_group">'+event.group.name+'</p>';
html += '<p class="meetup_listing_event_title"><a href='+url_link+'>'+event.name+'</a></p>';
html += '<p class="meetup_event_date">'+event_date+'</p>';
event_list_html.push(html);
}
return event_list_html;
}
function errorHtml(data) {
alert("error");
// Stubbed out for testing purposes only since production halted
// Could do something along the lines of:
// $('.page .chapter_location li').remove();
// $('.page .chapter_location').find('ul').append('<li>We're sorry. Meetup is not responding at this time.</li>'); etc.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment