aboutsummaryrefslogtreecommitdiff
path: root/bridges/UnogsBridge.php
blob: cac1875274c8c2771cc1a900496bca4cf6f18922 (plain) (blame)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php

class UnogsBridge extends BridgeAbstract {

	const MAINTAINER = 'csisoap';
	const NAME = 'uNoGS Bridge';
	const URI = 'https://unogs.com';
	const DESCRIPTION = 'Return what\'s new or removal on Netflix';

	const PARAMETERS = array(
		'global' => array(
			'feed' => array(
				'name' => 'feed',
				'type' => 'list',
				'title' => 'Choose whether you want latest movies or removal on Netflix',
				'values' => array(
					'What\'s New' => 'new last 7 days',
					'Expiring' => 'expiring'
				)
			)
		),
		'Global' => array(),
		'Country' => array(
			'country_code' => array(
				'name' => 'Country',
				'type' => 'list',
				'title' => 'Choose your preferred country',
				'values' => array(
					'Argentina' => 21,
					'Australia' => 23,
					'Belgium' => 26,
					'Brazil' => 29,
					'Canada' => 33,
					'Colombia' => 36,
					'Czech Republic' => 307,
					'France' => 45,
					'Germany' => 39,
					'Greece' => 327,
					'Hong Kong' => 331,
					'Hungary' => 334,
					'Iceland' => 265,
					'India' => 337,
					'Israel' => 336,
					'Italy' => 269,
					'Japan' => 267,
					'Lithuania' => 357,
					'Malaysia' => 378,
					'Mexico' => 65,
					'Netherlands' => 67,
					'Philippines' => 390,
					'Poland' => 392,
					'Portugal' => 268,
					'Romania' => 400,
					'Russia' => 402,
					'Singapore' => 408,
					'Slovakia' => 412,
					'South Africa' => 447,
					'South Korea' => 348,
					'Spain' => 270,
					'Sweden' => 73,
					'Switzerland' => 34,
					'Thailand' => 425,
					'Turkey' => 432,
					'Ukraine' => 436,
					'United Kingdom' => 46,
					'United States' => 78
				)
			)
		)
	);

	public function getName() {
		$feedName = '';
		if($this->queriedContext == 'Global') {
			$feedName .= 'Netflix Global - ';
		} elseif($this->queriedContext == 'Country') {
			$feedName .= 'Netflix ' . $this->getParametersKey('country_code') . ' - ';
		}
		if($this->getInput('feed') == 'expiring') {
			$feedName .= 'Expiring title';
		} elseif($this->getInput('feed') == 'new last 7 days') {
			$feedName .= 'What\'s New';
		} else {
			$feedName = self::NAME;
		}
		return $feedName;
	}

	private function getParametersKey($input = '') {
		$params = $this->getParameters();
		$tab = 'Country';
		if (!isset($params[$tab][$input])) {
			return '';
		}

		return array_search(
			$this->getInput($input),
			$params[$tab][$input]['values']
		);

	}

	private function getJSON($url) {
		$header = array(
			'Referer: https://unogs.com/',
			'referrer: http://unogs.com'
		);

		$raw = getContents($url, $header);
		return json_decode($raw, true);
	}

	private function getImage($nfid) {
		$url = self::URI . '/api/title/bgimages?netflixid=' . $nfid;
		$json = $this->getJSON($url);
		$image_wrapper = '';
		if(isset($json['bo1280x448'])) {
			$image_wrapper = 'bo1280x448';
		} else {
			$image_wrapper = 'bo665x375';
		}
		end($json[$image_wrapper]);
		$position = key($json[$image_wrapper]);
		$image_link = $json[$image_wrapper][$position]['url'];
		return $image_link;
	}

	private function handleData($data) {
		$item = array();
		$item['title'] = $data['title'] . ' - ' . $data['year'];
		$item['timestamp'] = $data['titledate'];
		$netflix_id = $data['nfid'];
		$item['uri'] = 'https://www.netflix.com/title/' . $netflix_id;
		$image_url = $this->getImage($netflix_id);
		$netflix_synopsis = $data['synopsis'];
		$expired_warning = '';
		if(isset($data['expires'])) {
			$expired_warning .= '<p><b>Expired on: ' . $data['expires'] . '</b></p>';
			$item['timestamp'] = $data['expires'];
		}
		$unogs_url = self::URI . '/title/' . $netflix_id;

		$item['content'] = <<<EOD
<img src={$image_url}>
$expired_warning
<p>$netflix_synopsis</p>
<p>Details: <a href={$unogs_url}>$unogs_url</a></p>
EOD;
		$this->items[] = $item;
	}

	public function collectData() {
		$feed = $this->getInput('feed');
		$is_global = false;
		$country_code = '';

		switch ($this->queriedContext) {
			case 'Country':
				$country_code = $this->getInput('country_code');
				break;
		}

		$api_url = self::URI . '/api/search?query=' . urlencode($feed)
				. ($country_code ? '&countrylist=' . $country_code : '') . '&limit=30';
		$json_data = $this->getJSON($api_url);
		$movies = $json_data['results'];

		if($this->getInput('feed') == 'expiring') {
			/*  uNoGS API returns movies/series that going to remove
			*   today according to the day you fetch the data.
			*   They put items that going to remove in the future on the last
			*   so I reverse this to get those items, not to bothers those that already removed today.
			*/
			$movies = array_reverse($movies);
		}

		foreach($movies as $movie) {
			$this->handleData($movie);
		}
	}
}