social_content_instagram.module in Social Content 7
Same filename and directory in other branches
Social Content: Instagram module.
File
modules/instagram/social_content_instagram.moduleView source
<?php
/**
* @file
* Social Content: Instagram module.
*/
define('SOCIAL_CONTENT_INSTAGRAM_ACCESS_TOKEN_URL', 'https://api.instagram.com/oauth/access_token');
define('SOCIAL_CONTENT_INSTAGRAM_AUTHORIZE_URL', 'https://api.instagram.com/oauth/authorize');
define('SOCIAL_CONTENT_INSTAGRAM_CLIENT_DETAILS_URL', 'http://instagram.com/developer/clients/manage/');
/**
* Implements hook_menu().
*/
function social_content_instagram_menu() {
$items['admin/config/services/social-content/%social_content_type/access_token_generator'] = array(
'title' > 'Instagram token generator',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'social_content_instagram_token_generator_form',
4,
),
'access arguments' => array(
'administer social content',
),
'file' => 'social_content_instagram.admin.inc',
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function social_content_instagram_social_content_info() {
$info = array();
$shared = array(
'content_type' => 'instagram',
'settings_form' => 'social_content_instagram_settings_form',
'external_id_field_mapping' => array(
'id' => 'field_instagram_external_id',
),
'post_callback' => 'social_content_instagram_post_callback',
);
$additional_settings_shared = array(
'access_token' => '',
'api_url' => 'https://api.instagram.com/v1',
'min_resolution' => '',
'client_id' => '',
'client_secret' => '',
);
$info['instagram_account'] = $shared + array(
'title' => t('Instagram (account)'),
'settings_form' => 'social_content_instagram_settings_form',
'data_callback' => 'social_content_instagram_account_data_callback',
'additional_settings' => $additional_settings_shared + array(
'user_id' => '',
),
);
$info['instagram_hashtag'] = $shared + array(
'title' => t('Instagram (hashtag)'),
'settings_form' => 'social_content_instagram_settings_form',
'data_callback' => 'social_content_instagram_hashtag_data_callback',
'additional_settings' => $additional_settings_shared + array(
'hashtags' => '',
),
);
return $info;
}
function social_content_instagram_settings_form(&$form, $social_content_type, $settings) {
$token_generator_link = l(t('Instagram access token generator'), 'admin/config/services/social-content/' . $social_content_type['name'] . '/access_token_generator');
$form['access_token'] = array(
'#type' => 'textfield',
'#title' => t('Access Token'),
'#default_value' => $settings['access_token'],
'#required' => TRUE,
'#description' => t('You can use the !token_generator_link to help you obtain this.', array(
'!token_generator_link' => $token_generator_link,
)),
);
if ($social_content_type['name'] == 'instagram_account') {
$example_url = 'https://api.instagram.com/v1/users/search?q=username={YOUR_USERNAME}&access_token=';
if (empty($settings['access_token'])) {
$example_url .= '{GENERATED_ACCESS_TOKEN}';
}
else {
$example_url .= $settings['access_token'];
}
$form['user_id'] = array(
'#type' => 'textfield',
'#title' => t('Instagram User ID'),
'#description' => t('Visit !url to obtain one.', array(
'!url' => $example_url,
)),
'#default_value' => $settings['user_id'],
);
}
elseif ($social_content_type['name'] == 'instagram_hashtag') {
$form['hashtags'] = array(
'#type' => 'textfield',
'#title' => t('Hashtags'),
'#description' => t('Without the leading #. You can define multiple hashtags separated by space.'),
'#default_value' => $settings['hashtags'],
);
}
$form['api_url'] = array(
'#type' => 'textfield',
'#title' => t('API URL'),
'#description' => t('Do not include ending slash'),
'#default_value' => $settings['api_url'],
);
$form['min_resolution'] = array(
'#type' => 'textfield',
'#title' => t('Minimum image resolution'),
'#description' => t('Only posts that have images that meet the minimum image resolution will be imported. WIDTHxHEIGHT'),
'#default_value' => $settings['min_resolution'],
);
}
function social_content_instagram_account_data_callback($settings, $last_id = NULL) {
if (!empty($settings['user_id'])) {
$params = array(
'access_token' => $settings['access_token'],
);
$params['user_id'] = $settings['user_id'];
if ($last_id) {
$params['min_id'] = $last_id;
}
$url = url($settings['api_url'] . '/users/' . $settings['user_id'] . '/media/recent/', array(
'query' => $params,
'external' => TRUE,
));
$result = drupal_http_request($url);
if ($result->code == 200) {
$posts = json_decode($result->data);
return $posts->data;
}
}
return FALSE;
}
function social_content_instagram_hashtag_data_callback($settings, $last_id = NULL) {
if (!empty($settings['hashtags'])) {
$params = array(
'access_token' => $settings['access_token'],
);
if (isset($settings['limit']) && $settings['limit'] > 0) {
$params['count'] = $settings['limit'];
}
if ($last_id) {
$params['min_tag_id'] = $last_id;
}
$posts = array();
$hashtags = explode(' ', $settings['hashtags']);
// Explode hashtags by space and request images for each hashtag.
foreach ($hashtags as $hashtag) {
$url = $settings['api_url'] . '/tags/' . trim($hashtag) . '/media/recent/?' . drupal_http_build_query($params);
$result = drupal_http_request($url);
if ($result->code == 200) {
$result_data = json_decode($result->data);
$posts += $result_data->data;
}
else {
watchdog('social_content_instagram', 'Error fetching feed, data: !data', array(
'!data' => $result->data,
), WATCHDOG_WARNING);
return FALSE;
}
}
return $posts;
}
return FALSE;
}
function social_content_instagram_post_callback(&$wrapper, $post, $external_id, $settings) {
$wrapper->created
->set($post->created_time);
$body_text = isset($post->caption) ? $post->caption->text : '';
$body_text = social_content_validate_text($body_text);
$wrapper->body
->set(array(
'value' => $body_text,
'format' => 'filtered_html',
));
if (!isset($post->images->standard_resolution->url)) {
watchdog('social_content_instagram', 'Missing image url on Instagram Import', WATCHDOG_WARNING);
return FALSE;
}
$image_url = $post->images->standard_resolution->url;
$image_field_name = 'field_instagram_picture';
$image_file = social_content_get_image_file($image_url, $image_field_name, $settings['min_resolution']);
if (!$image_file) {
return FALSE;
}
$wrapper->{$image_field_name}
->set($image_file);
$wrapper->field_instagram_link
->set(array(
'url' => $post->link,
));
return TRUE;
}
Functions
Constants
Name![]() |
Description |
---|---|
SOCIAL_CONTENT_INSTAGRAM_ACCESS_TOKEN_URL | @file Social Content: Instagram module. |
SOCIAL_CONTENT_INSTAGRAM_AUTHORIZE_URL | |
SOCIAL_CONTENT_INSTAGRAM_CLIENT_DETAILS_URL |