http_client = $httpClient; } /** * Get fighters url from name. * * @return string */ public function getFighterUrl() { // Manual overrides for name problems go here. if ($this->first_name == 'Khaos') { $this->first_name = 'Kalinn'; } 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' Malley") { $this->last_name = 'Omalley'; } if ($this->first_name == "Don'") { $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'") { $this->first_name = "ode"; } if ($this->first_name == "Sumudaerji") { $this->first_name = "su"; $this->last_name = "mudaerji"; } $hyphens = str_replace(" ", "-", $this->last_name); $suffix = $this->first_name . "-" . $hyphens; if ($this->first_name == "Aoriqileng") { $suffix = $this->first_name; } $url = "https://www.ufc.com/athlete/$suffix"; $trim = rtrim($url); return $url; } /** * Get contents of the fighter page. * * @return void */ public function getFighterPage() { try { $request = $this->http_client->request('GET', $this->getFighterUrl(), ['verify' => FALSE]); $this->fighter_page = $request->getBody()->getContents(); } catch (\Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; // exit(); } /* USE THIS FOR TESTING: $player_url = "https://www.ufc.com/athlete/anthony-hamilton"; try { $request = $this->http_client->request('GET', $player_url); } catch (\Exception $e) { return FALSE; } $content = $request->getBody()->getContents(); */ } /** * Get fighter age. * * @return void */ public function getAge() { $pattern = '/
[0-9]+\.[0-9]+/s'; preg_match_all($pattern, $this->fighter_page, $matches); $matches = reset($matches); // Get height. $pattern = '/[0-9]+\.[0-9]+/'; preg_match($pattern, $matches[0], $height); $this->height = reset($height); // Get weight. $pattern = '/[0-9]+\.[0-9]+/'; preg_match($pattern, $matches[1], $weight); $this->weight = reset($weight); // Get reach. $pattern = '/[0-9]+\.[0-9]+/'; preg_match($pattern, $matches[2], $reach); $this->reach = reset($reach); // Get leg reach. $pattern = '/[0-9]+\.[0-9]+/'; preg_match($pattern, $matches[3], $leg_reach); $this->leg_reach = reset($leg_reach); } /** * Extracts the fighter record. * * @return void */ public function getFighterRecord() { $pattern = "/[0-9]+[-][0-9]+[-][0-9]+.\(W-L-D\)/"; preg_match($pattern, $this->fighter_page, $matches); $r = reset($matches); $r = str_replace(" (W-L-D)", "", $r); $record_chunks = explode("-", $r); $this->wins = $record_chunks[0]; $this->losses = $record_chunks[1]; $this->ties = $record_chunks[2]; } /** * Extracts the striking accuracy. * * @return void */ public function getStrikingAccuracy() { $pattern = '/Striking accuracy.(.*)<\/title>/'; preg_match($pattern, $this->fighter_page, $matches); $this->striking_accuracy = str_replace('%', '', $matches[1]); } /** * Extracts the grappling accuracy. * * @return void */ public function getGrapplingAccuracy() { $pattern = '/<title>Grappling accuracy.(.*)<\/title>/'; preg_match($pattern, $this->fighter_page, $matches); $this->grappling_accuracy = str_replace('%', '', $matches[1]); } /** * Extracts averages from the statistics section. * * @return void */ public function getAverages() { $pattern = '/<div class="c-stat-compare__group-1(.*)(<\/div>)/sU'; preg_match_all($pattern, $this->fighter_page, $matches); $this->strikes_per_min = $this->extractNumber($matches[1][0]); $this->takedowns_per_15 = $this->extractNumber($matches[1][1]); $this->knockdown_ratio = $this->extractNumber($matches[1][3]); $pattern = '/<div class="c-stat-compare__group-2(.*)(<\/div>)/sU'; preg_match_all($pattern, $this->fighter_page, $matches); $this->absorbed_per_min = $this->extractNumber($matches[1][0]); } /** * Extracts wins per type. * * @return void */ public function getWinsBreakdown() { $pattern = '/<div class="c-stat-3bar__value">(.*)<\/div>/sU'; preg_match_all($pattern, $this->fighter_page, $matches); $this->knockouts = $this->extractWinByType($matches[0][3]); $this->decisions = $this->extractWinByType($matches[0][4]); $this->submissions = $this->extractWinByType($matches[0][5]); } /** * Helper to get number out of string. * * @param [type] $string * @return void */ protected function extractNumber($string) { $no_tags = strip_tags($string); preg_match_all('!\d+!', $no_tags, $matches); $number = implode('.', $matches[0]); return $number; } /** * Helper to extract number from number and percentage. * * @param [type] $string * @return void */ protected function extractWinByType($string) { $no_tags = strip_tags($string); preg_match_all('!\d+!', $no_tags, $matches); return $matches[0][0]; } /** * Creates a player node. * * @return void */ public function createPlayerNode() { $division_id = self::transformWeightClass(); $title = $this->first_name . " " . $this->last_name; $node = Node::create([ 'type' => 'fighter', 'title' => $title, 'field_division' => self::getTermByName($division_id), 'field_first_name' => $this->first_name, 'field_last_name' => $this->last_name, 'field_wins' => $this->wins, 'field_losses' => $this->losses, 'field_ties' => $this->ties, 'field_player_photo' => [ 'target_id' => $this->image_id, ], 'field_striking_accuracy' => $this->striking_accuracy, 'field_grappling_accuracy' => $this->grappling_accuracy, 'field_strikes_per_minute' => $this->strikes_per_min, 'field_absorbed_per_min' => $this->absorbed_per_min, 'field_takedowns_per_15' => $this->takedowns_per_15, 'field_knockdown_ratio' => $this->knockdown_ratio, 'field_knockouts' => $this->knockouts, 'field_decisions' => $this->decisions, 'field_submissions' => $this->submissions, 'field_age' => $this->age, 'field_reach' => $this->reach, 'field_leg_reach' => $this->leg_reach, 'field_height' => $this->height, 'field_weight' => $this->weight ]); $node->status = 1; $node->enforceIsNew(); $node->save(); } /** * Updates a player node. * * @param $nid * @return void */ public function updatePlayerNode($nid) { $node_storage = \Drupal::entityTypeManager()->getStorage('node'); $node = $node_storage->load($nid); $node->field_wins = $this->wins; $node->field_losses = $this->losses; $node->field_ties = $this->ties; $node->field_striking_accuracy = $this->striking_accuracy; $node->field_grappling_accuracy = $this->grappling_accuracy; $node->field_strikes_per_minute = $this->strikes_per_min; $node->field_absorbed_per_min = $this->absorbed_per_min; $node->field_takedowns_per_15 = $this->takedowns_per_15; $node->field_knockdown_ratio = $this->knockdown_ratio; $node->field_knockouts = $this->knockouts; $node->field_decisions = $this->decisions; $node->field_submissions = $this->submissions; $node->field_age = $this->age; $node->field_reach = $this->reach; $node->field_leg_reach = $this->leg_reach; $node->field_height = $this->height; $node->field_weight = $this->weight; $node->save(); } /** * Helper function to transform the weight class. * * @return void */ public function transformWeightClass() { $weight_class = $this->class; $stripped = str_replace("_", " ", $weight_class); $upper = ucwords($stripped); return $upper; } /** * Helper function to retrieve taxo term by name. * * @param [type] $term_name * @return void */ public function getTermByName($term_name) { // Get taxonomy term storage. $taxonomyStorage = \Drupal::service('entity.manager')->getStorage('taxonomy_term'); // Set name properties. $properties['name'] = $term_name; $properties['vid'] = 'ufc_divisions'; // Load taxonomy term by properties. $terms = $taxonomyStorage->loadByProperties($properties); $term = reset($terms); if (!$term) { $new_term = Term::create([ 'name' => $term_name, 'vid' => 'ufc_divisions' ])->save(); return $new_term; }; return $term->id(); } /** * Create media for the player headshot. * * @return void */ public function createMediaEntityFromImage() { $file_name = self::extractFileName(); if (empty($file_name)) { $rand = rand(0,100000000) . '.png'; $file_name = "default-headshot_$rand"; } if (!empty($this->image)) { $file_data = file_get_contents($this->image); } if (empty($file_data)) { $file_data = file_get_contents("public://player-headshots/headshot-default.png"); } $file = file_save_data( $file_data, "public://player-headshots/$file_name", FILE_EXISTS_RENAME); $media_image = Media::create([ 'bundle' => 'image', 'name' => $file_name, 'field_media_image' => [ 'target_id' => $file->id(), ], ]); $media_image->save(); $this->image_id = $media_image->id(); } /** * Helper to extract file name for images. * * @return void */ public function extractFilename() { if (!empty($this->image)) { preg_match('/[A-Z_0-9]*.png/', $this->image, $matches); $file_name = reset($matches); return $file_name; } return FALSE; } }