Skip to content

Commit 100516d

Browse files
committed
Option to cache film data
- saves new film data
1 parent 17d3016 commit 100516d

File tree

5 files changed

+116
-34
lines changed

5 files changed

+116
-34
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
vendor/*
2-
composer.lock
3-
test/index.php
2+
src/cache/*
3+
composer.lock

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ file or class name and description of purpose be included on the
186186
same "printed page" as the copyright notice for easier
187187
identification within third-party archives.
188188

189-
Copyright [yyyy] [name of copyright owner]
189+
Copyright 2019 Harry Merritt
190190

191191
Licensed under the Apache License, Version 2.0 (the "License");
192192
you may not use this file except in compliance with the License.

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111
],
1212
"type": "project",
1313
"require": {
14-
"php": ">=7.1",
15-
"paquettg/php-html-parser": "^2.1"
14+
"php": ">=7.1",
15+
"paquettg/php-html-parser": "^2.1",
16+
"tmarois/filebase": "1.0"
1617
},
1718
"require-dev": {
1819
"phpunit/phpunit": "6.*"

src/imdb.php

Lines changed: 109 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,85 @@
1616

1717
class Imdb {
1818

19-
/** @var string $imdb_base_url the base or 'pre-fix' for all imdb links */
20-
private $imdb_base_url = "https://www.imdb.com/";
21-
22-
23-
/**
24-
* Search IMDB for @param $query and return the all search results
25-
*
26-
* @param $query string - search string
27-
* @param $category string - category to search within (films || people)
28-
*
29-
* @return array
30-
*/
31-
public function search($query, $category="all") {
19+
20+
/**
21+
* Constructor function to set-up class variables
22+
*
23+
* @param $cache bool - cache film results
24+
* @param $imdb_base_url string - the base or 'pre-fix' for all imdb links
25+
*
26+
* @return none
27+
*/
28+
function __construct($cache=false, $imdb_base_url="https://www.imdb.com/") {
29+
$this->isCaching = $cache;
30+
$this->imdb_base_url = $imdb_base_url;
31+
}
32+
33+
34+
/**
35+
* Cache
36+
*
37+
* @param $operation string - operation to perform on the cache
38+
* @param $filmId string - imdb-id of film (ttxxxxxxx)
39+
* @param $filmData object - film data from imdb
40+
*
41+
* @return bool|object
42+
*/
43+
public function cache($operation, $filmId, $filmData=[]) {
44+
// Is cache enabled?
45+
if ($this->isCaching)
46+
{
47+
// Open cache database
48+
$db_cache = new \Filebase\Database([
49+
'dir' => __DIR__ . DIRECTORY_SEPARATOR . 'cache/films/',
50+
'backupLocation' => __DIR__ . DIRECTORY_SEPARATOR . 'cache/films/backups/',
51+
'format' => \Filebase\Format\Json::class,
52+
'cache' => true,
53+
'cache_expires' => 604800,
54+
'pretty' => false
55+
]);
56+
57+
// Get film data from cache
58+
$cache_filmData = $db_cache->get($filmId);
59+
60+
switch ($operation)
61+
{
62+
// Check if film has already been cached
63+
case "exists":
64+
if ($db_cache->has($filmId))
65+
{
66+
return true;
67+
}
68+
break;
69+
70+
// Get film data from cache
71+
case "get":
72+
return $cache_filmData->film;
73+
74+
// Update/add film data to cache
75+
case "update":
76+
// Add film data data to cache
77+
$cache_filmData->film = $filmData;
78+
$cache_filmData->save();
79+
break;
80+
}
81+
}
82+
else
83+
{
84+
return false;
85+
}
86+
}
87+
88+
89+
/**
90+
* Search IMDB for @param $query and return the all search results
91+
*
92+
* @param $query string - search string
93+
* @param $category string - category to search within (films || people)
94+
*
95+
* @return array
96+
*/
97+
public function search($query, $category="all") {
3298
// Replace all spaces with '+'
3399
$query = preg_replace("/\s/", "+", $query);
34100
// Create search url
@@ -95,7 +161,7 @@ public function search($query, $category="all") {
95161
}
96162
// Return resonse
97163
return $response;
98-
}
164+
}
99165

100166

101167
/**
@@ -108,6 +174,7 @@ public function search($query, $category="all") {
108174
public function film($query, $techSpecs=false) {
109175
// Define response array
110176
$response = [
177+
"id" => "",
111178
"title" => "",
112179
"year" => "",
113180
"length" => "",
@@ -129,7 +196,7 @@ public function film($query, $techSpecs=false) {
129196
{
130197
// String contains all numbers
131198
// Decide if $query is imdb-id or search string
132-
if (ctype_digit($query))
199+
if (ctype_digit($query) && strlen($query) === 7)
133200
{
134201
$filmId = "tt" . $query;
135202
} else
@@ -142,23 +209,34 @@ public function film($query, $techSpecs=false) {
142209
{
143210
// Use first film returned from search
144211
$filmId = $query_search["titles"][0]["id"];
145-
} else {
212+
} else
213+
{
146214
return $response;
147215
}
148216
}
149217
}
150218

219+
220+
// Check if film has been cached
221+
if ($this->cache("exists", $filmId))
222+
{
223+
// Return cached film data
224+
return $this->cache("get", $filmId);
225+
}
226+
227+
151228
// Set film url
152229
$film_url = $this->imdb_base_url . "title/$filmId/";
153230
// Load page
154231
$film_page = $this->loadDom($film_url);
155232

156-
$response["title"] = $this->textClean($this->htmlFind($film_page, '.title_wrapper h1')->text);
157-
$response["year"] = $this->textClean($this->htmlFind($film_page, '.title_wrapper h1 #titleYear a')->text);
158-
$response["rating"] = $this->textClean($this->htmlFind($film_page, '.ratings_wrapper .ratingValue span[itemprop=ratingValue]')->text);
233+
$response["id"] = $filmId;
234+
$response["title"] = $this->textClean($this->htmlFind($film_page, '.title_wrapper h1')->text);
235+
$response["year"] = $this->textClean($this->htmlFind($film_page, '.title_wrapper h1 #titleYear a')->text);
236+
$response["rating"] = $this->textClean($this->htmlFind($film_page, '.ratings_wrapper .ratingValue span[itemprop=ratingValue]')->text);
159237
$response["rating_votes"] = $this->textClean($this->htmlFind($film_page, '.ratings_wrapper span[itemprop=ratingCount]')->text);
160-
$response["length"] = $this->textClean($this->htmlFind($film_page, '.subtext time')->text);
161-
$response["plot"] = $this->textClean($this->htmlFind($film_page, '.plot_summary .summary_text')->text);
238+
$response["length"] = $this->textClean($this->htmlFind($film_page, '.subtext time')->text);
239+
$response["plot"] = $this->textClean($this->htmlFind($film_page, '.plot_summary .summary_text')->text);
162240

163241
// If rating votes exists
164242
if ($this->count($response["rating_votes"]) > 0)
@@ -185,7 +263,7 @@ public function film($query, $techSpecs=false) {
185263
$response["trailer"]["id"] = $this->textClean($trailer_link->getAttribute("data-video"));
186264
if (!empty($response["trailer"]["id"]))
187265
{
188-
$response["trailer"]["link"] = "https://www.imdb.com/videoplayer/" . $response["trailer"]["id"];
266+
$response["trailer"]["link"] = $this->imdb_base_url ."videoplayer/". $response["trailer"]["id"];
189267
}
190268
}
191269

@@ -271,6 +349,11 @@ public function film($query, $techSpecs=false) {
271349
}
272350
}
273351

352+
353+
// Add film data to cache
354+
$this->cache("update", $filmId, $response);
355+
356+
274357
return $response;
275358
}
276359

@@ -315,12 +398,9 @@ private function htmlFind($dom, $selection) {
315398

316399

317400
/**
318-
* Extract an imdb-id from a string '/ttxxxxxxx/'
319-
* Returns string of id or empty string if none found
401+
* Create an empty html element and return it
320402
*
321-
* @param $str string - string to extract ID from
322-
*
323-
* @return string
403+
* @return object
324404
*/
325405
private function emptyDomElement() {
326406
$dom = new Dom;
@@ -350,7 +430,8 @@ private function count($item) {
350430
* @return string
351431
*/
352432
private function extractImdbId($str) {
353-
try {
433+
try
434+
{
354435
// Search string for 2 letters followed by numbers
355436
// '/yyxxxxxxx'
356437
preg_match('/\/[A-Za-z]{2}[0-9]+/', $str, $imdbIds);

tests/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
// Initialise Imdb
1616
// Load film data
17-
$imdb = new Imdb();
17+
$imdb = new Imdb($cache=true);
1818
$film = $imdb->film($q, $techSpecs=true); // tt0816692 tt8633464
1919

2020
// Return loaded film data

0 commit comments

Comments
 (0)