Skip to content

Instantly share code, notes, and snippets.

@Ferrmolina
Last active July 11, 2016 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ferrmolina/f8aabb74b5cba445b17798008a587636 to your computer and use it in GitHub Desktop.
Save Ferrmolina/f8aabb74b5cba445b17798008a587636 to your computer and use it in GitHub Desktop.
Simple TV Show API - The Movie Database
<?php
include 'tmdb-api.php'; // Require: https://github.com/Alvaroctal/TMDB-PHP-API
$tmdb = new TMDB();
$tmdb->setAPIKey('API KEY HERE');
$tmdb->setLang($lang);
/**
* Obtener toda la información de un show en específico
* @param $terms (String a consultar)
* @return {JSON Object}
*/
function getShow($terms) {
$showID = getTVShowID($terms);
$jsonShow = "";
if ($showID != 0) {
$jsonShow = array(
'Result' => 'Show found',
'Name' => getTVShowName($showID),
'Description' => getTVShowDescription($showID),
'Total_Seasons' => getTVShowNumberSeasons($showID),
'Total_Episodes' => getTVShowNumberEpisodes($showID),
'Seasons' => getTVShowSeason($showID)
);
} else {
$jsonShow = array('Result' => 'Show not found');
}
return json_encode($jsonShow, JSON_PRETTY_PRINT);
}
/**
* Obtener ID del show
* @param $search (Término de búsqueda)
* @return if (resultado) { String } else { Integer }
*/
function getTVShowID($search) {
global $tmdb;
$tvShows = $tmdb->searchTVShow($search);
if (!empty($tvShows)) {
foreach($tvShows as $tvShow){
return $tvShow->getID();
}
} else {
return 0;
}
}
/**
* Obtener nombre del show
* @param $showID (ID del show a buscar)
* @return String
*/
function getTVShowName($showID) {
global $tmdb;
$tvShows = $tmdb->getTVShow($showID);
return $tvShows->getName();
}
/**
* Obtener descripcion del show
* @param $showID (ID del show a buscar)
* @return String
*/
function getTVShowDescription($showID) {
global $tmdb;
$tvShows = $tmdb->getTVShow($showID);
return $tvShows->getOverview();
}
/**
* Obtener total de temporadas del show
* @param $showID (ID del show a buscar)
* @return Integer
*/
function getTVShowNumberSeasons($showID) {
global $tmdb;
$tvShows = $tmdb->getTVShow($showID);
return $tvShows->getNumSeasons();
}
/**
* Obtener total de episodios del show
* @param $showID (ID del show a buscar)
* @return Integer
*/
function getTVShowNumberEpisodes($showID) {
global $tmdb;
$tvShows = $tmdb->getTVShow($showID);
$seasons = $tvShows->getSeasons();
$cantidadEpisodes = "";
for ($i = 1; $i < count($seasons); $i++) {
$season = $tmdb->getSeason($showID, $i);
$cantidadEpisodes += $season->getNumEpisodes();
}
return $cantidadEpisodes;
}
/**
* Obtener listado de temporadas del show
* @param $showID (ID del show a buscar)
* @return {JSON Object}
*/
function getTVShowSeason($showID) {
global $tmdb;
$tvShows = $tmdb->getTVShow($showID);
$seasons = $tvShows->getSeasons();
$JSONSeason = "";
for ($i = 1; $i < count($seasons); $i++) {
$season = $tmdb->getSeason($showID, $i);
$cantEpisodes = $season->getNumEpisodes();
$JSONSeason['Season-'.$i] = iterarEpisodes($cantEpisodes, $showID, $i);
}
return $JSONSeason;
}
/**
* Obtener nombre de episodios por temporada específica
* @param $showID (ID del show a buscar)
* @param $season (numero de temporada)
* @return {JSON Object}
*/
function getEpisodesBySeason($showID, $season) {
global $tmdb;
$season = $tmdb->getSeason($showID, $season);
$episodes = $season->getEpisodes();
$JSONEpisodes = "";
$index = 0;
foreach($episodes as $episode) {
$index++;
$JSONEpisodes['Episode-'.$index] = $episode->getName();
}
return $JSONEpisodes;
}
/**
* Obtener listado de nombres de episodios
* @param $cantEpisodes (numero total de episodios de la temporada)
* @param $showID (ID del show a buscar)
* @param $season (Temporada en la cual se itera y recupera los títulos)
* @return {JSON Object}
*/
function iterarEpisodes($cantEpisodes, $showID, $season) {
for ($n = 0; $n < count($cantEpisodes); $n++) {
return getEpisodesBySeason($showID, $season);
}
}
<?php
/** ============================================ USE ================================================ **/
// /index.php?show=the%20big%20bang%20theory&lang=en
$title = $_GET['show'];
$lang = $_GET['lang'];
include 'fetch.functions.php';
//echo '<pre>';
echo getShow($title);
/** ============================================ OUTPUT ============================================ **/
// {
// "Result": "Show found",
// "Name": "The Big Bang Theory",
// "Description": "The Big Bang Theory is centered on five characters living in Pasadena, California: roommates Leonard Hofstadter and Sheldon Cooper; Penny, a waitress and aspiring actress who lives across the hall; and Leonard and Sheldon's equally geeky and socially awkward friends and co-workers, mechanical engineer Howard Wolowitz and astrophysicist Raj Koothrappali. The geekiness and intellect of the four guys is contrasted for comic effect with Penny's social skills and common sense.",
// "Total_Seasons": 9,
// "Total_Episodes": 207,
// "Seasons": {
// "Season-1": {
// "Episode-1": "Pilot",
// "Episode-2": "The Big Bran Hypothesis",
// "Episode-3": "The Fuzzy Boots Corollary",
// "Episode-4": "The Luminous Fish Effect",
// "Episode-5": "The Hamburger Postulate",
// "Episode-6": "The Middle Earth Paradigm",
// "Episode-7": "The Dumpling Paradox",
// "Episode-8": "The Grasshopper Experiment",
// "Episode-9": "The Cooper-Hofstadter Polarization",
// "Episode-10": "The Loobenfeld Decay",
// "Episode-11": "The Pancake Batter Anomaly",
// "Episode-12": "The Jerusalem Duality",
// "Episode-13": "The Bat Jar Conjecture",
// "Episode-14": "The Nerdvana Annihilation",
// "Episode-15": "The Pork Chop Indeterminacy",
// "Episode-16": "The Peanut Reaction",
// "Episode-17": "The Tangerine Factor"
// },
// "Season-2": {
// "Episode-1": "The Bad Fish Paradigm",
// "Episode-2": "The Codpiece Topology",
// "Episode-3": "The Barbarian Sublimation",
// "Episode-4": "The Griffin Equivalency",
// "Episode-5": "The Euclid Alternative",
// "Episode-6": "The Cooper-Nowitzki Theorem",
// "Episode-7": "The Panty Pi\u00f1ata Polarization",
// "Episode-8": "The Lizard-Spock Expansion",
// "Episode-9": "The White Asparagus Triangulation",
// "Episode-10": "The Vartabedian Conundrum",
// "Episode-11": "The Bath Item Gift Hypothesis",
// "Episode-12": "The Killer Robot Instability",
// "Episode-13": "The Friendship Algorithm",
// "Episode-14": "The Financial Permeability",
// "Episode-15": "The Maternal Capacitance",
// "Episode-16": "The Cushion Saturation",
// "Episode-17": "The Terminator Decoupling",
// "Episode-18": "The Work Song Nanocluster",
// "Episode-19": "The Dead Hooker Juxtaposition",
// "Episode-20": "The Hofstadter Isotope",
// "Episode-21": "The Vegas Renormalization",
// "Episode-22": "The Classified Materials Turbulence",
// "Episode-23": "The Monopolar Expedition"
// },
// "Season-3": {
// "Episode-1": "The Electric Can Opener Fluctuation",
// "Episode-2": "The Jiminy Conjecture",
// "Episode-3": "The Gothowitz Deviation",
// "Episode-4": "The Pirate Solution",
// "Episode-5": "The Creepy Candy Coating Corollary",
// "Episode-6": "The Cornhusker Vortex",
// "Episode-7": "The Guitarist Amplification",
// "Episode-8": "The Adhesive Duck Deficiency",
// "Episode-9": "The Vengeance Formulation",
// "Episode-10": "The Gorilla Experiment",
// "Episode-11": "The Maternal Congruence",
// "Episode-12": "The Psychic Vortex",
// "Episode-13": "The Bozeman Reaction",
// "Episode-14": "The Einstein Approximation",
// "Episode-15": "The Large Hadron Collision",
// "Episode-16": "The Excelsior Acquisition",
// "Episode-17": "The Precious Fragmentation",
// "Episode-18": "The Pants Alternative",
// "Episode-19": "The Wheaton Recurrence",
// "Episode-20": "The Spaghetti Catalyst",
// "Episode-21": "The Plimpton Stimulation",
// "Episode-22": "The Staircase Implementation",
// "Episode-23": "The Lunar Excitation"
// },
// "Season-4": {
// "Episode-1": "The Robotic Manipulation",
// "Episode-2": "The Cruciferous Vegetable Amplification",
// "Episode-3": "The Zazzy Substitution",
// "Episode-4": "The Hot Troll Deviation",
// "Episode-5": "The Desperation Emanation",
// "Episode-6": "The Irish Pub Formulation",
// "Episode-7": "The Apology Insufficiency",
// "Episode-8": "The 21-second Excitation",
// "Episode-9": "The Boyfriend Complexity",
// "Episode-10": "The Alien Parasite Hypothesis",
// "Episode-11": "The Justice League Recombination",
// "Episode-12": "The Bus Pants Utilization",
// "Episode-13": "The Love Car Displacement",
// "Episode-14": "The Thespian Catalyst",
// "Episode-15": "The Benefactor Factor",
// "Episode-16": "The Cohabitation Formulation",
// "Episode-17": "The Toast Derivation",
// "Episode-18": "The Prestidigitation Approximation",
// "Episode-19": "The Zarnecki Incursion",
// "Episode-20": "The Herb Garden Germination",
// "Episode-21": "The Agreement Dissection",
// "Episode-22": "The Wildebeest Implementation",
// "Episode-23": "The Engagement Reaction",
// "Episode-24": "The Roommate Transmogrification"
// },
// "Season-5": {
// "Episode-1": "The Skank Reflex Analysis",
// "Episode-2": "The Infestation Hypothesis",
// "Episode-3": "The Pulled Groin Extrapolation",
// "Episode-4": "The Wiggly Finger Catalyst",
// "Episode-5": "The Russian Rocket Reaction",
// "Episode-6": "The Rhinitis Revelation",
// "Episode-7": "The Good Guy Fluctuation",
// "Episode-8": "The Isolation Permutation",
// "Episode-9": "The Ornithophobia Diffusion",
// "Episode-10": "The Flaming Spittoon Acquisition",
// "Episode-11": "The Speckerman Recurrence",
// "Episode-12": "The Shiny Trinket Maneuver",
// "Episode-13": "The Recombination Hypothesis",
// "Episode-14": "The Beta Test Initiation",
// "Episode-15": "The Friendship Contraction",
// "Episode-16": "The Vacation Solution",
// "Episode-17": "The Rothman Disintegration",
// "Episode-18": "The Werewolf Transformation",
// "Episode-19": "The Weekend Vortex",
// "Episode-20": "The Transporter Malfunction",
// "Episode-21": "The Hawking Excitation",
// "Episode-22": "The Stag Convergence",
// "Episode-23": "The Launch Acceleration",
// "Episode-24": "The Countdown Reflection"
// },
// "Season-6": {
// "Episode-1": "The Date Night Variable",
// "Episode-2": "The Decoupling Fluctuation",
// "Episode-3": "The Higgs Boson Observation",
// "Episode-4": "The Re-Entry Minimization",
// "Episode-5": "The Holographic Excitation",
// "Episode-6": "The Extract Obliteration",
// "Episode-7": "The Habitation Configuration",
// "Episode-8": "The 43 Peculiarity",
// "Episode-9": "The Parking Spot Escalation",
// "Episode-10": "The Fish Guts Displacement",
// "Episode-11": "The Santa Simulation",
// "Episode-12": "The Egg Salad Equivalency",
// "Episode-13": "The Bakersfield Expedition",
// "Episode-14": "The Cooper\/Kripke Inversion",
// "Episode-15": "The Spoiler Alert Segmentation",
// "Episode-16": "The Tangible Affection Proof",
// "Episode-17": "The Monster Isolation",
// "Episode-18": "The Contractual Obligation Implementation",
// "Episode-19": "The Closet Reconfiguration",
// "Episode-20": "The Tenure Turbulence",
// "Episode-21": "The Closure Alternative",
// "Episode-22": "The Proton Resurgence",
// "Episode-23": "The Love Spell Potential",
// "Episode-24": "The Bon Voyage Reaction"
// },
// "Season-7": {
// "Episode-1": "The Hofstadter Insufficiency",
// "Episode-2": "The Deception Verification",
// "Episode-3": "The Scavenger Vortex",
// "Episode-4": "The Raiders Minimization",
// "Episode-5": "The Workplace Proximity",
// "Episode-6": "The Romance Resonance",
// "Episode-7": "The Proton Displacement",
// "Episode-8": "The Itchy Brain Simulation",
// "Episode-9": "The Thanksgiving Decoupling",
// "Episode-10": "The Discovery Dissipation",
// "Episode-11": "The Cooper Extraction",
// "Episode-12": "The Hesitation Ramification",
// "Episode-13": "The Occupation Recalibration",
// "Episode-14": "The Convention Conundrum",
// "Episode-15": "The Locomotive Manipulation",
// "Episode-16": "The Table Polarization",
// "Episode-17": "The Friendship Turbulence",
// "Episode-18": "The Mommy Observation",
// "Episode-19": "The Indecision Amalgamation",
// "Episode-20": "The Relationship Diremption",
// "Episode-21": "The Anything Can Happen Recurrence",
// "Episode-22": "The Proton Transmogrification",
// "Episode-23": "The Gorilla Dissolution",
// "Episode-24": "The Status Quo Combustion"
// },
// "Season-8": {
// "Episode-1": "The Locomotion Interruption",
// "Episode-2": "The Junior Professor Solution",
// "Episode-3": "The First Pitch Insufficiency",
// "Episode-4": "The Hook-Up Reverberation",
// "Episode-5": "The Focus Attenuation",
// "Episode-6": "The Expedition Approximation",
// "Episode-7": "The Misinterpretation Agitation",
// "Episode-8": "The Prom Equivalency",
// "Episode-9": "The Septum Deviation",
// "Episode-10": "The Champagne Reflection",
// "Episode-11": "The Clean Room Infiltration",
// "Episode-12": "The Space Probe Disintegration",
// "Episode-13": "The Anxiety Optimization",
// "Episode-14": "The Troll Manifestation",
// "Episode-15": "The Comic Book Store Regeneration",
// "Episode-16": "The Intimacy Acceleration",
// "Episode-17": "The Colonization Application",
// "Episode-18": "The Leftover Thermalization",
// "Episode-19": "The Skywalker Incursion",
// "Episode-20": "The Fortification Implementation",
// "Episode-21": "The Communication Deterioration",
// "Episode-22": "The Graduation Transmission",
// "Episode-23": "The Maternal Combustion",
// "Episode-24": "The Commitment Determination"
// },
// "Season-9": {
// "Episode-1": "The Matrimonial Momentum",
// "Episode-2": "The Separation Oscillation",
// "Episode-3": "The Bachelor Party Corrosion",
// "Episode-4": "The 2003 Approximation",
// "Episode-5": "The Perspiration Implementation",
// "Episode-6": "The Helium Insufficiency",
// "Episode-7": "The Spock Resonance",
// "Episode-8": "The Mystery Date Observation",
// "Episode-9": "The Platonic Permutation",
// "Episode-10": "The Earworm Reverberation",
// "Episode-11": "The Opening Night Excitation",
// "Episode-12": "The Sales Call Sublimation",
// "Episode-13": "The Empathy Optimization",
// "Episode-14": "The Meemaw Materialization",
// "Episode-15": "The Valentino Submergence",
// "Episode-16": "The Positive Negative Reaction",
// "Episode-17": "The Celebration Experimentation",
// "Episode-18": "The Application Deterioration",
// "Episode-19": "The Solder Excursion Diversion",
// "Episode-20": "The Big Bear Precipitation",
// "Episode-21": "The Viewing Party Combustion",
// "Episode-22": "The Fermentation Bifurcation",
// "Episode-23": "The Line Substitution Solution",
// "Episode-24": "The Convergence Convergence"
// }
// }
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment