tboothman / imdbphp

PHP library for retrieving film and tv information from IMDb
253 stars 84 forks source link

bug: Awards not working #321

Open wunderkinnd opened 1 year ago

wunderkinnd commented 1 year ago

Returning empty array when calling $movie->awards();

eg on tt0059575

code example

$imdbid = 0059575;
$config = new \Imdb\Config();
$config->language = 'de-DE,de,en';
$movie = new \Imdb\Title($imdbid, $config);

$imdb_awards = $movie->awards();

echo "<pre>";
    print_r($imdb_awards);
echo "</pre>";

error-message Notice: Trying to access array offset on value of type null

when trying to print the array

AgentSmith0 commented 1 year ago

I can confirm this issue, awards() always returns an empty array.

duck7000 commented 8 months ago

as a courtesy to the users here i will share my awards method that uses GraphQL I hope that somebody will use it. If you need additional info i explained it all in my wiki page https://github.com/duck7000/imdbphp6/wiki/Title-class

Remember that it might be different from the award method use here

    #-------------------------------------------------------[ Awards ]---
    /**
     * Get all awards for a title
     * @param $winsOnly Default: false, set to true to only get won awards
     * @param $event Default: "" fill eventId Example " ev0000003" to only get Oscars
     *  Possible values for $event:
     *  ev0000003 (Oscar)
     *  ev0000223 (Emmy)
     *  ev0000292 (Golden Globe)
     * @return array[festivalName][0..n] of 
     *      array[awardYear,awardWinner(bool),awardCategory,awardName,awardNotes
     *      array awardPerons[creditId,creditName,creditNote],awardOutcome]
     *  Array
            (
                [Academy Awards, USA] => Array
                    (
                        [0] => Array
                            (
                        [awardYear] => 1972
                        [awardWinner] => 
                        [awardCategory] => Best Picture
                        [awardName] => Oscar
                        [awardPerons] => Array
                            (
                                [0] => Array
                                    (
                                        [creditId] => nm0000040
                                        [creditName] => Stanley Kubrick
                                        [creditNote] => screenplay/director
                                    )

                            )
                        [awardNotes] => Based on the novel
                        [awardOutcome] => Nominee
                    )
                    )
            )
     * @see IMDB page / (TitlePage)
     */
    public function award($winsOnly = false, $event = "")
    {
        $winsOnly = $winsOnly === true ? "WINS_ONLY" : "null";
        $event = !empty($event) ? "events: " . '"' . trim($event) . '"' : "";

        if (empty($this->awards)) {
            $query = <<<EOF
query Award(\$id: ID!) {
  title(id: \$id) {
    awardNominations(
      first: 9999
      sort: {by: PRESTIGIOUS, order: DESC}
      filter: {wins: $winsOnly $event}
    ) {
      edges {
        node {
          award {
            event {
              text
            }
            text
            category {
              text
            }
            eventEdition {
              year
            }
            notes {
              plainText
            }
          }
          isWinner
          awardedEntities {
            ... on AwardedTitles {
              secondaryAwardNames {
                name {
                  id
                  nameText {
                    text
                  }
                }
                note {
                  plainText
                }
              }
            }
          }
        }
      }
    }
  }
}
EOF;
            $data = $this->graphql->query($query, "Award", ["id" => "tt$this->imdbID"]);
            foreach ($data->title->awardNominations->edges as $edge) {
                $eventName = isset($edge->node->award->event->text) ? $edge->node->award->event->text : '';
                $eventEditionYear = isset($edge->node->award->eventEdition->year) ? $edge->node->award->eventEdition->year : '';
                $awardName = isset($edge->node->award->text) ? $edge->node->award->text : '';
                $awardCategory = isset($edge->node->award->category->text) ? $edge->node->award->category->text : '';
                $awardNotes = isset($edge->node->award->notes->plainText) ? $edge->node->award->notes->plainText : '';
                $awardIsWinner = $edge->node->isWinner;
                $conclusion = $awardIsWinner === true ? "Winner" : "Nominee";

                //credited persons
                $persons = array();
                if ($edge->node->awardedEntities->secondaryAwardNames !== null) {
                    foreach ($edge->node->awardedEntities->secondaryAwardNames as $creditor) {
                        $creditName = isset($creditor->name->nameText->text) ? $creditor->name->nameText->text : '';
                        $creditId = isset($creditor->name->id) ? $creditor->name->id : '';
                        $creditNote = isset($creditor->note->plainText) ? $creditor->note->plainText : '';
                        $persons[] = array(
                            'creditId' => str_replace('nm', '', $creditId),
                            'creditName' => $creditName,
                            'creditNote' => trim($creditNote, " ()")
                        );
                    }
                }

                $this->awards[$eventName][] = array(
                    'awardYear' => $eventEditionYear,
                    'awardWinner' => $awardIsWinner,
                    'awardCategory' => $awardCategory,
                    'awardName' => $awardName,
                    'awardNotes' => $awardNotes,
                    'awardPerons' => $persons,
                    'awardOutcome' => $conclusion
                );
            }
        }
        return $this->awards;
    }