/** * Finn IMDB-ID for filmen */ public function get_imdb_id($cache = true, $save = true, $fetch = false) { if ($this->imdb_id_custom) { return $this->imdb_id; } if ($cache && $this->imdb_id) { return $this->imdb_id; } $id = false; // har vi id-cache? if ($cache && file_exists($this->path . "/" . self::FILE_IMDB_ID)) { $id = trim(file_get_contents($this->path . "/" . self::FILE_IMDB_ID)); $this->imdb_id = $id; return $id; } // sjekk for .nfo filer $nfo_files = FilmDB::search_folder($this->path, function ($folder, $file) { if (!is_file($folder . "/" . $file)) { return false; } if (substr($file, -4) != ".nfo") { return false; } return true; }); if (count($nfo_files) > 0) { foreach ($nfo_files as $file) { $file = $this->path . "/" . $file; // sjekk for imdb-lenke i nfo-filene $matches = array(); if (preg_match_all("#http://[a-z\\.]*imdb.com/title/(tt[0-9]+)(/|\\s)#", @file_get_contents($file), $matches)) { $prev_id = false; foreach ($matches[1] as $v) { if ($prev_id !== false && $prev_id != $v) { continue 2; } $prev_id = $v; } // kun 1 unik id ble funnet $id = $prev_id; break; } } } // må vi søke etter filmen? if (!$id && $fetch) { $search = $this->get_clean_name(); // velg første film vi finner $res = IMDB::search($search); if ($res === false) { $this->set_failed_status("HTTP-request failed for IMDB-search"); return false; } if (is_array($res)) { // fant ingen treff? if (count($res) == 0) { $this->set_failed_status("No search match."); return false; } $id = $res[0]['id']; } else { $id = $res; } } // lagre id? if ($id && $save) { if (!file_put_contents($this->path . "/" . self::FILE_IMDB_ID, $id)) { throw new \Exception("Kunne ikke lagre film-ID."); } } $this->imdb_id = $id; return $id; }