-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchHelper.php
66 lines (60 loc) · 1.72 KB
/
searchHelper.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
<?php
/*
USAGE:
Data is retrieved in the format of $array['text'] and $array['url'] stored in our get functions.
Example:
$search = getSearchRaw(composeURL("taylor swift"));
$artists = (getArtists($search));
print_r($artists);
echo $artists[$i]['text'];
echo $artists[$i]['url'];
*/
require_once('./scrapper/simple_html_dom.php');
require_once('./constants.php');
/* START OF SEARCH DATA DEPENDENT VARIABLES
These numbers should only be modified if the website we scrap data from changes. */
define("LYRIC_INDEX_START", 0);
define("LYRIC_INDEX_END", 5);
define("ARTIST_INDEX_START", 6);
define("ARTIST_INDEX_END", 11);
/* END OF DATA DEPENDENT VARIABLES */
function composeURL($input) {
$url = BASE_SEARCH_URL;
return BASE_SEARCH_URL . preg_replace('/\s+/', '+', $input);
}
function getSearchRaw($url) {
//<url class="search" -> <li></li>...
$c = 0;
$html = file_get_html($url);
foreach($html->find('ul[class=search]',0)->find('li') as $element) {
foreach($element->find('a') as $next) {
$data[$c][TEXT] = $next->innertext;
$data[$c][URL] = $next->href;
$c++;
//echo $next->href . ' - ' . $next->innertext. '<br>';
}
}
return $data;
}
function getSearchLyrics($data) { // Return a nested array w/ title & urls.
$c = 0;
$max = sizeof($data);
if($max > LYRIC_INDEX_END)
$max = LYRIC_INDEX_END;
for($i = LYRIC_INDEX_START; $i < $max; $i++) {
$lyrics[$c][TEXT] = $data[$i][TEXT];
$lyrics[$c][URL] = $data[$i][URL];
$c++;
}
return $lyrics;
}
function getSearchArtists($data) {
$c = 0;
for($i = ARTIST_INDEX_START; $i < ARTIST_INDEX_END; $i++) {
$artists[$c][TEXT] = $data[$i][TEXT];
$artists[$c][URL] = $data[$i][URL];
$c++;
}
return $artists;
}
?>