Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
Issue #2982332 by Insasse, volkerk, webflo, daniel.bosen: New Riddle API
Browse files Browse the repository at this point in the history
* fix for new api

* fix test
  • Loading branch information
ol0lll authored and dbosen committed Jul 19, 2018
1 parent 8b43f97 commit da41da9
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 157 deletions.
2 changes: 1 addition & 1 deletion config/install/riddle_marketplace.settings.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
riddle_marketplace:
token: ''
api_url: 'https://www.riddle.com/apiv3/item/token/%%TOKEN%%?client=d8'
api_url: 'https://www.riddle.com/api/creators/riddle/get-list?token=%%TOKEN%%'
url: 'https://www.riddle.com/a/%%RIDDLE_UID%%'
empty_title_prefix: 'Riddle '
3 changes: 0 additions & 3 deletions config/schema/riddle_marketplace.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,3 @@ riddle_marketplace.settings:
empty_title_prefix:
type: string
label: 'Riddle prefix for title'
fetch_unpublished:
type: integer
label: 'Fetch unpublished riddles'
92 changes: 68 additions & 24 deletions modules/media_riddle_marketplace/src/Plugin/media/Source/Riddle.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
use Drupal\Core\File\FileSystem;
use Drupal\media\MediaSourceBase;
use Drupal\media\MediaInterface;
use Drupal\riddle_marketplace\RiddleFeedServiceInterface;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesser;

/**
* Provides media type plugin for Riddle.
Expand All @@ -31,6 +35,20 @@ class Riddle extends MediaSourceBase {
*/
protected $configFactory;

/**
* The file system service.
*
* @var \Drupal\Core\File\FileSystem
*/
protected $fileSystem;

/**
* The HTTP client to fetch the feed data with.
*
* @var \GuzzleHttp\Client
*/
protected $httpClient;

/**
* Riddle feed service.
*
Expand All @@ -55,11 +73,17 @@ class Riddle extends MediaSourceBase {
* The field type plugin manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\File\FileSystem $file_system
* The file system service.
* @param \GuzzleHttp\Client $http_client
* The http client service.
* @param \Drupal\riddle_marketplace\RiddleFeedServiceInterface $riddleFeed
* Riddle feed service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, RiddleFeedServiceInterface $riddleFeed) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, FileSystem $file_system, Client $http_client, RiddleFeedServiceInterface $riddleFeed) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->fileSystem = $file_system;
$this->httpClient = $http_client;
$this->riddleFeed = $riddleFeed;
}

Expand All @@ -75,6 +99,8 @@ public static function create(ContainerInterface $container, array $configuratio
$container->get('entity_field.manager'),
$container->get('plugin.manager.field.field_type'),
$container->get('config.factory'),
$container->get('file_system'),
$container->get('http_client'),
$container->get('riddle_marketplace.feed')
);
}
Expand Down Expand Up @@ -116,7 +142,7 @@ public function getMetadata(MediaInterface $media, $name) {

$riddleFeed = $this->riddleFeed->getFeed();
$riddle = array_filter($riddleFeed, function ($entry) use ($code) {
return $entry['uid'] == $code;
return $entry['id'] == $code;
});

$riddle = current($riddle);
Expand Down Expand Up @@ -144,37 +170,55 @@ public function getMetadata(MediaInterface $media, $name) {
return NULL;

case 'thumbnail_local':
case 'thumbnail_local_uri':
case 'thumbnail_uri':
$local_uri = $this->getMetadata($media, 'thumbnail_local_uri');
if ($local_uri) {
if (file_exists($local_uri)) {
return $local_uri;
if (isset($riddle['image'])) {
$directory = $this->configFactory->get('media_riddle_marketplace.settings')->get('local_images');
// Try to load from local filesystem.
$absolute_uri = current(glob($this->fileSystem->realpath($directory . '/' . $code . ".*{jpg,jpeg,png,gif}"), GLOB_BRACE));
if ($absolute_uri) {
return $directory . '/' . $this->fileSystem->basename($absolute_uri);
}
else {

$directory = dirname($local_uri);
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);

$image_url = $this->getMetadata($media, 'thumbnail');

return file_unmanaged_save_data(file_get_contents($image_url), $local_uri, FILE_EXISTS_REPLACE);
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
// Get image from remote and save locally.
try {
$response = $this->httpClient->get($riddle['image']);
$format = $this->guessExtension($response->getHeaderLine('Content-Type'));
if (in_array($format, ['jpg', 'jpeg', 'png', 'gif'])) {
return file_unmanaged_save_data($response->getBody(), $directory . '/' . $code . "." . $format, FILE_EXISTS_REPLACE);
}
}
catch (ClientException $e) {
// Do nothing.
}
}
return NULL;

case 'thumbnail_local_uri':
if (isset($riddle['image'])) {
return $this->configFactory->get('media_riddle_marketplace.settings')
->get('local_images') . '/' . $code . '.' . pathinfo(parse_url($riddle['image'], PHP_URL_PATH), PATHINFO_EXTENSION);
}
return parent::getMetadata($media, 'thumbnail_uri');

default:
return parent::getMetadata($media, $name);
}
}

return NULL;
}

/**
* Returns the extension based on the mime type.
*
* If the mime type is unknown, returns null.
*
* This method uses the mime type as guessed by getMimeType()
* to guess the file extension.
*
* @param string $mime_type
* The mime type to guess extension for.
*
* @return string|null
* The guessed extension or null if it cannot be guessed.
*
* @see ExtensionGuesser
* @see getMimeType()
*/
public function guessExtension($mime_type) {
$guesser = ExtensionGuesser::getInstance();
return $guesser->guess($mime_type);
}

}
5 changes: 4 additions & 1 deletion modules/media_riddle_marketplace/src/RiddleMediaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,11 @@ public function createMediaEntities() {
public function getNewRiddles() {

$feed = $this->feedService->getFeed();
$riddle_feed_ids = array_column($feed, 'id');

$riddle_feed_ids = array_column($feed, 'uid');
if (empty($riddle_feed_ids)) {
return [];
}

/** @var \Drupal\media\MediaTypeInterface[] $riddleBundles */
$riddleBundles = $this->entityTypeManager->getStorage('media_type')
Expand Down
20 changes: 20 additions & 0 deletions riddle_marketplace.post_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/**
* @file
* Post update functions.
*/

/**
* Set new api url if needed.
*/
function riddle_marketplace_post_update_api_url() {
$module_settings = Drupal::configFactory()->getEditable('riddle_marketplace.settings');
$api_url = $module_settings->get('riddle_marketplace.api_url');
$new_api_url = 'https://www.riddle.com/api/creators/riddle/get-list?token=%%TOKEN%%';

if ($api_url != $new_api_url) {
$module_settings->set('riddle_marketplace.api_url', $new_api_url);
$module_settings->save();
}
}
14 changes: 5 additions & 9 deletions scripts/travis/test-source-code.sh
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
#!/usr/bin/env bash
# remove xdebug to make php execute faster
phpenv config-rm xdebug.ini

# globally require drupal coder for code tests
composer global require drupal/coder
composer global require "symfony/yaml:^3.4" "drupal/coder"

# run phpcs
phpcs --config-set installed_paths ~/.composer/vendor/drupal/coder/coder_sniffer
phpcs --standard=Drupal --report=summary -p .
phpcs --standard=DrupalPractice --report=summary -p .
phpcs --standard=Drupal --ignore=README.md -p .
phpcs --standard=DrupalPractice -p .

# JS ESLint checking
mv ~/.nvm ~/.nvm-backup
git clone https://github.com/creationix/nvm.git ~/.nvm
(cd ~/.nvm && git checkout `git describe --abbrev=0 --tags`)
set -x
source ~/.nvm/nvm.sh
set +x
nvm install 4
nvm install 6
npm install -g eslint
eslint .
rm -rf ~/.nvm
mv ~/.nvm-backup ~/.nvm
12 changes: 1 addition & 11 deletions src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ public function buildForm(array $form, FormStateInterface $form_state) {
[':riddle' => 'http://www.riddle.com']),
'#default_value' => $settings->get('riddle_marketplace.token'),
];

$form['fetch_unpublished'] = [
'#type' => 'checkbox',
'#title' => $this->t('Fetch unpublished riddles'),
'#default_value' => $settings->get('riddle_marketplace.fetch_unpublished'),
'#description' => $this->t('If checked, all riddles will be fetched from the API, not only published ones'),
];

return parent::buildForm($form, $form_state);
}

Expand All @@ -48,9 +40,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$values = $form_state->getValues();
$config = $this->configFactory()->getEditable('riddle_marketplace.settings');
$config->set('riddle_marketplace.token', $values['token'])
->set('riddle_marketplace.fetch_unpublished', $values['fetch_unpublished'])
->save();
$config->set('riddle_marketplace.token', $values['token'])->save();
}

/**
Expand Down
79 changes: 11 additions & 68 deletions src/RiddleFeedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ class RiddleFeedService implements RiddleFeedServiceInterface {
*/
private $emptyTitlePrefix;

/**
* Should unpublished riddles also fetched from api.
*
* @var int
*/
private $fetchUnpublished;

/**
* Riddle Feed Service.
*
Expand All @@ -77,7 +70,6 @@ public function __construct(CacheBackendInterface $cacheService, ConfigFactoryIn

// Set Empty Title Prefix.
$this->emptyTitlePrefix = $this->moduleSettings->get('riddle_marketplace.empty_title_prefix');
$this->fetchUnpublished = $this->moduleSettings->get('riddle_marketplace.fetch_unpublished');
}

/**
Expand Down Expand Up @@ -129,9 +121,6 @@ private function getApiUrl() {
[$token],
$this->moduleSettings->get('riddle_marketplace.api_url')
);
if (!$this->fetchUnpublished) {
$url .= '&status=published';
}
return $url;
}
throw new NoApiKeyException();
Expand Down Expand Up @@ -169,65 +158,26 @@ private function fetchRiddleResponse() {
private function processRiddleResponse($riddleResponse) {
$feed = [];

if (!empty($riddleResponse) && is_array($riddleResponse)) {
foreach ($riddleResponse as $riddleEntry) {
if (!empty($riddleResponse) && is_array($riddleResponse['items'])) {
foreach ($riddleResponse['items'] as $riddleEntry) {
// Skip invalid riddle feed entries.
if (!$this->isValidRiddleFeedEntry($riddleEntry)) {
continue;
}

$feed[$riddleEntry['uid']] = [
$feed[$riddleEntry['id']] = [
'title' => $this->getRiddleTitle($riddleEntry),
'uid' => $riddleEntry['uid'],
'status' => ($riddleEntry['status'] == 'published') ? 1 : 0,
'image' => $this->getImage($riddleEntry),
'id' => $riddleEntry['id'],
// All riddles in response are published atm.
'status' => 1,
'image' => $riddleEntry['thumb'],
];
}
}

return $feed;
}

/**
* Return an image url.
*
* @param array|null $riddleEntry
* Single Riddle Feed Entry.
*
* @return string
* A full url.
*/
private function getImage($riddleEntry) {

$image = $data = NULL;
if (!empty($riddleEntry['data']['image']['standard'])) {
$data = $riddleEntry['data'];
}
elseif (!empty($riddleEntry['draftData']['image']['standard'])) {
$data = $riddleEntry['draftData'];
}

if ($data) {
$urlParts = parse_url($data['image']['standard']);
$image = $urlParts['path'];

$pathinfo = pathinfo($urlParts['path']);

if (empty($urlParts['host'])) {
$image = 'https://www.riddle.com' . $image;
}
else {
$image = $urlParts['scheme'] . '://' . $urlParts['host'] . $image;
}

if (!empty($data['image']['format']) && empty($pathinfo['extension'])) {
$image = $image . '.' . $data['image']['format'];
}
}

return $image;
}

/**
* Validation Riddle Feed Entry.
*
Expand All @@ -239,9 +189,7 @@ private function getImage($riddleEntry) {
*/
private function isValidRiddleFeedEntry($riddleEntry) {
if (
empty($riddleEntry) || !is_array($riddleEntry)
|| ((empty($riddleEntry['data']) || !is_array($riddleEntry['data'])) && (empty($riddleEntry['draftData']) || !is_array($riddleEntry['draftData'])))
|| empty($riddleEntry['uid'])
empty($riddleEntry) || !is_array($riddleEntry) || empty($riddleEntry['id'])
) {
return FALSE;
}
Expand All @@ -262,15 +210,10 @@ private function isValidRiddleFeedEntry($riddleEntry) {
* Riddle element title.
*/
private function getRiddleTitle(array $riddleEntry) {
if (!empty($riddleEntry['data']['title'])) {
return $riddleEntry['data']['title'];
if (!empty($riddleEntry['title'])) {
return $riddleEntry['title'];
}

if (!empty($riddleEntry['draftData']['title'])) {
return $riddleEntry['draftData']['title'];
}

return $this->emptyTitlePrefix . $riddleEntry['uid'];
return $this->emptyTitlePrefix . $riddleEntry['id'];
}

/**
Expand Down
Loading

0 comments on commit da41da9

Please sign in to comment.