Making WordPress.org

Changeset 4838


Ignore:
Timestamp:
01/30/2017 03:43:58 PM (6 years ago)
Author:
iandunn
Message:

Official WordPress Events: Batch requests for meetup groups and events

The Meetup.com API limits v2/profiles results to 200 entries per response. This script was written when there were less than 200 groups on the chapter program, but there are now more than 350, so the results were being artifically limited, and only events from the first 200 groups were being shown.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/official-wordpress-events/official-wordpress-events.php

    r4814 r4838  
    1818     * @todo
    1919     *
     20     * Meetups only pulling 1 week instead of full month
     21     * Maybe pull more than 1 month of meetups
     22     * Make meetups and wordcamps cut off on the same date, so it doesn't look like there aren't any meetups later in the year
    2023     * Ability to feature a camp in a hero area
    2124     * Add a "load more" button that retrieves more events via AJAX and updates the DOM. Have each click load the next month of events?
     
    305308        }
    306309       
    307         $response = $this->remote_get( sprintf(
    308             '%s2/events?group_id=%s&time=0,1m&page=%d&key=%s',
    309             self::MEETUP_API_BASE_URL,
    310             implode( ',', $groups ),
    311             self::POSTS_PER_PAGE,
    312             MEETUP_API_KEY
    313         ) );
    314 
    315         $meetups = json_decode( wp_remote_retrieve_body( $response ) );
    316 
    317         if ( ! empty ( $meetups->results ) ) {
    318             $meetups = $meetups->results;
    319 
    320             foreach ( $meetups as $meetup ) {
    321                 $start_timestamp = ( $meetup->time / 1000 ) + ( $meetup->utc_offset / 1000 );    // convert to seconds
    322 
    323                 if ( isset( $meetup->venue ) ) {
    324                     $location = $this->format_meetup_venue_location( $meetup->venue );
    325                 } else {
    326                     $location = $this->reverse_geocode( $meetup->group->group_lat, $meetup->group->group_lon );
    327                     $location = $this->format_reverse_geocode_address( $location->address_components );
    328                 }
    329                
    330                 $events[] = new Official_WordPress_Event( array(
    331                     'type'            => 'meetup',
    332                     'title'           => $meetup->name,
    333                     'url'             => $meetup->event_url,
    334                     'start_timestamp' => $start_timestamp,
    335                     'end_timestamp'   => ( empty ( $meetup->duration ) ? $start_timestamp : $start_timestamp + ( $meetup->duration / 1000 ) ),  // convert to seconds
    336                     'location'        => $location,
    337                 ) );
     310        $groups = array_chunk( $groups, 200, true );
     311        foreach( $groups as $group_batch ) {
     312            $response = $this->remote_get( sprintf(
     313                '%s2/events?group_id=%s&time=0,1m&page=%d&key=%s',
     314                self::MEETUP_API_BASE_URL,
     315                implode( ',', $group_batch),
     316                self::POSTS_PER_PAGE,
     317                MEETUP_API_KEY
     318            ) );
     319
     320            $meetups = json_decode( wp_remote_retrieve_body( $response ) );
     321
     322            if ( ! empty ( $meetups->results ) ) {
     323                $meetups = $meetups->results;
     324
     325                foreach ( $meetups as $meetup ) {
     326                    $start_timestamp = ( $meetup->time / 1000 ) + ( $meetup->utc_offset / 1000 );    // convert to seconds
     327
     328                    if ( isset( $meetup->venue ) ) {
     329                        $location = $this->format_meetup_venue_location( $meetup->venue );
     330                    } else {
     331                        $location = $this->reverse_geocode( $meetup->group->group_lat, $meetup->group->group_lon );
     332                        $location = $this->format_reverse_geocode_address( $location->address_components );
     333                    }
     334
     335                    $events[] = new Official_WordPress_Event( array(
     336                        'type'            => 'meetup',
     337                        'title'           => $meetup->name,
     338                        'url'             => $meetup->event_url,
     339                        'start_timestamp' => $start_timestamp,
     340                        'end_timestamp'   => ( empty ( $meetup->duration ) ? $start_timestamp : $start_timestamp + ( $meetup->duration / 1000 ) ), // convert to seconds
     341                        'location'        => $location,
     342                    ) );
     343                }
    338344            }
    339345        }
     
    348354     */
    349355    protected function get_meetup_group_ids() {
     356        $group_ids = array();
     357
    350358        if ( ! defined( 'MEETUP_API_KEY' ) || ! MEETUP_API_KEY ) {
    351             return array();
     359            return $group_ids;
    352360        }
    353361       
    354         $response = $this->remote_get( sprintf(
     362        $request_url = sprintf(
    355363            '%s2/profiles?&member_id=%d&key=%s',
    356364            self::MEETUP_API_BASE_URL,
    357365            self::MEETUP_MEMBER_ID,
    358366            MEETUP_API_KEY
    359         ) );
    360 
    361         $group_ids = json_decode( wp_remote_retrieve_body( $response ) );
     367        );
     368
     369        while ( '' !== $request_url ) {
     370            $response = $this->remote_get( $request_url );
     371            $body     = json_decode( wp_remote_retrieve_body( $response ) );
    362372   
    363         if ( ! empty ( $group_ids->results ) ) {
    364             $group_ids = wp_list_pluck( $group_ids->results, 'group' );
    365             $group_ids = wp_list_pluck( $group_ids, 'id' );
    366         }
     373            if ( ! empty ( $body->results ) ) {
     374                $groups    = wp_list_pluck( $body->results, 'group' );
     375                $group_ids = array_merge( $group_ids, wp_list_pluck( $groups, 'id' ) );
     376            }
    367377       
    368         if ( ! isset( $group_ids ) || ! is_array( $group_ids ) ) {
    369             $group_ids = array();
     378            $request_url = $body->meta->next;
    370379        }
    371380       
Note: See TracChangeset for help on using the changeset viewer.