/** * save to database **/ public function save() { $db = nSQL::connect(); $vars = get_object_vars($this); $vars = array_filter($vars, function ($a) { return !empty($a); }); if (!$vars) { return false; } if (!$this->id) { $properties = array_keys($vars); $sql = sprintf("INSERT INTO %s (%s) VALUES (%s)", $this->get_table(), implode(', ', $properties), "'" . implode("', '", $vars) . "'"); if ($result = $db->query($sql)) { $this->id = $db->insert_id; } } else { $updates = array(); foreach ($vars as $property => $value) { $updates[] = sprintf("%s = '%s'", $property, $db->real_escape_string($value)); } $sql = sprintf("UPDATE %s SET %s WHERE id = %d", $this->get_table(), implode(', ', $updates), $this->id); $result = $db->query($sql); } return $result; }
/** * Generate alternate titles for the film using RhymeBrain. * @param int $name_count, the number of names to generate **/ public function generate_names($names_per_word = 5, $rhymes_per_word = 10) { if (!$this->name) { return false; } $names = nRhyme::process_title($this->name, $names_per_word, $rhymes_per_word); $db = nSQL::connect(); if ($names) { foreach ($names as $data) { if (nGlibName::load_one(array('name' => $data['title'], 'movie_id' => $this->get_id()))) { # glib title already exists continue; } $glib = new nGlibName(array('name' => $data['title'], 'movie_id' => $this->get_id())); if ($glib->save()) { $word = nWord::load_one(array('word' => $data['rhyme'])); if (!$word) { $word = new nWord(array('word' => $data['rhyme'])); $word->save(); } if ($word_id = $word->get_id()) { $db->query(sprintf("INSERT INTO glib_name_word (glib_name_id, word_id) VALUES (%d, %d)", $glib->get_id(), $word_id)); } } } } }
/** * Retrieve a list of Opening Movies * that are not already in the database **/ public static function get_opening_movies($limit = 20) { try { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, static::$base_url . "/lists/movies/opening.json?apikey=" . RT_KEY . "&limit={$limit}"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $json = curl_exec($curl); curl_close($curl); if (($json = json_decode($json)) !== NULL) { $db = nSQL::connect(); foreach ($json->movies as $movie) { # check to see if this movie is already in our DB $result = $db->query(sprintf("SELECT id FROM movie WHERE rt_id = %d", $movie->id)); # we have no record of this film. add it to the database and calculate glib names if (!$result->num_rows) { $movie_object = new nMovie(array('name' => $movie->title, 'rt_id' => $movie->id, 'release_date' => isset($movie->release_dates->theater) ? strtotime($movie->release_dates->theater) : time())); if ($movie_object->save()) { $movie_object->generate_names(); } } } } } catch (Exception $e) { error_log(print_r($e, true)); } return NULL; }
/** * find rhymes for a word. * * @param string $word, the word to rhyme * @param int $count, the number of rhymes to get * * @return an array where the indices are the rhyming words and the values are the corresponding * rhyme scores. the RhymeBrain API seems to sort by score automatically, so this script doesnt bother. **/ public static function get_rhymes($word, $count = 10) { $ch = curl_init(); # &maxResults=$count # NOTE: taking the above expression out of the url. # limiting results doesn't seem to retrieve the highest-scored rhymes... curl_setopt($ch, CURLOPT_URL, static::$base_url . "?function=getRhymes&word={$word}"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $json = curl_exec($ch); curl_close($ch); if (($json = json_decode($json)) !== FALSE) { $return = array(); $db = nSQL::connect(); $current = 0; foreach ($json as $j) { if ($current == $count) { break; } if (isset(static::$custom_word_scores[$j->word])) { $glib_score = static::$custom_word_scores[$j->word]; } else { # query to see if we have a score for this word $sql = sprintf("SELECT score FROM word WHERE word = '%s' LIMIT 1", nSQL::escape($j->word)); $glib_score = 0; $result = $db->query($sql); if ($result->num_rows) { list($glib_score) = $result->fetch_row(); } static::$custom_word_scores[$j->word] = $glib_score; } $return[$j->word] = array('score' => $j->score, 'glib_score' => $glib_score); $current++; } return $return; } return false; }