commit 721332122df142db87aa746a43e994461df73e6c Author: codepoet80 Date: Mon Mar 23 10:05:41 2026 -0400 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2486a08 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +secrets.php +app/ \ No newline at end of file diff --git a/api/genres/index.php b/api/genres/index.php new file mode 100644 index 0000000..d8663a8 --- /dev/null +++ b/api/genres/index.php @@ -0,0 +1,23 @@ +prepare($sql); +$stmt->execute(); +$genres = $stmt->fetchAll(PDO::FETCH_OBJ); + +foreach($genres as $genre) { + $sql = "SELECT count(distinct tbl_movies.title) as total, tbl_movie_genres.genre_id FROM tbl_movies + INNER JOIN tbl_movie_genres ON tbl_movie_genres.movie_id = tbl_movies.id + WHERE genre_id=:genreid"; + $stmt = $conn->prepare($sql); + $stmt->bindValue(':genreid', $genre->id); + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_OBJ); + $genre->count = $result[0]->total; +} +echo json_encode($genres); + +?> \ No newline at end of file diff --git a/api/movies/bygenre/index.php b/api/movies/bygenre/index.php new file mode 100644 index 0000000..4fc08ec --- /dev/null +++ b/api/movies/bygenre/index.php @@ -0,0 +1,51 @@ + 100) + $take = 100; +$skip = 0; +if (isset($_GET["skip"]) && is_numeric($_GET["skip"])) + $skip = (int)$_GET["skip"]; + +header('Content-Type: application/json'); +if (is_numeric($genre)) + $sql = "SELECT * FROM tbl_genres WHERE id=:genre"; +else + $sql = "SELECT * FROM tbl_genres WHERE genre LIKE :genre"; +$stmt = $conn->prepare($sql); +$stmt->bindValue(':genre', $genre); +$stmt->execute(); +$genres = $stmt->fetchAll(PDO::FETCH_OBJ); +if (count($genres) > 0) { + $genreid = $genres[0]->id; + + $sql = "SELECT tbl_movies.*, tbl_movie_genres.genre_id FROM tbl_movies + INNER JOIN tbl_movie_genres ON tbl_movie_genres.movie_id = tbl_movies.id + WHERE genre_id=:genreid "; + if (!isset($_GET["skip"])) + $sql .= " ORDER BY RAND() "; + else + $sql .= " ORDER BY title "; + $sql .= " LIMIT " . $take; + if (isset($_GET["skip"])) + $sql .= " OFFSET " . $skip; + + $stmt = $conn->prepare($sql); + $stmt->bindValue(':genreid', $genreid); + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_OBJ); + echo json_encode($result); +} else { + echo "[]"; +} +?> \ No newline at end of file diff --git a/api/movies/bytitle/index.php b/api/movies/bytitle/index.php new file mode 100644 index 0000000..36dbe75 --- /dev/null +++ b/api/movies/bytitle/index.php @@ -0,0 +1,28 @@ + 100) + $take = 100; +$skip = 0; +if (isset($_GET["skip"]) && is_numeric($_GET["skip"])) + $skip = (int)$_GET["skip"]; + +header('Content-Type: application/json'); +$sql = "SELECT * FROM tbl_movies WHERE title LIKE :query ORDER BY title LIMIT " . $take . " OFFSET " . $skip; + +$stmt = $conn->prepare($sql); +$stmt->bindValue(':query', $query . "%"); +$stmt->execute(); +$result = $stmt->fetchAll(PDO::FETCH_OBJ); +echo json_encode($result); +?> \ No newline at end of file diff --git a/api/movies/byyear/index.php b/api/movies/byyear/index.php new file mode 100644 index 0000000..4b75b1b --- /dev/null +++ b/api/movies/byyear/index.php @@ -0,0 +1,31 @@ + 100) + $take = 100; +$skip = 0; +if (isset($_GET["skip"]) && is_numeric($_GET["skip"])) + $skip = (int)$_GET["skip"]; + +header('Content-Type: application/json'); +if (!isset($_GET["skip"])) + $sql = "SELECT * FROM tbl_movies WHERE year=:year ORDER BY RAND() LIMIT " . $take; +else + $sql = "SELECT * FROM tbl_movies WHERE year=:year ORDER BY title LIMIT " . $take . " OFFSET " . $skip; + +$stmt = $conn->prepare($sql); +$stmt->bindValue(':year', $year); +$stmt->execute(); +$result = $stmt->fetchAll(PDO::FETCH_OBJ); +echo json_encode($result); +?> \ No newline at end of file diff --git a/api/movies/index.php b/api/movies/index.php new file mode 100644 index 0000000..12a99f9 --- /dev/null +++ b/api/movies/index.php @@ -0,0 +1,24 @@ + 100) + $take = 100; +$skip = 0; +if (isset($_GET["skip"]) && is_numeric($_GET["skip"])) + $skip = (int)$_GET["skip"]; + +header('Content-Type: application/json'); +if (!isset($_GET["skip"])) + $sql = "SELECT * FROM tbl_movies ORDER BY RAND() LIMIT " . $take; +else + $sql = "SELECT * FROM tbl_movies LIMIT " . $take . " OFFSET " . $skip; + +$stmt = $conn->prepare($sql); +$stmt->execute(); +$result = $stmt->fetchAll(PDO::FETCH_OBJ); +echo json_encode($result); +?> \ No newline at end of file diff --git a/api/years/index.php b/api/years/index.php new file mode 100644 index 0000000..fd9a142 --- /dev/null +++ b/api/years/index.php @@ -0,0 +1,20 @@ +prepare($sql); +$stmt->execute(); +$years = $stmt->fetchAll(PDO::FETCH_OBJ); + +foreach ($years as $year) { + $sql = "SELECT COUNT(DISTINCT title) as total FROM tbl_movies where year=:year"; + $stmt = $conn->prepare($sql); + $stmt->bindValue(':year', $year->year); + $stmt->execute(); + $counts = $stmt->fetchAll(PDO::FETCH_OBJ); + $year->count = $counts[0]->total; +} +echo json_encode($years); +?> \ No newline at end of file diff --git a/assets/hero.png b/assets/hero.png new file mode 100644 index 0000000..7122362 Binary files /dev/null and b/assets/hero.png differ diff --git a/assets/icon-128.png b/assets/icon-128.png new file mode 100644 index 0000000..fed69d4 Binary files /dev/null and b/assets/icon-128.png differ diff --git a/data/movies.json b/data/movies.json new file mode 100644 index 0000000..e69de29 diff --git a/data/resume.txt b/data/resume.txt new file mode 100755 index 0000000..c227083 --- /dev/null +++ b/data/resume.txt @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/database.php b/database.php new file mode 100644 index 0000000..245b98a --- /dev/null +++ b/database.php @@ -0,0 +1,23 @@ +setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + return $conn; + } catch(PDOException $e) { + echo "
Database Connection Error: " . $e->getMessage(); + die(); + } +} +?> \ No newline at end of file diff --git a/flixnet.sql b/flixnet.sql new file mode 100644 index 0000000..e343680 --- /dev/null +++ b/flixnet.sql @@ -0,0 +1,133 @@ +-- MariaDB dump 10.19-11.3.2-MariaDB, for Linux (aarch64) +-- +-- Host: localhost Database: flixnet +-- ------------------------------------------------------ +-- Server version 11.3.2-MariaDB + +/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; +/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; +/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; +/*!40101 SET NAMES utf8mb4 */; +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; + +-- +-- Table structure for table `tbl_genres` +-- + +DROP TABLE IF EXISTS `tbl_genres`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `tbl_genres` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `genre` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `tbl_genres` +-- + +LOCK TABLES `tbl_genres` WRITE; +/*!40000 ALTER TABLE `tbl_genres` DISABLE KEYS */; +/*!40000 ALTER TABLE `tbl_genres` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `tbl_movie_genres` +-- + +DROP TABLE IF EXISTS `tbl_movie_genres`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `tbl_movie_genres` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `movie_id` int(11) NOT NULL, + `genre_id` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=257 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `tbl_movie_genres` +-- + +LOCK TABLES `tbl_movie_genres` WRITE; +/*!40000 ALTER TABLE `tbl_movie_genres` DISABLE KEYS */; +/*!40000 ALTER TABLE `tbl_movie_genres` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `tbl_movie_related` +-- + +DROP TABLE IF EXISTS `tbl_movie_related`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `tbl_movie_related` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `from_imdb_id` varchar(30) NOT NULL, + `to_imdb_id` varchar(30) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=1346 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `tbl_movie_related` +-- + +LOCK TABLES `tbl_movie_related` WRITE; +/*!40000 ALTER TABLE `tbl_movie_related` DISABLE KEYS */; +/*!40000 ALTER TABLE `tbl_movie_related` ENABLE KEYS */; +UNLOCK TABLES; + +-- +-- Table structure for table `tbl_movies` +-- + +DROP TABLE IF EXISTS `tbl_movies`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `tbl_movies` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `imdb_id` varchar(30) DEFAULT NULL, + `tmdb_id` varchar(30) DEFAULT NULL, + `title` varchar(255) NOT NULL, + `description` longtext DEFAULT NULL, + `year` int(11) DEFAULT NULL, + `runtime` varchar(255) DEFAULT NULL, + `rating` decimal(10,0) DEFAULT NULL, + `language` varchar(30) DEFAULT NULL, + `is_adult` bit(1) DEFAULT b'0', + `identifier` varchar(255) DEFAULT NULL, + `moviepath` varchar(255) DEFAULT NULL, + `poster` varchar(255) DEFAULT NULL, + `backdrop` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=144 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + +-- +-- Dumping data for table `tbl_movies` +-- + +LOCK TABLES `tbl_movies` WRITE; +/*!40000 ALTER TABLE `tbl_movies` DISABLE KEYS */; +/*!40000 ALTER TABLE `tbl_movies` ENABLE KEYS */; +UNLOCK TABLES; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; +/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; +/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +-- Dump completed on 2024-02-24 9:33:30 diff --git a/index.php b/index.php new file mode 100644 index 0000000..fe663f3 --- /dev/null +++ b/index.php @@ -0,0 +1,90 @@ + + + + + + + + + + + + + + + + + + + + + <?php echo $title . $subtitle; ?> + + + web.css" rel="stylesheet" type="text/css" > + + + + +
+
+
+

+

+

Available for most platforms as a Progressive Web App and the webOS App Museum for webOS devices.

+

View the source and contribute on GitHub"?>.

+

+ + + pwa-badge.png" width="200" height="59" alt="Install the PWA" /> + + + + + play-badge.png" width="200" height="59" alt="Get it on Google play" /> + + + + + museum-badge.png" width="200" height="59" alt="Find it in the App Museum" /> + + +

+
+
+ <?php echo $title ?> +
+
+ +
+ + diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..16b6da9 --- /dev/null +++ b/readme.md @@ -0,0 +1,58 @@ +# Public Domain Movie API + +## Using a client + +A client for this service exists as a cross-platform PWA, primarily targeting legacy webOS (mobile) devices, but available for other platforms if hosted on a web server. You can check it out here: [Flixnet Client](https://github.com/webOSArchive/enyo1-flixnet). + +- you probably want security on the utils folder +- `utils/import-movies.php` works best if it can write to a text file called resume.txt and a movie file called `../data/movies.json` +- movie data comes from: https://github.com/casbah-ma/cinedantan/ +- extra metadata comes from TMDB +- movies come from Archive.org + +## Create the Database + +Use a mysql compatible server (I used mariadb) + +- `CREATE DATABASE flixnet;` +- `USE flixnet;` +- `SOURCE /path/to/flixnet.sql` +- `CREATE USER 'dev'@'localhost' IDENTIFIED BY 'Str0ngP@ssword';` +- `GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON flixnet.* TO 'dev'@'localhost' IDENTIFIED BY 'Str0ngP@ssword';` +- `FLUSH PRIVILEGES` + +## API + +### /api/movies + +- If no query string is set, returns 10 random movies +- If only `take` is set to a numeric value, returns that number of random movies (max 100) +- If `skip` is set to a numeric value, returns either 10 or `take` number of movies in order, starting at the position indicated by skip + +### /api/movies/bygenre + +- **Required** query string parameter is `genre` followed by *either* the name (string) or id (numeric) of the genre +- If only `take` is set to a numeric value, returns that number of random movies (max 100) in that specified genre +- If `skip` is set to a numeric value, returns either 10 or `take` number of movies in order, starting at the position indicated by skip + +### /api/movies/bytitle + +- **Required** query string parameter is `query` followed by the string to search for in movie titles + - Search is performed with a wildcard following the search string +- If only `take` is set to a numeric value, returns that number of random movies (max 100) with a matching title +- If `skip` is set to a numeric value, returns either 10 or `take` number of movies in order, starting at the position indicated by skip + +### /api/movies/byyear + +- **Required** query string parameter is `year` and must be numeric + - Search is performed with a wildcard following the search string +- If only `take` is set to a numeric value, returns that number of random movies (max 100) in the given year +- If `skip` is set to a numeric value, returns either 10 or `take` number of movies in order, starting at the position indicated by skip + +### /api/genres + +- Returns an array of the known genres in alphabetical order, including a count of movies in that genre + +### /api/years + +- Returns an array of the known years in numerical order, including a count of movies in that year \ No newline at end of file diff --git a/secrets-example.php b/secrets-example.php new file mode 100644 index 0000000..58130cf --- /dev/null +++ b/secrets-example.php @@ -0,0 +1,6 @@ + diff --git a/utils/import-movies.php b/utils/import-movies.php new file mode 100644 index 0000000..85bc295 --- /dev/null +++ b/utils/import-movies.php @@ -0,0 +1,305 @@ +

Movie Importer

+
+Warning, could not create resume file, import will not survive browser window close. To resolve, provide write access to: " . $resumefile); + } +} +if (isset($_GET["resume"])) + $rd = "disabled"; +eol(); + +//Determine config +echo "Resume: " . eol(); + +$md = ""; +$max = 25; //Number of movies to load each pass +if (isset($_GET["max"])) { + $max = $_GET["max"]; + $md = "disabled"; +} +echo "Max/Pass: " . eol(); + +$cooldown = 2; //Delay between reloading page to start another pass +$dd = ""; +if (isset($_GET["cooldown"])) { + $cooldown = $_GET["cooldown"]; + $dd = "disabled"; +} +echo "Cooldown: " . eol(); + +$go = false; //User confirms ready +if (isset($_GET["go"])) + $go = $_GET["go"]; +echo "" . eol(); + +//Check for API key +echo "Checking credentials..."; +if (isset($apiKey)) { + echo "OK"; +} else { + die("TMDB API Key missing or not set"); +} +eol(); + +//Make sure database connections works +echo "Checking Database..."; +$conn; +try { + $conn = new PDO("mysql:host=$dbserver;dbname=flixnet", $dbuser, $dbpasswd); + // set the PDO error mode to exception + $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + echo "OK"; +} catch(PDOException $e) { + echo "
Error: " . $e->getMessage(); + die(); +} +eol(); + +//Make sure we can get the data file +echo "Checking Data file..."; +if (file_exists("../data/movies.json")) { + $data = file_get_contents ("../data/movies.json"); +} +if ($data != "") { + echo "OK"; +} else { + $data = getData("https://raw.githubusercontent.com/casbah-ma/cinedantan/master/public/database/movies.json"); + try { + file_put_contents("../data/movies.json", $data); + $check = file_get_contents("../data/movies.json"); + if ($check == "" || $check != $data) + throw new Exception("Could not populate movie file"); + echo "OK"; + } catch (Exception $ex) { + echo "
  • Warning: could not load or save movie data, it will be fetched from the remote server repeatedly. You may need to set a lower max to avoid timeouts. Make data/movies.json writeable to avoid this condition."; + } +} +eol(); + +if (!$go) + echo "" . PHP_EOL; +?> +
  • +Importing Movies..."; + + $movies = json_decode($data, true); + $count = 0; + $total = count($movies); + echo "Parsing " . $max . " of " . $total . " movies starting at " . $resume . " (" . round(($resume / $total)*100) . "% complete)" . eol(); + echo "
    "; + $useMax = $max + $resume; + foreach ($movies as $movie) { + if ($count >= $resume) { + echo "Processing movie #" . $count . ": " . $movie['title'] . eol(); + $imdbid = $movie['imdb']; + if (isset($imdbid) && $imdbid != "") { + + $sql = "SELECT * FROM tbl_movies WHERE imdb_id=:i"; + $stmt = $conn->prepare($sql); + $stmt->bindValue(':i', $imdbid); + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_OBJ); + + if ($result) { + echo " - Movie already exists, skipping!" . eol(); + } else { + $tmdbURL = str_replace("%", $imdbid, $lookupURL); + $tmdbInfo = getData($tmdbURL); + if (isset($tmdbInfo)) { + $tmdbData = json_decode($tmdbInfo); + //echo " - backdrop: " . $backdropURL . $tmdbData->movie_results[0]->backdrop_path . eol(); + //echo " - poster: " . $posterURL . $tmdbData->movie_results[0]->poster_path . eol(); + $useFile = pickMovieFile($movie['aoFiles']); + if ($useFile == "") { + echo " - Compatible movie file not found, skipping!" . eol(); + } else { + try { + // Insert the movie record + $sql = "INSERT INTO tbl_movies (imdb_id, tmdb_id, title, description, year, runtime, rating, language, + is_adult, identifier, moviepath, poster, backdrop) values (:imdbid, :tmdbid, :title, :story, + :year, :runtime, :rating, :language, :adult, :identifier, :moviepath, :poster, :backdrop)"; + $stmt = $conn->prepare($sql); + $stmt->bindValue(':imdbid', $imdbid); + $stmt->bindValue(':tmdbid', $tmdbData->movie_results[0]->id); + $stmt->bindValue(':title', $movie['title']); + $stmt->bindValue(':story', $movie['story']); + $stmt->bindValue(':year', $movie['year']); + $stmt->bindValue(':runtime', $movie['runtime']); + $stmt->bindValue(':rating', $movie['rating']); + $stmt->bindValue(':language', $tmdbData->movie_results[0]->original_language); + $stmt->bindValue(':adult', $tmdbData->movie_results[0]->adult); + $stmt->bindValue(':identifier', $movie['identifier']); + $stmt->bindValue(':moviepath', $useFile); + $stmt->bindValue(':backdrop', $backdropURL . $tmdbData->movie_results[0]->backdrop_path); + $stmt->bindValue(':poster', $posterURL . $tmdbData->movie_results[0]->poster_path); + $stmt->execute(); + //TODO - handle bad data + $movieid = $conn->lastInsertId(); + + // Inserted related movie records + foreach ($movie['related'] as $related) { + $sql = "INSERT INTO tbl_movie_related (from_imdb_id, to_imdb_id) value (:from, :to)"; + $stmt = $conn->prepare($sql); + $stmt->bindValue(':from', $imdbid); + $stmt->bindValue(':to', $related); + $stmt->execute(); + } + + // Inserted related movie records + foreach ($movie['genre'] as $genre) { + $genreid = selectOrCreateGenreRecord($genre, $conn); + if (!$genreid) { + //some movies have no genre + } else { + $sql = "INSERT INTO tbl_movie_genres (movie_id, genre_id) value (:movieid, :genreid)"; + $stmt = $conn->prepare($sql); + $stmt->bindValue(':movieid', $movieid); + $stmt->bindValue(':genreid', $genreid); + $stmt->execute(); + } + } + echo "- Inserted movie id: " . $movieid . " using file " . $useFile . eol(); + } catch (Exception $ex) { + echo "- ERROR inserting movie: " . $movie['title'] . eol(); + array_push($errors, $movie['title']); + } + } + } + } + eol(); + } + } + $count++; + if ($count >= $useMax) { + $resume = $count; + break; + } + } + echo "
    "; + if ($resume >= $total || $count >= $total) { + echo "All done processing movies!" . eol(); + if (count($errors) > 0) { + echo "Movies with errors..."; + foreach ($errors as $error) { + echo "- " . $error . eol(); + } + } + file_put_contents($resumefile, 0); + } else { + echo "Continuing processing movies at " . $resume . " in " . $cooldown . " seconds..." . eol(); + try { + file_put_contents($resumefile, $resume); + if (!file_exists($resumefile)) + throw new Exception("Could not update resume file"); + echo PHP_EOL . ""; + } catch(Exception $ex) { + $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http'; + $newUrl = $protocol."://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; + $newUrl = explode("?", $newUrl)[0]; + $newUrl = $newUrl . "?go=true&resume=" . $resume . "&max=" . $max . "&cooldown=" . $cooldown; + echo PHP_EOL . ""; + } + } + echo "

    "; +} + +function getData($url) { + $ch = curl_init($url); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + + $response = curl_exec($ch); + if(curl_error($ch)) { + echo curl_error($ch); + } + curl_close($ch); + return $response; +} + +function pickMovieFile($movieFiles) { + $useFile = ""; + foreach ($movieFiles as $aoFile) { + if (strtolower($aoFile['format']) == "mpeg4") { + $useFile = $aoFile['url']; + break; + } + if (strtolower($aoFile['format']) == "64kb mpeg4" && $useFile == "") { + $useFile = $aoFile['url']; + } + if (strtolower($aoFile['format']) == "256kb mpeg4" && ($useFile == "" || $useFile == "64kb mpeg4")) { + $useFile = $aoFile['url']; + } + if (strtolower($aoFile['format']) == "512kb mpeg4" && ($useFile == "" || $useFile == "64kb mpeg4" || $useFile == "256kb mpeg4")) { + $useFile = $aoFile['url']; + break; + } + if (strpos(strtolower($aoFile['format']), "mpeg4") !== false && $useFile == "") { + $useFile = $aoFile['url']; + } + } + return $useFile; +} + +function selectOrCreateGenreRecord($genre, $conn) { + $genre = strtolower($genre); + if ($genre != "") { + $genreid = null; + + $sql = "SELECT * FROM tbl_genres WHERE genre=:genre"; + $stmt = $conn->prepare($sql); + $stmt->bindValue(':genre', $genre); + $stmt->execute(); + $result = $stmt->fetchAll(PDO::FETCH_OBJ); + if ($result) { + return $result[0]->id; + } else { + $sql = "INSERT INTO tbl_genres (genre) value (:genre)"; + $stmt = $conn->prepare($sql); + $stmt->bindValue(':genre', $genre); + $stmt->execute(); + return $conn->lastInsertId(); + } + } else { + return false; + } +} + +function eol() { + echo "
    " . PHP_EOL; +} +?>