Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed method Spouse #275

Merged
merged 5 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 127 additions & 58 deletions src/Imdb/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,74 +598,143 @@ public function height()
public function spouse()
{
if (empty($this->spouses)) {
$this->getPage("Bio");
$doc = new \DOMDocument();
@$doc->loadHTML($this->page["Bio"]);
$xp = new \DOMXPath($doc);
$posters = array();
$found = false;
if ($tab = $doc->getElementById('tableSpouses')) {
foreach ($tab->getElementsByTagName('tr') as $sp) {
$first = $sp->getElementsByTagName('td')->item(0); // name and IMDBID
$nam = trim($first->nodeValue);
if ($href = $first->getElementsByTagName('a')->item(0)) {
$mid = preg_replace('!.*/name/nm(\d+).*!', '$1', $href->getAttribute('href'));
} else {
$mid = '';
}
if (!empty($nam)) {
$found = true;
}
$first = $sp->getElementsByTagName('td')->item(1); // additional details
$html = $first->ownerDocument->saveXML($first);
preg_match_all('!(\(.+?\))!ms', $html, $matches);
$comment = '';
$children = '';
for ($i = 0; $i < count($matches[0]); ++$i) {
if ($i == 0) { // usually the "lifespan" of the relation
if (preg_match('!(\(<a href="/date/(?<month>\d+)-(?<day>\d+).*>\s*\d+\s*(?<monthname>.*)<.*)?\s*(&nbsp;)?\s*(?<year>\d{4})\s+-!ms',
$matches[0][0], $match)) { // from date
$xp = $this->getXpathPage("Bio");
$spouse = $xp->query("//table[contains(@id, 'tableFamily')]/tr[1]/td[1]");
if ($spouse->count()) {
if (trim($spouse->item(0)->nodeValue) == "Spouse") {
if ($tab = $xp->query("//table[contains(@id, 'tableFamily')]/tr[1]/td[2]")) {
$html = $tab->item(0)->ownerDocument->saveXML($tab->item(0));
$htmlParts = explode("<br/>", $html);
foreach ($htmlParts as $parts) {
// imdbid
$mid = '';
if (preg_match('/<a href="\/name\/nm(\d+).*">/', $parts, $url)) {
$mid = $url[1];
}
// spouse name
$name = '';
if (preg_match('![^(]*\([^(\d]*!', $parts, $nameRaw)) {
$nameClean = preg_replace('/[^A-Za-z0-9().\-\"\"\W ]/', '', strip_tags($nameRaw[0]));
if (strpos($nameClean, ')') && !strpos($nameClean, '?')) {
$name = trim($nameClean);
echo 'name';
} else {
$nameClean = explode('(', $nameClean);
if (!strpos($nameClean[0], '?')) {
$name = trim($nameClean[0]);
}
}
}
//Dates, comment and children
preg_match_all('!(\(.+?\))!ms', strip_tags($parts), $matches);
// remove leftover spouse name parts (imdbid 0001228 extra name between brackets)
if (!preg_match('~[0-9]+~', $matches[0][0]) && !strpos($matches[0][0], '?')) {
unset($matches[0][0]);
sort($matches[0]);
}
$datesRaw = preg_replace('/[^A-Za-z0-9-]/', ' ', $matches[0][0]);
//from date
$fromDay = '';
$fromMonth = '';
$fromYear = '';
$fromDateRaw = explode('-', $datesRaw);
if (array_key_exists(0, $fromDateRaw) && preg_match('~[0-9]+~', $fromDateRaw[0])) {
$fromDate = array_values(array_filter(explode(' ', trim($fromDateRaw[0]))));
$count = count($fromDate);
if ($count == 1) {
if (preg_match('~[0-9]+~', $fromDate[0])) {
$fromYear = $fromDate[0];
}
} elseif ($count == 2) {
$fromMonth = trim($fromDate[0]);
if (preg_match('~[0-9]+~', $fromDate[1])) {
$fromYear = $fromDate[1];
}
} elseif ($count == 3) {
if (preg_match('~[0-9]+~', $fromDate[0])) {
$fromDay = $fromDate[0];
}
$fromMonth = trim($fromDate[1]);
if (preg_match('~[0-9]+~', $fromDate[2])) {
$fromYear = $fromDate[2];
}
}
$from = array(
"day" => $match['day'],
"month" => $match['month'],
"mon" => $match['monthname'],
"year" => $match['year']
);
"day" => $fromDay,
"month" => $fromMonth,
"mon" => $this->monthNo($fromMonth),
"year" => $fromYear
);
} else {
$from = array("day" => '', "month" => '', "mon" => '', "year" => '');
$from = array("day" => '', "month" => '', "mon" => '', "year" => '');
}
if (preg_match('!(.+?)\s+-\s+(<a href="/date/(?<month>\d+)-(?<day>\d+).*>\s*\d+\s*(?<monthname>.*)<.*)?\s*(&nbsp;)?\s*(?<year>\d{4})!ms',
$matches[0][0], $match)) { // to date
//to date
$toDay = '';
$toMonth = '';
$toYear = '';
$toDateRaw = explode('-', $datesRaw);
if (array_key_exists(1, $toDateRaw) && preg_match('~[0-9]+~', $toDateRaw[1])) {
$toDate = array_values(array_filter(explode(' ', trim($toDateRaw[1]))));
$count = count($toDate);
if ($count == 1) {
if (preg_match('~[0-9]+~', $toDate[0])) {
$toYear = $toDate[0];
}
} elseif ($count == 2) {
$toMonth = trim($toDate[0]);
if (preg_match('~[0-9]+~', $toDate[1])) {
$toYear = $toDate[1];
}
} elseif ($count == 3) {
if (preg_match('~[0-9]+~', $toDate[0])) {
$toDay = $toDate[0];
}
$toMonth = trim($toDate[1]);
if (preg_match('~[0-9]+~', $toDate[2])) {
$toYear = $toDate[2];
}
}
$to = array(
"day" => $match['day'],
"month" => $match['month'],
"mon" => $match['monthname'],
"year" => $match['year']
);
"day" => $toDay,
"month" => $toMonth,
"mon" => $this->monthNo($toMonth),
"year" => $toYear
);
} else {
$to = array("day" => '', "month" => '', "mon" => '', "year" => '');
$to = array("day" => '', "month" => '', "mon" => '', "year" => '');
}
}
if ($i > 0 || empty($from)) {
$comment .= $matches[0][$i] . " ";
// Comment and Children
$elements = count($matches[0]) - 1; //count remaining elements after dates
$comment = '';
$children = 0;
if ($elements == 1) {
if (preg_match('!(\d+) child!', $matches[0][1], $match)) {
$children = $match[1];
} else {
$comment = trim($matches[0][1], " ()");
}
} elseif ($elements == 2) {
//sometimes those 2 values are reversed, don't know why, so have to check.
if (preg_match('!(\d+) child!', $matches[0][1], $match)) {
$children = $match[1];
$comment = trim($matches[0][2], " ()");
} elseif (preg_match('!(\d+) child!', $matches[0][2], $match)) {
$children = $match[1];
$comment = trim($matches[0][1], " ()");
}
}
$this->spouses[] = array(
'imdb' => $mid,
'name' => $name,
'from' => $from,
'to' => $to,
'comment' => $comment,
'children' => (int)$children
);
}
}
if (preg_match('!(\d+) child!', $html, $match)) {
$children = $match[1];
}
$this->spouses[] = array(
'imdb' => $mid,
'name' => $nam,
'from' => $from,
'to' => $to,
'comment' => $comment,
'children' => $children
);
}
}
if (!$found) {
return $this->spouses;
} // no spouses
}
return $this->spouses;
}
Expand Down
142 changes: 120 additions & 22 deletions tests/PersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ public function test_movies_soundtrack()
$result = $person->movies_soundtrack();
$this->assertIsArray($result);
$this->assertGreaterThanOrEqual(6, $result);
$poppyHill = current(array_filter($result, function ($item) { return $item['mid'] == '1798188'; }));
$poppyHill = current(array_filter($result, function ($item) {
return $item['mid'] == '1798188';
}));
$this->assertEquals('1798188', $poppyHill['mid']);
$this->assertEquals('From Up on Poppy Hill', $poppyHill['name']);
$this->assertEquals('2011', $poppyHill['year']);
Expand Down Expand Up @@ -176,10 +178,8 @@ public function test_movies_self()
$this->assertLessThan(35, count($result));

$matches = 0;
foreach($result as $movie)
{
if($movie['mid'] == 1095875)
{
foreach ($result as $movie) {
if ($movie['mid'] == 1095875) {
$this->assertEquals('Jônetsu tairiku', $movie['name']);
$this->assertEquals('2014', $movie['year']);
$this->assertEquals('', $movie['chid']);
Expand Down Expand Up @@ -224,7 +224,7 @@ public function test_movies_archive()
$this->assertEquals(array(), $oscars['addons']);

$troldspejlet = array_find_item($result, 'mid', '0318251');
$this->assertEquals('0318251',$troldspejlet['mid']);
$this->assertEquals('0318251', $troldspejlet['mid']);
$this->assertEquals('Troldspejlet', $troldspejlet['name']);
$this->assertEquals('2009', $troldspejlet['year']);
$this->assertEquals('', $troldspejlet['chid']);
Expand Down Expand Up @@ -284,13 +284,111 @@ public function test_height()
$this->assertEquals('1.64 m', $result['metric']);
}

// @TODO Write proper tests for this method
// @TODO this method doesn't work
// public function test_spouse()
// {
// $person = $this->getimdb_person();
// $this->assertNotEmpty($person->spouse());
// }
public function test_spouse_still_married()
{
$person = $this->getimdb_person();
$result = $person->spouse();
$this->assertNotEmpty($result);
$this->assertCount(1, $result);
$first = $result[0];
$this->assertEquals(array(
'imdb' => '1088112',
'name' => 'Akemi Ôta',
'from' =>
array(
'day' => '',
'month' => 'October',
'mon' => '10',
'year' => '1965',
),
'to' =>
array(
'day' => '',
'month' => '',
'mon' => '',
'year' => '',
),
'comment' => '',
'children' => 2,
), $first);
}

public function test_spouse_multiple_spouse()
{
$person = $this->getimdb_person('0000245');
$result = $person->spouse();
$this->assertNotEmpty($result);
$this->assertCount(3, $result);
$this->assertEquals(array(
'imdb' => '6699367',
'name' => 'Susan Schneider',
'from' =>
array(
'day' => 22,
'month' => 'October',
'mon' => 10,
'year' => 2011,
),
'to' =>
array(
'day' => 11,
'month' => 'August',
'mon' => 8,
'year' => 2014,
),
'comment' => 'his death',
'children' => 0,
), $result[0]);

$this->assertEquals(array(
'imdb' => '0931265',
'name' => 'Marsha Garces Williams',
'from' =>
array(
'day' => 30,
'month' => 'April',
'mon' => 4,
'year' => 1989,
),
'to' =>
array(
'day' => '',
'month' => '',
'mon' => '',
'year' => 2010,
),
'comment' => 'divorced',
'children' => 2,
), $result[1]);

$this->assertEquals(array(
'imdb' => '0892239',
'name' => 'Valerie Velardi',
'from' =>
array(
'day' => 4,
'month' => 'June',
'mon' => 6,
'year' => 1978,
),
'to' =>
array(
'day' => 6,
'month' => 'December',
'mon' => 12,
'year' => 1988,
),
'comment' => 'divorced',
'children' => 1,
), $result[2]);
}

public function test_spouse_no_spouse()
{
$person = $this->getimdb_person('0005132');
$result = $person->spouse();
$this->assertCount(0, $result);
}

//@TODO Write proper tests for this method
public function test_bio()
Expand Down Expand Up @@ -384,19 +482,19 @@ public function test_magcovers()
'inturl' => '',
'name' => 'Comixene (DE)',
'date' =>
array(
'day' => '',
'month' => 'September',
'mon' => '09',
'year' => '2005',
'full' => 'September 2005',
),
array(
'day' => '',
'month' => 'September',
'mon' => '09',
'year' => '2005',
'full' => 'September 2005',
),
'details' => 'Iss. 89',
'auturl' => '',
'author' => '',
), $first);
), $first);
}

public function test_real_id()
{
$person = $this->getimdb_person();
Expand Down