initial commit

This commit is contained in:
codepoet80 committed 2026-03-23 10:05:41 -04:00
commit 721332122d
17 files changed
+795

No files matched your search

+2
View File
@@ -0,0 +1,2 @@
secrets.php
app/
+23
View File
@@ -0,0 +1,23 @@
<?php
include "../../database.php";
$conn = connectDB();
header('Content-Type: application/json');
$sql = "SELECT * FROM tbl_genres ORDER BY genre";
$stmt = $conn->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);
?>
+51
View File
@@ -0,0 +1,51 @@
<?php
include "../../../database.php";
$conn = connectDB();
$genre = "";
if (isset($_GET["genre"]))
$genre = $_GET["genre"];
if ($genre == "") {
die ("specify genre in querystring");
}
$take = 10;
if (isset($_GET["take"]) && is_numeric($_GET["take"]))
$take = (int)$_GET["take"];
if ($take > 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 "[]";
}
?>
+28
View File
@@ -0,0 +1,28 @@
<?php
include "../../../database.php";
$conn = connectDB();
$query = "";
if (isset($_GET["query"]))
$query = $_GET["query"];
if ($query == "") {
die ("specify query in querystring");
}
$take = 10;
if (isset($_GET["take"]) && is_numeric($_GET["take"]))
$take = (int)$_GET["take"];
if ($take > 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);
?>
+31
View File
@@ -0,0 +1,31 @@
<?php
include "../../../database.php";
$conn = connectDB();
$year = 0;
if (isset($_GET["year"]) && is_numeric($_GET["year"]))
$year = $_GET["year"];
if ($year == 0) {
die ("specify year in querystring");
}
$take = 10;
if (isset($_GET["take"]) && is_numeric($_GET["take"]))
$take = (int)$_GET["take"];
if ($take > 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);
?>
+24
View File
@@ -0,0 +1,24 @@
<?php
include "../../database.php";
$conn = connectDB();
$take = 10;
if (isset($_GET["take"]) && is_numeric($_GET["take"]))
$take = (int)$_GET["take"];
if ($take > 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);
?>
+20
View File
@@ -0,0 +1,20 @@
<?php
include "../../database.php";
$conn = connectDB();
header('Content-Type: application/json');
$sql = "SELECT UNIQUE year FROM tbl_movies ORDER BY year";
$stmt = $conn->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);
?>
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 1000 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

View File
Whitespace-only changes.
+1
View File
@@ -0,0 +1 @@
0
+23
View File
@@ -0,0 +1,23 @@
<?php
include_once "secrets.php";
function connectDB() {
global $dbserver, $dbuser, $dbpasswd;
$conn;
try {
if (!isset($dbserver))
die("Database server not set.");
if (!isset($dbuser))
die("Database username not set.");
if (!isset($dbpasswd))
die("Database password not set.");
$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);
return $conn;
} catch(PDOException $e) {
echo "<br>Database Connection Error: " . $e->getMessage();
die();
}
}
?>
+133
View File
@@ -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
+90
View File
@@ -0,0 +1,90 @@
<!DOCTYPE html>
<html lang="en">
<?php
//This file is only used for advertising on a hosting webserver
//Figure out what protocol the client wanted
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
$PROTOCOL = "https";
} else {
$PROTOCOL = "http";
}
//App Details
$title = "Flixnet";
$subtitle = " | webOS Archive";
$description = "Flixnet is a public domain movie browser created and hosted by webOS Archive for retro and modern devices.";
$github = "https://github.com/webosarchive/flixnet-service";
$pwaLink = $PROTOCOL."://flixnet.webosarchive.org/app/";
$museumLink = $PROTOCOL."://appcatalog.webosarchive.org/app/Flixnet";
$homeLink = $PROTOCOL."://flixnet.webosarchive.org";
$icon = $homeLink."/assets/icon-128.png";
$hero = $homeLink."/assets/hero.png";
?>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="description" content="<?php echo $description; ?>">
<meta name="keywords" content="webos, firefoxos, pwa, rss">
<meta name="author" content="webOS Archive">
<meta property="og:title" content="<?php echo $title; ?>">
<meta property="og:description" content="<?php echo $description; ?>">
<meta property="og:image" content="<?php echo $hero; ?>">
<meta name="twitter:card" content="app">
<meta name="twitter:site" content="@webOSArchive">
<meta name="twitter:title" content="<?php echo $title; ?>">
<meta name="twitter:description" content="<?php echo $description; ?>">
<title><?php echo $title . $subtitle; ?></title>
<link id="favicon" rel="icon" type="image/png" sizes="64x64" href="<?php echo $icon;?>">
<link href="<?php echo $PROTOCOL . "://www.webosarchive.org/app-template/"?>web.css" rel="stylesheet" type="text/css" >
</head>
<body>
<?php
$docRoot = "./";
echo file_get_contents("https://www.webosarchive.org/menu.php?docRoot=" . $docRoot . "&protocol=" . $PROTOCOL);
?>
<table width="100%" border=0 style="width:100%;border:0px"><tr><td align="center" style="width:100%;height:100%;border:0px">
<div id="row">
<div id="content" align="left">
<h1><img src="<?php echo $icon;?>" width="60" height="60" alt=""/><?php echo $title; ?></h1>
<p><?php echo $description; ?></p>
<p>Available for most platforms as a Progressive Web App and the webOS App Museum for webOS devices.</p>
<p>View the source and contribute on <?php echo "<a href='" . $github . "'>GitHub</a>"?>.</p>
<p class="center">
<?php if (isset($pwaLink)) { ?>
<a class="download-link" href="<?php echo $pwaLink; ?>">
<img src="<?php echo $PROTOCOL . "://www.webosarchive.org/app-template/"?>pwa-badge.png" width="200" height="59" alt="Install the PWA" />
</a>
<?php } ?>
<?php if (isset($playLink)) { ?>
<a class="download-link" href="<?php echo $playLink; ?>">
<img src="<?php echo $PROTOCOL . "://www.webosarchive.org/app-template/"?>play-badge.png" width="200" height="59" alt="Get it on Google play" />
</a>
<?php } ?>
<?php if (isset($museumLink)) { ?>
<a class="download-link" href="<?php echo $museumLink; ?>">
<img src="<?php echo $PROTOCOL . "://www.webosarchive.org/app-template/"?>museum-badge.png" width="200" height="59" alt="Find it in the App Museum" />
</a>
<?php } ?>
</p>
</div>
<div id="hero">
<img src="assets/hero.png" width="480" alt="<?php echo $title ?>" />
</div>
</div>
<div id="footer">
&copy; webOSArchive.
<div id="footer-links">
<a href="<?php echo $PROTOCOL . "://www.webosarchive.org/privacy.html"?>">Privacy Policy</a>
</div>
</div>
</td></tr></table>
</body>
</html>
+58
View File
@@ -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
+6
View File
@@ -0,0 +1,6 @@
<?php
$dbserver = "localhost";
$dbuser = "your_db_username";
$dbpasswd = "your_db_password";
$apiKey = "your_tmdb_api_key";
?>
+305
View File
@@ -0,0 +1,305 @@
<h2>Movie Importer</h2>
<form method="get">
<?php
$allowed = ['127.0.0.1', '::1'];
if (!in_array($_SERVER['REMOTE_ADDR'], $allowed)) {
http_response_code(403);
die('Forbidden');
}
include "../secrets.php";
echo "Checking environment...";
$errors = [];
$resumefile = '../data/resume.txt';
$resume;
if (isset($_GET["resume"]))
$resume = $_GET["resume"];
// Open the file to get existing content
$rd = "";
if (file_exists($resumefile)) {
$resume = file_get_contents($resumefile);
echo "Read OK!";
$rd = "disabled";
} else {
try {
file_put_contents($resumefile, 0);
if (!file_exists($resumefile))
throw new Exception ("Could not create file");
echo "Create OK!";
} catch(Exception $ex) {
echo ("<li>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: <input type='text' name='resume' value='" . htmlspecialchars($resume, ENT_QUOTES) . "' " . $rd .">" . eol();
$md = "";
$max = 25; //Number of movies to load each pass
if (isset($_GET["max"])) {
$max = $_GET["max"];
$md = "disabled";
}
echo "Max/Pass: <input type='text' name='max' value='" . htmlspecialchars($max, ENT_QUOTES) . "' " . $md .">" . eol();
$cooldown = 2; //Delay between reloading page to start another pass
$dd = "";
if (isset($_GET["cooldown"])) {
$cooldown = $_GET["cooldown"];
$dd = "disabled";
}
echo "Cooldown: <input type='text' name='cooldown' value='" . htmlspecialchars($cooldown, ENT_QUOTES) . "' " . $dd .">" . eol();
$go = false; //User confirms ready
if (isset($_GET["go"]))
$go = $_GET["go"];
echo "<input type='hidden' name='go' value='true'>" . 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 "<br>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 "<li>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 "<input type='Submit' value='Ready!'>" . PHP_EOL;
?>
</form>
<?php
$lookupURL = "https://api.themoviedb.org/3/find/%?api_key=" . $apiKey . "&external_source=imdb_id";
//Documentation on image paths: https://www.themoviedb.org/talk/5aeaaf56c3a3682ddf0010de
$backdropURL = "https://image.tmdb.org/t/p/w300/"; //or w154
$posterURL = "https://image.tmdb.org/t/p/w154/"; //or w92
if (!$go) {
// echo "<a href='"
}
if ($go) {
echo "<hr>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 "<hr>";
$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 "<hr>";
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 . "<script>window.setTimeout('document.location.reload()', " . ($cooldown*1000) . ")</script>";
} 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 . "<script>window.setTimeout('document.location=\"" . $newUrl . "\"', " . ($cooldown*1000) . ")</script>";
}
}
echo "<p></p>";
}
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 "<br>" . PHP_EOL;
}
?>