aboutsummaryrefslogtreecommitdiff
path: root/bridges/SensCritiqueBridge.php
blob: fc2093219d2553ff93ba62bbda223b86a5f3ebc6 (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
<?php
class SensCritiqueBridge extends BridgeAbstract {

	private $request;

  public function loadMetadatas() {
		$this->maintainer = "kranack";
		$this->name = "Sens Critique";
		$this->uri = "http://www.senscritique.com";
		$this->description = "Sens Critique news";
		$this->update = "2016-08-09";

		$this->parameters[] =
		'[
			{
				"name" : "Movies",
				"identifier" : "m",
				"type": "checkbox"
			},
			{
				"name" : "Series",
				"identifier" : "s",
				"type": "checkbox"
			},
			{
				"name" : "Video Games",
				"identifier" : "g",
				"type": "checkbox"
			},
			{
				"name" : "Books",
				"identifier" : "b",
				"type": "checkbox"
			},
			{
				"name" : "BD",
				"identifier" : "bd",
				"type": "checkbox"
			},
			{
				"name" : "Music",
				"identifier" : "mu",
				"type": "checkbox"
			}
		]';
	}

	public function collectData(array $param) {
		if ((isset($param['m']) && $param['m'])) {
			$this->collectMoviesData();
		} else if ((isset($param['s']) && $param['s'])) {
			$this->collectSeriesData();
		} else if ((isset($param['g']) && $param['g'])) {
			$this->collectGamesData();
		} else if ((isset($param['b']) && $param['b'])) {
			$this->collectBooksData();
		} else if ((isset($param['bd']) && $param['bd'])) {
			$this->collectBDsData();
		} else if ((isset($param['mu']) && $param['mu'])) {
			$this->collectMusicsData();
		} else {
			$this->returnError('You must choose a category', 400);
		}
  }

	private function collectMoviesData() {
		$html = '';
    $html = $this->file_get_html('http://www.senscritique.com/films/cette-semaine') or $this->returnError('No results for this query.', 404);
		$list = $html->find('ul.elpr-list', 0);

		$this->extractDataFromList($list);
	}

	private function collectSeriesData() {
		$html = '';
		$html = $this->file_get_html('http://www.senscritique.com/series/actualite') or $this->returnError('No results for this query.', 404);
		$list = $html->find('ul.elpr-list', 0);

		$this->extractDataFromList($list);
	}

	private function collectGamesData() {
		$html = '';
		$html = $this->file_get_html('http://www.senscritique.com/jeuxvideo/actualite') or $this->returnError('No results for this query.', 404);
		$list = $html->find('ul.elpr-list', 0);

		$this->extractDataFromList($list);
	}

	private function collectBooksData() {
		$html = '';
		$html = $this->file_get_html('http://www.senscritique.com/livres/actualite') or $this->returnError('No results for this query.', 404);
		$list = $html->find('ul.elpr-list', 0);

		$this->extractDataFromList($list);
	}

	private function collectBDsData() {
		$html = '';
		$html = $this->file_get_html('http://www.senscritique.com/bd/actualite') or $this->returnError('No results for this query.', 404);
		$list = $html->find('ul.elpr-list', 0);

		$this->extractDataFromList($list);
	}

	private function collectMusicsData() {
		$html = '';
		$html = $this->file_get_html('http://www.senscritique.com/musique/actualite') or $this->returnError('No results for this query.', 404);
		$list = $html->find('ul.elpr-list', 0);

		$this->extractDataFromList($list);
	}

	private function extractDataFromList($list) {
		if ($list === null) {
			$this->returnError('Cannot extract data from list', 400);
		}

		foreach ($list->find('li') as $movie) {
		    $item = new \Item();
		    $item->author = htmlspecialchars_decode($movie->find('.elco-title a', 0)->plaintext, ENT_QUOTES) . ' ' . $movie->find('.elco-date', 0)->plaintext;
		    $item->title = $movie->find('.elco-title a', 0)->plaintext . ' ' . $movie->find('.elco-date', 0)->plaintext;
		    $item->content = '<em>' . $movie->find('.elco-original-title', 0)->plaintext . '</em><br><br>' .
												 $movie->find('.elco-baseline', 0)->plaintext . '<br>' .
												 $movie->find('.elco-baseline', 1)->plaintext . '<br><br>' .
												 $movie->find('.elco-description', 0)->plaintext . '<br><br>' .
												 trim($movie->find('.erra-ratings .erra-global', 0)->plaintext) . ' / 10';
		    $item->id = $this->getURI() . $movie->find('.elco-title a', 0)->href;
		    $item->uri = $this->getURI() . $movie->find('.elco-title a', 0)->href;
		    $this->items[] = $item;
		}
	}

	public function getURI() {
		return (trim($this->uri) === "") ? "http://www.senscritique.com" : $this->uri;
	}

	public function getCacheDuration(){
		return 21600; // 6 hours
	}

}