Compare commits

..

No commits in common. "41d8c16d8422177fc7e542e1be620d4fc153a743" and "8cee39a6df9edfcf72a0676945434515b7d59aeb" have entirely different histories.

6 changed files with 81 additions and 134 deletions

View File

@ -154,7 +154,6 @@
}, },
"scripts": { "scripts": {
"build-fe": "cd web/themes/custom/dchadwick && npm ci && npm run compile", "build-fe": "cd web/themes/custom/dchadwick && npm ci && npm run compile",
"deploy": "sh scripts/deploy.sh", "deploy": "sh scripts/deploy.sh"
"start-proxy": "php -S 0.0.0.0:8888 proxy.php"
} }
} }

View File

@ -1,24 +0,0 @@
<?php
$url = $_GET['url'] ?? '';
if (empty($url)) {
http_response_code(400);
echo json_encode(['error' => 'No URL provided']);
exit;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Accept: application/vnd.api+json',
'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.0.1 Safari/605.1.15'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
http_response_code($httpCode);
header('Content-Type: application/vnd.api+json');
echo $response;

View File

@ -5,7 +5,6 @@ namespace Drupal\ufc\Commands;
use Drupal\ufc\Services\FighterImporter; use Drupal\ufc\Services\FighterImporter;
use Drupal\ufc\Services\FightImporter; use Drupal\ufc\Services\FightImporter;
use Drupal\Core\Batch\BatchBuilder; use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\node\Entity\Node; use Drupal\node\Entity\Node;
use Drush\Commands\DrushCommands; use Drush\Commands\DrushCommands;
use Drush\Attributes as CLI; use Drush\Attributes as CLI;
@ -13,13 +12,10 @@ use Symfony\Component\DomCrawler\Crawler;
class UfcCommands extends DrushCommands { class UfcCommands extends DrushCommands {
/**
* The UFC fighter list cache ID.
*/
protected $cacheId = 'ufc:fighter-list'; protected $cacheId = 'ufc:fighter-list';
/** /**
* Import fighters from UFC.com JSON:API. * Import fighters from UFC.com.
*/ */
#[CLI\Command(name: 'ufc:import-fighters', aliases: ['impft'])] #[CLI\Command(name: 'ufc:import-fighters', aliases: ['impft'])]
public function importFighters(): void { public function importFighters(): void {
@ -28,22 +24,32 @@ class UfcCommands extends DrushCommands {
// First check for the item in cache. // First check for the item in cache.
$fighter_list = \Drupal::cache()->get($this->cacheId); $fighter_list = \Drupal::cache()->get($this->cacheId);
if (!$fighter_list) { if (!$fighter_list) {
dump("Getting fighters from UFC.com JSON:API"); /* $fighter_list = $fighter_importer->getListOfCurrentFighters(); */
$fighter_list = $fighter_importer->getListOfCurrentFighters(); $api_list = \Drupal::httpClient()->get('https://www.ufc.com/jsonapi/node/athlete')->getBody()->getContents();
\Drupal::cache()->set($this->cacheId, $fighter_list, CacheBackendInterface::CACHE_PERMANENT); $fighter_list = json_decode($api_list)->data;
dump($fighter_list);
exit();
\Drupal::cache()->set($this->cacheId, $fighter_list, time() + 300);
} }
else { else {
$fighter_list = $fighter_list->data; $fighter_list = $fighter_list->data;
} }
$fighter_import_queue = \Drupal::queue('fighter_import'); $fighter_import_queue = \Drupal::queue('fighter_import');
foreach ($fighter_list as $fighter) { foreach ($fighter_list as $division => $fighters) {
foreach ($fighters as $fighter) {
$queue_item = new \stdClass(); $queue_item = new \stdClass();
$queue_item->id = $fighter['id']; $queue_item->first_name = $fighter['firstname'];
$queue_item->last_name = $fighter['lastname'];
$queue_item->image = $fighter['image'];
$queue_item->division = $division;
$queue_item->profile = $fighter['profile'];
$fighter_import_queue->createItem($queue_item); $fighter_import_queue->createItem($queue_item);
} }
} }
}
/** /**
* Import events based on ESPN.com. * Import events based on ESPN.com.
*/ */

View File

@ -95,22 +95,28 @@ final class FighterQueueWorker extends QueueWorkerBase implements ContainerFacto
* The item to process. * The item to process.
*/ */
private function doProcess($item): bool { private function doProcess($item): bool {
// @todo: $fighter = new Fighter(\Drupal::httpClient());
// Item->id will be the ID of the fighter. $fighter->first_name = $item->first_name;
// Need to fetch fighter list from cache. $fighter->last_name = $item->last_name;
// Then get the corred fighter based on the id. $fighter->image = $item->image;
// Then update or create the fighter node, including any media. $fighter->class = $item->division;
// these values will need to be retrieves from the existing data. if (!$fighter->scrapeDataFromFighterPage($item->profile)) {
/* $node_lookup = reset(\Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['title' => $title])); */ \Drupal::logger('ufc')->alert("FAILED: $fighter->first_name $fighter->last_name to " . $item->profile);
/* if (!empty($node_lookup)) { */ }
/* // Update instead of create. */ // Check if node exists, by title.
/* $fighter->updatePlayerNode($node_lookup->id()); */ $fighter->createMediaEntityFromImage();
/* \Drupal::logger('ufc')->notice("$title updated successfully."); */ $title = $fighter->first_name . " " . $fighter->last_name;
/* } */ $node_lookup = reset(\Drupal::entityTypeManager()->getStorage('node')->loadByProperties(['title' => $title]));
/* else { */
/* \Drupal::logger('ufc')->warning("No existing player found for $title...creating"); */ if (!empty($node_lookup)) {
/* $fighter->createPlayerNode(); */ // Update instead of create.
/* } */ $fighter->updatePlayerNode($node_lookup->id());
\Drupal::logger('ufc')->notice("$title updated successfully.");
}
else {
\Drupal::logger('ufc')->warning("No existing player found for $title...creating");
$fighter->createPlayerNode();
}
return TRUE; return TRUE;
} }

View File

@ -24,18 +24,22 @@ class FighterImporter {
*/ */
protected $entityTypeManager; protected $entityTypeManager;
/**
* The UFC cache bin.
*/
protected $cache;
/** /**
* Array of all fighters. * Array of all fighters.
* @var array[] * @var array[]
*/ */
public $fighters = []; public $fighters = [];
public $cookieJar; /**
* The current weight class.
* @var string
*/
protected $weightClass;
/**
* The UFC cache bin.
*/
protected $cache;
/** /**
* The base url for fighter lists. * The base url for fighter lists.
@ -60,64 +64,6 @@ class FighterImporter {
$this->cache = $cache; $this->cache = $cache;
} }
/**
* Get list of current fighters.
*
* @return array $fighters
*/
public function getListOfCurrentFighters(): array {
$this->addFightersFromUrl(self::UFC_BASE);
return $this->fighters;
}
/**
* Add fighters from a URL.
*
* @param string $url
* The URL to scrape.
*/
public function addFightersFromUrl(string $url) {
sleep(10);
dump("Requesting: " . $url);
$proxied_url = 'http://192.168.0.13:8888/?url=' . urlencode($url);
$ua = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4.1 Safari/605.1.15';
$headers = [
'referer' => true,
'verify' => false,
'headers' => [
'User-Agent' => $ua,
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding' => 'gzip, deflate, br',
'Cache-Control' => 'no-cache',
'Pragma' => 'no-cache',
'Referer' => 'https://www.ufc.com/athletes/all',
]
];
$response = $this->httpClient->request('GET', $proxied_url, $headers);
$content = $response->getBody()->getContents();
$content_arr = json_decode($content, TRUE);
if (!$content_arr) {
exit("something went wrong");
}
$athletes = $content_arr['data'];
$count = count($athletes);
dump("Found " . $count . " fighters.");
foreach ($athletes as $athlete) {
$this->fighters[] = $athlete;
}
$next_page = $content_arr['links']['next']['href'] ?? FALSE;
if ($next_page) {
$this->addFightersFromUrl($next_page);
}
}
/**
* There is a pager, loop through to get all fighters.
*
* @param string $base_url
*/
public function importFighters(): void { public function importFighters(): void {
// Get JSON File from API. // Get JSON File from API.
$api_athletes = $this->cache->get('ufc_api_athletes'); $api_athletes = $this->cache->get('ufc_api_athletes');
@ -129,6 +75,20 @@ class FighterImporter {
// Iterate over data to import all fighters. // Iterate over data to import all fighters.
} }
/**
* Get list of current fighters.
*
* @return array $fighters
*/
public function getListOfCurrentFighters(): array {
foreach ($this->divisions as $division => $div_base_url) {
$division_url = self::UFC_BASE . $div_base_url;
$this->weightClass = $division;
echo "Starting import for " . $division . "\n";
self::loopThroughFighterPages($division_url);
}
return $this->fighters;
}
/** /**
* There is a pager, loop through to get all fighters. * There is a pager, loop through to get all fighters.

View File

@ -12,19 +12,19 @@
# This means that if you want to override any value of a parameter, the # This means that if you want to override any value of a parameter, the
# whole parameter array needs to be copied from # whole parameter array needs to be copied from
# sites/default/default.services.yml or from core/core.services.yml file. # sites/default/default.services.yml or from core/core.services.yml file.
parameters: # parameters:
http.response.debug_cacheability_headers: true # http.response.debug_cacheability_headers: true
services: # services:
cache.backend.null: # cache.backend.null:
class: Drupal\Core\Cache\NullBackendFactory # class: Drupal\Core\Cache\NullBackendFactory
logger.channel.config_schema: # logger.channel.config_schema:
parent: logger.channel_base # parent: logger.channel_base
arguments: [ 'config_schema' ] # arguments: [ 'config_schema' ]
config.schema_checker: # config.schema_checker:
class: Drupal\Core\Config\Development\LenientConfigSchemaChecker # class: Drupal\Core\Config\Development\LenientConfigSchemaChecker
arguments: # arguments:
- '@config.typed' # - '@config.typed'
- '@messenger' # - '@messenger'
- '@logger.channel.config_schema' # - '@logger.channel.config_schema'
tags: # tags:
- { name: event_subscriber } # - { name: event_subscriber }