Example #1
0
function process_job(&$jobData)
{
    $data = json_decode($jobData->getData());
    if (!is_array($data)) {
        echo "Found bad job:\n";
        print_r($data);
        echo "\n";
        bs()->delete($jobData);
        continue;
    }
    echo "===============================================\n";
    echo "# Beginning job\n";
    print_r($data);
    foreach ($data as $job) {
        // Get the instagram access token for this user ID
        $user = ORM::for_table('users')->where('instagram_user_id', $job->object_id)->find_one();
        if ($user) {
            if ($user->micropub_success) {
                // Retrieve recent photos for the user after the time specified in the post
                // https://api.instagram.com/v1/users/self/media/recent?min_timestamp=1394295266
                $timestamp = $job->time;
                $media_id = $job->data->media_id;
                if ($photo = IG\get_photo($user, $media_id)) {
                    $entry = h_entry_from_photo($user, $photo);
                    $photo_url = $photo->images->standard_resolution->url;
                    // Download the photo to a temp folder
                    echo "Downloading photo...\n";
                    $filename = download_file($photo_url);
                    if (property_exists($photo, 'videos')) {
                        $video_url = $photo->videos->standard_resolution->url;
                        echo "Downloading video...\n";
                        $video_filename = download_file($video_url, 'mp4');
                    } else {
                        $video_filename = false;
                    }
                    // Send the photo to the micropub endpoint
                    echo "Sending photo" . ($video_filename ? " and video" : "") . " to micropub endpoint: " . $user->micropub_endpoint . "\n";
                    // Collapse category to a comma-separated list if they haven't upgraded yet
                    if ($user->send_category_as_array != 1) {
                        if ($entry['category'] && is_array($entry['category']) && count($entry['category'])) {
                            $entry['category'] = implode(',', $entry['category']);
                        }
                    }
                    print_r($entry);
                    echo "\n";
                    $response = micropub_post($user->micropub_endpoint, $user->micropub_access_token, $entry, $filename, $video_filename);
                    print_r($response);
                    echo "\n";
                    unlink($filename);
                    if ($video_filename) {
                        unlink($video_filename);
                    }
                    // Store the request and response from the micropub endpoint in the DB so it can be displayed to the user
                    $user->last_micropub_response = json_encode($response);
                    $user->last_instagram_photo = $photo->id;
                    $user->last_photo_date = date('Y-m-d H:i:s');
                    $user->save();
                    /*
                    // Add the link to the photo caption
                    
                    $comment_text = '';
                    
                    if($photo->caption && $photo->caption->id) {
                      $comment_id = $photo->caption->id;
                      $comment_text = $photo->caption->text;
                    
                      // Now delete the comment (caption) if there is one
                      $result = IG\delete_comment($user, $media_id, $comment_id);
                      print_r($result);
                    }
                    
                    // Re-add the caption with the citation 
                    $canonical = 'http://aaron.pk/xxxxx';
                    $comment_text .= ' ('.$canonical.')';
                    $result = IG\add_comment($user, $media_id, $comment_text);
                    print_r($result);
                    */
                }
            } else {
                echo "This user has not successfully completed a test micropub post yet\n";
            }
        } else {
            echo "No user account found for Instagram user " . $job->object_id . "\n";
        }
    }
    echo "# Job Complete\n-----------------------------------------------\n\n";
    bs()->delete($jobData);
}
Example #2
0
                $app->redirect('/auth/instagram');
            }
        }
        $app->redirect('/dashboard');
    }
});
$app->get('/dashboard', function () use($app) {
    if ($user = require_login($app)) {
        // If the user hasn't connected their Instagram account yet, redirect to the page to auth instagram
        if ($user->instagram_access_token == '') {
            $app->redirect('/auth/instagram-start');
        } else {
            // Go fetch the latest Instagram photo and show it to them for testing the micropub endpoint
            try {
                if ($photos = IG\get_latest_photos($user)) {
                    $entry = h_entry_from_photo($user, $photos[0]);
                    $photo_url = $photos[0]->images->standard_resolution->url;
                } else {
                    $entry = false;
                    $photo_url = false;
                }
            } catch (IG\AccessTokenException $e) {
                $user->instagram_access_token = '';
                $user->instagram_response = '';
                $user->save();
                $app->redirect('/auth/instagram-start');
            } catch (Exception $e) {
                $html = render('auth_error', array('title' => 'Error', 'error' => 'Error', 'errorDescription' => $e->getMessage()));
                $app->response()->body($html);
                return;
            }