-
Notifications
You must be signed in to change notification settings - Fork 0
/
couchpotato_imdb.php
125 lines (103 loc) · 4.27 KB
/
couchpotato_imdb.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
/*
* Written By: Tobias
*/
// MySQL details
define('SQLHOST', 'localhost'); //The MySQL host ip/fqdn
define('SQLLOGIN', '');//The MySQL login
define('SQLPASS', ''); //The MySQL password
define('SQLDB', ''); //The MySQL database to use
define('SQLPORT', 3306); //The MySQL port to connect on
define('SQLSOCK', '/var/run/mysqld/mysqld.sock');
// Make sure we can use the mysqli extension
if (!extension_loaded ('mysqli')) {
echo json_encode(array("error" => "Mysqli Extension not loaded."));
die();
}
// Set content type
header('Content-Type: application/json');
// Do parameters match what we expect?
if (empty($_REQUEST['passkey']) || strlen($_REQUEST['passkey']) != 32 || (empty($_REQUEST['imdbid']) && empty($_REQUEST['search'])) || empty($_REQUEST['user'])) {
echo json_encode(array("error" => "Incorrect parameters."));
die();
}
// Connect to DB manually for exposed service. Variables below come from config.php loaded above
$DbLink = mysqli_connect(SQLHOST, SQLLOGIN, SQLPASS, SQLDB, SQLPORT, SQLSOCK) or die("Error: " . mysqli_error($DbLink));
// Get the Username, passkey and\or IMDb ID\search string from request
$Username = $DbLink->escape_string($_REQUEST['user']);
$PassKey = $DbLink->escape_string($_REQUEST['passkey']);
$IMDbID = $DbLink->escape_string($_REQUEST['imdbid']);
//$SearchString = $DbLink->escape_string($_REQUEST['search']);
//imdb
//url
$url = 'http://www.imdb.com/title/'.$IMDbID.'/';
//get the page content
$imdb_content = get_data($url);
//parse for product name
$name = get_match('/<title>(.*)<\/title>/isU',$imdb_content);
//build content
$content = ''.$name.'';
$content = substr($content, 0, strpos($content, " ("));
$content = str_replace(' ', '.', $content);
$content = preg_replace('/[^A-Za-z0-9\.]/', '', $content);
$content = str_replace('..', '.', $content);
//echo $content;
//gets the match content
function get_match($regex,$content)
{
preg_match($regex,$content,$matches);
return $matches[1];
}
//gets the data from a URL
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
$headers = array('Accept-Language: de-de;q=0.8,de;q=0.5',);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
// Things look good, let's find this media
$MediaQuery = "SELECT id, title, link, content FROM ttrss_entries WHERE title LIKE '%$content%'";
$MediaLookupResults = $DbLink->query($MediaQuery);
// Check for results
$TotalResults = $MediaLookupResults->num_rows;
if ($TotalResults == 0) {
echo json_encode(array("total_results" => $TotalResults));
}
else {
// Initialize for output
$JSONOutput = array();
// For each returned row build JSON output
while($ResultRow = mysqli_fetch_array($MediaLookupResults)) {
// Release Name
$Name = $ResultRow['title'];
// Download URL
$DownloadURL = $ResultRow['link'];
// Get download size (B -> KB -> MB)
$content = $ResultRow['content'];
$Size = get_match('/(\s[0-9].*|^[0-9].*)[G,M]/isU',$content);
$Size = str_replace(' ', '', $Size);
if ($Size < 10) {
$Size = round($Size * 1024, 0);
}
else {
$Size = round($Size, 0);
}
$MediaSize = $Size;
// Build array for JSON encoding
$Details = array("release_name" => $Name,
"download_url" => $DownloadURL,
"imdb_id" => $IMDbID,
"size" => $MediaSize);
// Add to final output array
array_push($JSONOutput, $Details);
}
// Encode and return data!!
echo json_encode(array("results" => $JSONOutput, "total_results" => $TotalResults));
}