Skip to content

Commit

Permalink
Merge pull request #5 from pixelant/development_new
Browse files Browse the repository at this point in the history
[TASK] implement check if feed was deleted
  • Loading branch information
MattiasNilsson authored Aug 3, 2017
2 parents a740d01 + 68e38b8 commit 053e1ba
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Classes/Controller/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,4 @@ public static function translate($label = '')
{
return LocalizationUtility::translate($label, 'pxa_social_feed');
}
}
}
2 changes: 1 addition & 1 deletion Classes/Task/CleanUpTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function execute()
*/
public function getAdditionalInformation()
{
return 'Delete entries older than ' . $this->getDays() . ' days.';
return 'Delete entries older than ' . $this->getDays() . ' days or deleted at social media feed directly.';
}

/**
Expand Down
43 changes: 33 additions & 10 deletions Classes/Utility/Api/TwitterApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class TwitterApi
/**
* path to get twitter feed
*/
const API_URL = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
const API_FETCH_URL = 'https://api.twitter.com/1.1/';

/**
* consumer key
Expand Down Expand Up @@ -105,9 +105,30 @@ public function __construct(
* perform request to api
*
* @return array
*/
public function performFetchRequest()
{
return $this->performRequest(self::API_FETCH_URL . 'statuses/user_timeline.json');
}

/**
* perform request to api
*
* @return array
*/
public function performStatusesLookup()
{
return $this->performRequest(self::API_FETCH_URL . 'statuses/lookup.json');
}

/**
* perform request to api
*
* @param string $url
* @return array
* @throws \Exception
*/
public function performRequest()
protected function performRequest($url)
{
if (empty($this->getFields)) {
throw new \Exception('Get fields could not be empty', 1463139019);
Expand All @@ -117,11 +138,11 @@ public function performRequest()
/** @var RequestUtility $requestUtility */
$requestUtility = GeneralUtility::makeInstance(
RequestUtility::class,
self::API_URL,
$url,
RequestUtility::METHOD_GET
);
$requestUtility->setGetParameters($this->getGetFields());
$requestUtility->setHeaders(['Authorization' => $this->getAuthHeader()]);
$requestUtility->setHeaders(['Authorization' => $this->getAuthHeader($url)]);

$response = $requestUtility->send();
if (!empty($response)) {
Expand All @@ -134,9 +155,10 @@ public function performRequest()
/**
* Get Authorization header
*
* @param string $url
* @return string
*/
protected function getAuthHeader()
protected function getAuthHeader($url)
{
$oauth = [
'oauth_consumer_key' => $this->consumerKey,
Expand All @@ -147,7 +169,7 @@ protected function getAuthHeader()
'oauth_version' => '1.0'
];

$sigBase = $this->buildSigBase(array_merge($oauth, $this->getGetFields()));
$sigBase = $this->buildSigBase(array_merge($oauth, $this->getGetFields()), $url);
$sigKey = rawurlencode($this->consumerSecret) . '&' . rawurlencode($this->oauthAccessTokenSecret);

$oauth['oauth_signature'] = base64_encode(hash_hmac('sha1', $sigBase, $sigKey, true));
Expand All @@ -168,19 +190,20 @@ protected function getAuthHeader()
* Private method to generate the base string
*
* @param array $oauth
*
* @param string $url
* @return string Built base string
*/
private function buildSigBase($oauth)
private function buildSigBase($oauth, $url)
{
ksort($oauth);
$urlParts = [];

foreach ($oauth as $key => $value) {
$urlParts[] = $key . '=' . $value;
$urlParts[] = $key . '=' . rawurlencode($value);
}

return 'GET' . '&' . rawurlencode(self::API_URL) . '&' . rawurlencode(implode('&', $urlParts));
return 'GET' . '&' . rawurlencode($url) . '&'
. rawurlencode(implode('&', $urlParts));
}

/**
Expand Down
156 changes: 153 additions & 3 deletions Classes/Utility/Task/CleanUpTaskUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

use Pixelant\PxaSocialFeed\Domain\Model\Configuration;
use Pixelant\PxaSocialFeed\Domain\Model\Token;
use Pixelant\PxaSocialFeed\Domain\Repository\ConfigurationRepository;
use Pixelant\PxaSocialFeed\Utility\Api\TwitterApi;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
* Class CleanUpTaskUtility
Expand All @@ -50,12 +56,20 @@ class CleanUpTaskUtility
*/
protected $dbConnection;

/**
* objectManager
*
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
protected $objectManager;

/**
* initialize
*/
public function __construct()
{
$this->dbConnection = $GLOBALS['TYPO3_DB'];
$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
}

/**
Expand All @@ -68,6 +82,7 @@ public function run($days)
{
$obsoleteEntries = $this->getObsoleteEntries($days);
$this->deleteObsoleteEntries($obsoleteEntries);
$this->removeDeletedFeeds();

return true;
}
Expand All @@ -89,14 +104,14 @@ protected function getObsoleteEntries($days)
'crdate ASC'
);

return $this->groupByConfigrations($records);
return $this->groupByConfigurations($records);
}

/**
* @param array $records
* @return array
*/
protected function groupByConfigrations($records)
protected function groupByConfigurations($records)
{
$recordsByConfiguration = [];

Expand Down Expand Up @@ -164,9 +179,144 @@ protected function deleteObsoleteEntries($obsoleteEntries)
$uids .= ',' . $obsoleteEntry['uid'];
}

$where = '';
if (!empty($uids)) {
$where .= 'uid IN (' . ltrim($uids, ',') . ') OR ';
}
$where .= 'deleted=1';

$this->dbConnection->exec_DELETEquery(
self::TABLE_FEED,
'uid IN (' . ltrim($uids, ',') . ') OR deleted=1'
$where
);
}

/**
* Remove feed entries which were not found
*
* @return void
*/
private function removeDeletedFeeds()
{
$configurations = $this->objectManager->get(ConfigurationRepository::class)->findAll();
$feedsToRemove = [];

/** @var Configuration $configuration */
foreach ($configurations as $configuration) {
$feeds = $this->dbConnection->exec_SELECTgetRows(
'uid,external_identifier',
self::TABLE_FEED,
'configuration=' . $configuration->getUid()
);

if ($configuration->getToken()->getSocialType() !== Token::TWITTER) {
foreach ($feeds as $feed) {
if ($this->isFeedDeleted($feed['external_identifier'], $configuration)) {
$feedsToRemove[] = $feed;
}
}
} else {
$this->checkTwitterFeeds($configuration, $feeds, $feedsToRemove);
}
}

$this->deleteObsoleteEntries($feedsToRemove);
}

/**
* @param string $externalIdentifier
* @param Configuration $configuration
* @return boolean
*/
private function isFeedDeleted($externalIdentifier, Configuration $configuration)
{
switch ($configuration->getToken()->getSocialType()) {
case Token::FACEBOOK:
$url = sprintf(
ImportTaskUtility::FACEBOOK_API_URL . '%s?access_token=%s|%s',
$externalIdentifier,
$configuration->getToken()->getCredential('appId'),
$configuration->getToken()->getCredential('appSecret')
);

$data = json_decode(GeneralUtility::getUrl($url), true);

return !(isset($data['id']) && $data['id'] == $externalIdentifier);
break;
case Token::INSTAGRAM_OAUTH2:
$url = sprintf(
ImportTaskUtility::INSTAGRAM_API_URL . 'media/%s?access_token=%s',
$externalIdentifier,
$configuration->getToken()->getCredential('accessToken')
);

$data = json_decode(GeneralUtility::getUrl($url), true);

return !(isset($data['data']['id']) && $data['data']['id'] == $externalIdentifier);
break;
default:
throw new \UnexpectedValueException('Such social type is not valid', 1466690851);
break;
}
}

/**
* <<<<<<< HEAD
* @param Configuration $configuration
* @param array $twitterFeeds
* @param array &$feedsToRemove
*/
private function checkTwitterFeeds(Configuration $configuration, $twitterFeeds, &$feedsToRemove)
{
if (!empty($twitterFeeds)) {
do {
// twitter limit
$feedsList = array_slice($twitterFeeds, 0, 99);
$twitterFeeds = array_slice($twitterFeeds, 99);

$fields = [
'id' => $this->getListOfArrayField('external_identifier', $feedsList),
'include_entities' => 'false',
'trim_user' => 1,
'map' => 'false'
];

/** @var TwitterApi $twitterApi */
$twitterApi = GeneralUtility::makeInstance(
TwitterApi::class,
$configuration->getToken()->getCredential('consumerKey'),
$configuration->getToken()->getCredential('consumerSecret'),
$configuration->getToken()->getCredential('accessToken'),
$configuration->getToken()->getCredential('accessTokenSecret')
);

$data = $twitterApi->setGetFields($fields)->performStatusesLookup();

$availableItems = $this->getListOfArrayField('id_str', $data);

foreach ($feedsList as $feedListItem) {
if (!GeneralUtility::inList($availableItems, $feedListItem['external_identifier'])) {
$feedsToRemove[] = $feedListItem;
}
}
} while (count($twitterFeeds) > 0);
}
}

/**
* Get list of array field
*
* @param string $field
* @param array $listArray
* @return string
*/
private function getListOfArrayField($field, $listArray = [])
{
$list = [];
foreach ($listArray as $item) {
$list[] = $item[$field];
}

return implode(',', $list);
}
}
11 changes: 7 additions & 4 deletions Classes/Utility/Task/ImportTaskUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@

class ImportTaskUtility
{
const FACEBOOK_API_URL = 'https://graph.facebook.com/v2.9/';

const INSTAGRAM_API_URL = 'https://api.instagram.com/v1/';

/**
* objectManager
Expand Down Expand Up @@ -89,7 +92,7 @@ public function run($configurationsUids)
//getting data array from facebook graph api json result
// @codingStandardsIgnoreStart
$url = sprintf(
'https://graph.facebook.com/v2.6/%s/posts/?fields=likes.summary(true).limit(0),message,attachments,created_time,updated_time&limit=%d&access_token=%s|%s',
self::FACEBOOK_API_URL . '%s/posts/?fields=likes.summary(true).limit(0),message,attachments,created_time,updated_time&limit=%d&access_token=%s|%s',
$configuration->getSocialId(),
$configuration->getFeedsLimit(),
$configuration->getToken()->getCredential('appId'),
Expand All @@ -112,7 +115,7 @@ public function run($configurationsUids)
case Token::INSTAGRAM_OAUTH2:
// getting data array from instagram api json result
//predefine a format of request string;
$urlFormat = 'https://api.instagram.com/v1/%s/%s/media/recent/?access_token=%s&count=%d';
$urlFormat = self::INSTAGRAM_API_URL . '%s/%s/media/recent/?access_token=%s&count=%d';

// hashtag used in configuration (leading '#' symbol): preparing values for 'tag' API call
if (GeneralUtility::isFirstPartOfStr($configuration->getSocialId(), '#')) {
Expand Down Expand Up @@ -153,7 +156,7 @@ public function run($configurationsUids)
'include_rts' => 1
];

/** @var \Pixelant\PxaSocialFeed\Utility\Api\TwitterApi $twitterApi */
/** @var TwitterApi $twitterApi */
$twitterApi = GeneralUtility::makeInstance(
TwitterApi::class,
$configuration->getToken()->getCredential('consumerKey'),
Expand All @@ -162,7 +165,7 @@ public function run($configurationsUids)
$configuration->getToken()->getCredential('accessTokenSecret')
);

$data = $twitterApi->setGetFields($fields)->performRequest();
$data = $twitterApi->setGetFields($fields)->performFetchRequest();

if (is_array($data)) {
$this->saveTwitterFeed($data, $configuration);
Expand Down
2 changes: 1 addition & 1 deletion ext_emconf.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
'uploadfolder' => '0',
'createDirs' => '',
'clearCacheOnLoad' => 0,
'version' => '1.4.0',
'version' => '1.5.0',
'constraints' => [
'depends' => [
'typo3' => '7.6.0-8.9.99',
Expand Down

0 comments on commit 053e1ba

Please sign in to comment.