This commit is contained in:
Dan Chadwick
2024-04-09 01:47:04 +00:00
parent 3bcbe3b783
commit 3cfd95ee81
219 changed files with 47894 additions and 3767 deletions

View File

@@ -0,0 +1,137 @@
<?php
namespace Drupal\ufc\Traits;
trait NameConversionTrait {
public function convertNames() {
// Manual overrides for name problems go here.
if ($this->first_name == 'Khaos') {
$this->first_name = 'Kalinn';
}
if ($this->first_name == "Aoriqileng") {
$suffix = $this->first_name;
}
if ($this->first_name == 'J') {
$this->first_name = 'JP';
$this->last_name = 'Buys';
}
if ($this->last_name == 'Mc Kee') {
$this->last_name = 'McKee';
}
if ($this->last_name == 'Mc Gee') {
$this->last_name = 'McGee';
}
if ($this->last_name == 'Mc Gregor') {
$this->last_name = 'mcgregor';
}
if ($this->last_name == "O&#039; Malley") {
$this->last_name = 'Omalley';
}
if ($this->first_name == "Don&#039;") {
$this->first_name = 'dontale';
$this->last_name = 'mayes';
}
if ($this->first_name == "Marc-") {
$this->first_name = 'Marc';
}
if ($this->first_name == "A") {
$this->first_name = 'AJ';
$this->last_name = 'Dobson';
}
if ($this->first_name == "C") {
$this->first_name = 'CB';
$this->last_name = 'Dollaway';
}
if ($this->last_name == "Della Maddalena") {
$this->last_name = 'Della';
}
if ($this->first_name == "Elizeudos") {
$this->first_name = 'elizeu';
$this->last_name = 'dos-santos';
}
if ($this->last_name == "La Flare") {
$this->last_name = 'laflare';
}
if ($this->first_name == "JoelÁlvarez") {
$this->first_name = 'Joel';
}
if ($this->last_name == "J Brown") {
$this->first_name = 'TJ';
$this->last_name = 'Brown';
}
if ($this->first_name == "Alexda") {
$this->first_name = "alex-da";
}
if ($this->last_name == "Mc Kinney") {
$this->last_name = "mckinney";
}
if ($this->last_name == "Van Camp") {
$this->last_name = "vancamp";
}
if ($this->last_name == "J Laramie") {
$this->first_name = "TJ";
$this->last_name = "Laramie";
}
if ($this->last_name == "Al- Qaisi") {
$this->last_name = "alqaisi";
}
if ($this->first_name == "Alatengheili") {
$this->first_name = "heili";
$this->last_name = "alateng";
}
if ($this->last_name == "J Dillashaw") {
$this->first_name = "TJ";
$this->last_name = "Dillashaw";
}
if ($this->first_name == "Andersondos") {
$this->first_name = "Anderson-dos";
}
if ($this->last_name == "Silvade Andrade") {
$this->last_name = "Silva-de-andrade";
}
if ($this->first_name == "Ode&#039;") {
$this->first_name = "ode";
}
if ($this->first_name == "Sumudaerji") {
$this->first_name = "su";
$this->last_name = "mudaerji";
}
if ($this->first_name == "Georges" && $this->last_name == "St- Pierre") {
$this->last_name = "st pierre";
}
}
}

View File

@@ -0,0 +1,132 @@
<?php
namespace Drupal\ufc\Traits;
trait ScraperHelperTrait {
/**
* Map to extract average from a blob of text, process, and set.
*/
public function extractAndSetAverage(\DOMText|\DOMElement $elem): void {
$child_content = strtolower($elem->textContent);
if (strlen($child_content) < 1) {
return;
}
$averages_map = [
"strikes_per_min" => [
"sig. str. landed",
"cleanFloatDatapoint",
],
"absorbed_per_min" => [
"sig. str. absorbed",
"cleanFloatDatapoint",
],
"takedowns_per_15" => [
"takedown avg",
"cleanFloatDatapoint",
],
"submission_avg_per_15" => [
"submission avg",
"cleanFloatDatapoint",
],
"sig_strike_defense" => [
"sig. str. defense",
"cleanIntDatapoint",
],
"takedown_defense" => [
"takedown defense",
"cleanIntDatapoint",
],
"knockdown_ratio" => [
"knockdown avg",
"cleanFloatDatapoint",
],
"average_fight_time" => [
"average fight time",
"convertAverageFightTimeToSeconds",
],
];
foreach ($averages_map as $avg_type => $handlers) {
// First item in $handlers is the string to search for.
// Second item is the cleaning function.
if (str_contains($child_content, $handlers[0])) {
/* @var DOMElement */
$this->{$avg_type} = $this->{$handlers[1]}($elem->firstElementChild->nodeValue);
}
}
}
/**
* Extracts accuracy number from a string.
*/
public function extractAccuracyFromString(string $string): float {
// Examples:
// Striking accuracy 65%
// Grappling Accuracy 60%
$split_on_space = explode(" ", $string);
$percentage = $split_on_space[2] ?? FALSE;
// If no percentage located, simply set to 0.
if (!$percentage) {
return 0.0;
}
$percentage = (float) trim(str_replace('%', '', $percentage));
assert(is_float($percentage));
return $percentage;
}
/**
* Pulls strike totals.
*/
public function setStrikes(string $input): void {
$clean = str_replace(["\n", '"'], "", $input);
$break_on_space = array_filter(explode(" ", $clean));
$data = [];
foreach ($break_on_space as $str) {
if (ctype_digit($str)) {
$data[] = $str;
}
}
if (!empty($data) && count($data) == 3) {
$this->standing_strikes = (int) $data[0];
$this->clinch_strikes = (int) $data[1];
$this->ground_strikes = (int) $data[2];
}
}
/**
* Converts time format (MM:SS) to seconds (s).
*/
public function convertAverageFightTimeToSeconds(string $fight_time): int {
$seconds = 0;
$mins_seconds = explode(':', $fight_time);
$seconds += ( (int) $mins_seconds[0] * 60) + (int) $mins_seconds[1];
assert(is_int($seconds));
return $seconds;
}
/**
* Cleans a float datapoint for insertion to the database.
*/
protected function cleanFloatDatapoint(string $datapoint): float {
$datapoint = str_replace("\n", "", $datapoint);
$datapoint = (float) trim($datapoint);
assert(is_float($datapoint));
return $datapoint;
}
/**
* Cleans an int datapoint for insertion to the database.
*/
protected function cleanIntDatapoint(string $datapoint): int {
$datapoint = str_replace("\n", "", $datapoint);
$datapoint = (int) trim($datapoint);
assert(is_int($datapoint));
return $datapoint;
}
}