function micropub_post_for_user(&$user, $params, $file_path = NULL) { // Now send to the micropub endpoint $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token, $file_path); $user->last_micropub_response = substr(json_encode($r), 0, 1024); $user->last_micropub_response_date = date('Y-m-d H:i:s'); // Check the response and look for a "Location" header containing the URL if ($r['response'] && preg_match('/Location: (.+)/', $r['response'], $match)) { $r['location'] = trim($match[1]); $user->micropub_success = 1; } else { $r['location'] = false; } $user->save(); return $r; }
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); }
$user->save(); $app->response()->body(json_encode(array('result' => 'ok'))); } }); $app->post('/micropub/test', function () use($app) { if ($user = require_login($app)) { $params = $app->request()->params(); if ($user->send_category_as_array != 1) { if (is_array($params['category'])) { $params['category'] = implode(',', $params['category']); } } // Download the file to a temp folder $filename = download_file($params['url']); // Now send to the micropub endpoint $r = micropub_post($user->micropub_endpoint, $user->micropub_access_token, $params, $filename); $response = $r['response']; #unlink($filename); $user->last_micropub_response = json_encode($r); // Check the response and look for a "Location" header containing the URL if ($response && preg_match('/Location: (.+)/', $response, $match)) { $location = $match[1]; $user->micropub_success = 1; } else { $location = false; } $user->save(); $app->response()->body(json_encode(array('response' => htmlspecialchars($response), 'location' => $location, 'error' => $r['error'], 'curlinfo' => $r['curlinfo'], 'filename' => $filename))); } }); $app->post('/instagram/callback', function () use($app) {
if ($response && preg_match('/Location: (.+)/', $response, $match)) { $location = $match[1]; $user->micropub_success = 1; $user->save(); } else { $location = false; } json_response($app, ['location' => $location, 'status' => $r['status'], 'response' => trim($response), 'error' => $r['error']]); } }); ///// $app->post('/micropub/post', function () use($app) { if ($user = require_login($app)) { $params = $app->request()->params(); // Now send to the micropub endpoint $post_id = $params['post_id']; unset($params['post_id']); $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token); $response = $r['response']; // Check the response and look for a "Location" header containing the URL if ($response && preg_match('/Location: (.+)/', $response, $match)) { $location = $match[1]; $user->micropub_success = 1; } else { $location = false; } $user->save(); $app->response()['Content-Type'] = 'application/json'; $app->response()->body(json_encode(array('response' => htmlspecialchars(trim($response)), 'location' => $location, 'error' => $r['error'], 'curlinfo' => $r['curlinfo'], 'post_id' => $post_id))); } });