Created
December 18, 2013 22:40
-
-
Save chadmandoo/8031117 to your computer and use it in GitHub Desktop.
Drupal Bing news
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Code for the Bing News feature. | |
*/ | |
include_once 'bing_news.features.inc'; | |
define ('BING_NEWS_URL', 'https://api.datamarket.azure.com/Bing/Search/v1/Composite?Sources=%27news%27&Query=%27@bing_news_query%27&NewsSortBy=%27Date%27&@bing_skip&$format=json'); | |
/** | |
* Implements hook_menu(); | |
* @return mixed | |
*/ | |
function bing_news_menu() { | |
$items['admin/config/system/bing_news'] = array( | |
'title' => t('ServiceSource News settings'), | |
'description' => t('Configure connection and filtering options to the Bing Search API.'), | |
'page callback' => 'drupal_get_form', | |
'page arguments' => array('bing_news_settings'), | |
'access arguments' => array('administer bing news'), | |
); | |
return $items; | |
} | |
/** | |
* Implements hook_permission(). | |
*/ | |
function bing_news_permission() | |
{ | |
return array( | |
'administer bing news' => array( | |
'title' => t('Administer Bing News'), | |
'description' => t('Can modify the settings for the Bing News feed importer.') | |
) | |
); | |
} | |
/** | |
* Provides the settings for the Bing News module | |
* @param $form_state | |
* @return mixed | |
*/ | |
function bing_news_settings($form_state) { | |
$form = array(); | |
$form['bing_news_enabled'] = array( | |
'#type' => 'checkbox', | |
'#title' => 'Enable the news feed import', | |
'#default_value' => variable_get('bing_news_enabled', 0), | |
); | |
$form['bing_news_primary_account_key'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Primary Account Key'), | |
'#description' => t('Found on your !link', array('!link' => l('Windows Azure Marketplace account page', 'https://datamarket.azure.com/account'))), | |
'#default_value' => variable_get('bing_news_primary_account_key', ''), | |
); | |
$form['bing_news_search_query'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Bing Search Query'), | |
'#description' => t('e.g. ServiceSource'), | |
'#default_value' => variable_get('bing_news_search_query', ''), | |
); | |
$form['bing_news_max_age'] = array( | |
'#type' => 'select', | |
'#title' => t('Age of news article'), | |
'#description' => t('Maximum age of the news article to be considered for importing.'), | |
'#options' => array( | |
'1 day' => t('1 day'), | |
'1 week' => t('1 week'), | |
'1 month' => t('1 month'), | |
'1 year' => t('1 year') | |
), | |
'#default_value' => variable_get('bing_news_max_age', '1 day'), | |
); | |
return system_settings_form($form); | |
} | |
function bing_news_import() { | |
// If the import is disabled, bail out. | |
if (!variable_get('bing_news_enabled', 0)) { | |
return ; | |
} | |
// Retrieve the primary account key. | |
if ($accountKey = variable_get('bing_news_primary_account_key', false)) { | |
// Prepare the URL | |
$prepared_url = str_replace('@bing_news_query', variable_get('bing_news_search_query', ''), BING_NEWS_URL); | |
$prepared_url = str_replace('@bing_skip', '$skip=0', $prepared_url); | |
// Set the stream context with our authentication information | |
$context = stream_context_create(array( | |
'http' => array( | |
'request_fulluri' => true, | |
'header' => "Authorization: Basic " . base64_encode($accountKey . ":" . $accountKey) | |
) | |
)); | |
$stop = true; | |
$skip_link = 0; | |
$old_skip = 15; | |
set_time_limit(600); | |
do{ | |
$old_skip = $skip_link-15; | |
$prepared_url = str_replace('$skip='.$old_skip, '$skip='.$skip_link, $prepared_url); | |
$skip_link+= 15; | |
if ($result = file_get_contents($prepared_url, 0, $context)) { | |
// Decode the json | |
$result_obj = json_decode($result); | |
// Make sure we've got news results | |
if (isset($result_obj->d, $result_obj->d->results, $result_obj->d->results[0], $result_obj->d->results[0]->News)) { | |
// Get the maximum news item age variable | |
$age = variable_get('bing_news_max_age', '1 day'); | |
foreach ($result_obj->d->results[0]->News as $key => $news_item) { | |
// Make sure this item is within our import time window | |
$counts = count($result_obj->d->results[0]->News); | |
$news_item_age = strtotime($news_item->Date); | |
$max_age = strtotime('today - '.$age); | |
if ($news_item_age > $max_age) { | |
// Check the news id to see if we've imported this article previously. | |
$query = new EntityFieldQuery(); | |
$query->entityCondition('entity_type', 'node') | |
->entityCondition('bundle', 'news_item') | |
->propertyCondition('title', $news_item->Title) | |
->range(0,1); | |
$result = $query->execute(); | |
if (isset($result['node']) && $result['node']) { | |
// We've got this node imported already. | |
// TODO update the news information? Probably not... | |
// Move along for now. | |
}else{ | |
// This node does not exist in the database. Create it. | |
$node = entity_create('node', array('type' => 'news_item')); | |
$entity = entity_metadata_wrapper('node',$node); | |
//Set properties | |
$entity->author = 1; // Author | |
$entity->title->set($news_item->Title); // Title | |
$entity->field_news_description->set($news_item->Description); // News Description | |
$entity->field_news_source->set($news_item->Source); // News Source | |
$entity->field_news_date->set($news_item_age); // News Date | |
$entity->field_news_url->url->set($news_item->Url); // News URL | |
$entity->field_news_url->title->set("View"); // News URL | |
$entity->status = 0; // Unpublish for future curation. | |
//Save | |
$entity->save(); //Save new instance | |
} | |
} | |
else | |
{ | |
$stop = false; | |
} | |
} | |
} | |
} | |
}while($stop); | |
} | |
} | |
/** | |
* Implements hook_cron() | |
*/ | |
function bing_news_cron() { | |
// Trigger the news import. | |
bing_news_import(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment