Projects /PHP_DataScraping/phptest.php

Language PHP Lines 371
MD5 Hash 1e6c135daa241815fc0b308f326f01d1
Repository https://github.com/NodoFox/Projects.git View Raw File
  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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<html>
<head>
<title> Search Result </title>
<meta charset="UTF-8">
</head>
<style type="text/css">
table{
    text-align: center;
    
    
}
td, th{
    text-align: center;
    height:50px;
    padding:7px;
}
img{
    height:180px;
    width:160px;
}
body{
    text-align: center;
    
}
#search{
    font-family:courier;
}
</style>
<body>
<h1 id="search"> SEARCH RESULT </h1><br>
<?php
    error_reporting(0);
    // FUNCTION TO GET THE ROWS OF TABLE --
    function getTableRow($page){
        if(preg_match_all('/<tr.*?>[\s\S\n]*?([\s\S]*?)<\/tr>/', $page, $tableRow, PREG_SET_ORDER)){
            return $tableRow;
        }
        else{
            //echo $page;
            //echo "Hey there!<br>";           
            return "NoMatchFound";
        }
    }
    function getEachPerformer($allPerformers){
        if(preg_match_all('/<a href="(.*?)">(.*?)<\/a>/',$allPerformers[0][0],$each,PREG_SET_ORDER))
            return $each;
        else
            return "N/A";
    }
    // GETS EACH PERFORMER AS AN ASSOSIATIVE ARRAY
    function getPerformers($tRow){
        if(preg_match_all('/<span class="performer".*?>[\s\S\n]*?([\s\S]*?)<\/span>/',$tRow,$performers,PREG_SET_ORDER)){
            $performerEach = getEachPerformer($performers);
            return $performerEach;
        }
        else{
            return "N/A";
        }
    }
    // GETS TITLE OF THE SONG
    function getSongTitle($tRow){
        if(preg_match_all('/<div class="title".*?>[\s\S\n]*?<a href="(.*?)".*?>[\s\S\n]*?([\s\S]*?)<\/a>/',$tRow,$title, PREG_SET_ORDER)){
            return $title;
        }
        else
            return "N/A";
    }
    function getEachComposer($compData){
        //if (preg_match_all('/Various Artists/',$compData[0][0],$result,PREG_SET_ORDER)) return "Various Artists";
        if(preg_match_all('/<a href="(.*?)">[\s\S\n]*?([\s\S]*?)<\/a>/',$compData[0][0], $allComposers, PREG_SET_ORDER)){
            return $allComposers;
        }
        else if(preg_match_all('/([\s\S]*)/',$compData[0][0],$result,PREG_SET_ORDER)) {         
        return $result[0][0]; }
            
        else {return "N/A";}
    }
    // GETS ALL THE COMPOSERS OF THE SONG
    function getComposers($tRow, $regexString){
        if(preg_match_all($regexString,$tRow, $composerData, PREG_SET_ORDER)){
            $composers = getEachComposer($composerData);
            return $composers;
        }
        else
            return "N/A";
    }
    // GETS ALL THE COMPOSERS OF THE SONG
    function getComposers2($tRow, $regexString){
        if(preg_match_all($regexString,$tRow, $composerData, PREG_SET_ORDER)){
            $composers = getEachComposer2($composerData);
            return $composers;
        }
        else
            return "N/A";
    }
    function getEachComposer2($compData){
        //if (preg_match_all('/Various Artists/',$compData[0][0],$result,PREG_SET_ORDER)) return "Various Artists";
        if(preg_match_all('/<a href="(.*?)">[\s\S\n]*?([\s\S]*?)<\/a>/',$compData[0][0], $allComposers, PREG_SET_ORDER)){
            return $allComposers;
        }
                  
        else {return "N/A";}
    }
    
    function getSongSample($tRow){
        if(preg_match_all('/<div class="[\S]*? icon-search-song-new[\s\S\n]*?<a href="(.*)?"[\s\S]*? title="(.*)?"/',$tRow,$songSample,PREG_SET_ORDER)){
            return $songSample;
        }
        else
            return "NoSample";
    }
    function getImageLink($tRow){
        
    }
?>
<?php
    // FOR A SONG
    function buildSongTable($rows){
    if($rows == "NoMatchFound"){
            echo "<br><h2>Query Failed! No Match Found, Please Try Again!</h2>";
        }
        else{
            $count = 5;
            if(count($rows)<$count)
                $count = count($rows);
            echo "<table border = '1' align='center'>";
            echo "<tr><th>Sample</th><th>Title</th><th>Performer(s)</th><th>Composer(s)</th><th>Details</th></tr>";
            for($i=0;$i<$count;$i++){
                echo "<tr>";
                for($k=0;$k<1;$k++){                    
                    // Sample
                    echo "<td>";
                    $sample=getSongSample($rows[$i][$k]);
                    if($sample =="NoSample")
                        echo "<img src='SongImg.jpg' />";
                    else
                        echo "<a href='".$sample[0][1]."'target ='_blank'>"."<img src='SongImg.jpg' />"."</a>";
                    echo "</td>";
                    // Title
                    echo "<td>";
                    $title = getSongTitle($rows[$i][$k]);
                    $title[0][2] = str_replace("&quot;","",$title[0][2]);
                    echo $title[0][2];
                    echo "</td>";
                    //Performers
                    echo "<td>";
                    $performers=getPerformers($rows[$i][$k]);
                    if($performers == "N/A")
                        echo "N/A";
                    else{
                        echo $performers[0][2];
                        for($performCount = 1; $performCount<count($performers);$performCount++){
                            
                            echo " / ".$performers[$performCount][2];                            
                        }
                    }
                    echo "</td>";
                    // Composers
                    echo "<td>";
                    $regex = '/<div class="info".*?>([\s\S\n]*?[\s\S]*?)<\/div>/';
                    $composers=getComposers2($rows[$i][$k],$regex);
                    if($composers == "N/A")
                        echo "N/A";
                    
                    else{
                        echo $composers[0][2];
                        for($performCount = 1; $performCount<count($composers);$performCount++){
                            
                            echo " / ".$composers[$performCount][2];                            
                        }
                    }
                    
                    echo "</td>";
                    //Details
                    echo "<td><a href='".$title[0][1]."'target ='_blank'> Details </a>";
                }
                echo "</tr>";
                
            }
            echo "</table>";
        }
    }
?>
<?php
//ARTIST'S METHODS
    function getArtistName($tRow){
        if(preg_match_all('/<div class="name".*?>[\s\S\n]*?<a href="(.*?)".*?>(.*?)<\/a>[\s\S\n]*?<\/div>/',$tRow,$artist,PREG_SET_ORDER)){
            return $artist;
        }
        else{
            return "N/A";
        }
    }
    function getImage($tRow){
        if(preg_match_all('/<img src=(.*?)>/',$tRow,$putImage,PREG_SET_ORDER)){
            return $putImage;
        }
        else
            return "NoImage";
    }
    function getGenreOrYear($tDiv){
        if(preg_match_all('/[\s\S\n]*?([\s\S]*?)<br\/>[\s\S\n]*?([\s\S]*)/',$tDiv[0][1],$GenreYear,PREG_SET_ORDER))
            return $GenreYear;
        else
            return "N/A";
    }
    function getGenresYears($tRow){
        if(preg_match_all('/<div class="info".*?>[\s\S\n]*?([\s\S]*?)<\/div>/',$tRow,$GenresYears,PREG_SET_ORDER)){
            //$GenresYears = str_replace("\n","",$GenresYears);
            $genAndYear = getGenreOrYear($GenresYears);
            return $genAndYear;
        }
        else
            return "N/A";
    }
?>
<?php
    // BUILDING ARTIST TABLE
    function buildArtistTable($rows){
        if($rows == "NoMatchFound"){
            echo "<br><h2>Query Failed! No Match Found, Please Try Again!</h2>";
        }
        else{
            $count = 5;
            if(count($rows)<$count)
                $count = count($rows);
            echo "<table border = '1' align='center'>";
            echo "<tr><th>Image</th><th>Name</th><th>Genre(s)</th><th>Year(s)</th><th>Details</th></tr>";
            for($i=0;$i<$count;$i++){
                echo "<tr>";
                for($k=0;$k<1;$k++){
                    //Image
                    
                    $image = getImage($rows[$i][$k]);
                    if($image!="NoImage")
                        echo "<td>".$image[0][0]."</td>";//"<td><img src='".$image[0][1]."'alt='No_Image'>"."</td>";
                    else
                        echo "<td><img src='"."ArtistImg.jpg"."'alt='No_Image'/>"."</td>";                   
                    // ArtistName                    
                    $artist = getArtistName($rows[$i][$k]);
                    echo "<td>".$artist[0][2]."</td>";
                    //Genre and Year
                    $genreYear = getGenresYears($rows[$i][$k]);
                    if($genreYear=="N/A"){
                        echo  "<td>"."N/A"."</td>"."<td>"."N/A"."</td>";
                    }
                    else{
                        if(strlen(trim($genreYear[0][1]))==0) echo "<td>"."N/A"."</td>";
                        else{
                            echo "<td>".$genreYear[0][1]."</td>";
                        }
                        if(strlen(trim($genreYear[0][2])==0))echo "<td>"."N/A"."</td>";
                        else{
                            echo "<td>".$genreYear[0][2]."</td>";
                        }
                    
                    }
                    
                    //Details
                    echo "<td><a href='".$artist[0][1]."'target ='_blank'>Details</a></td>";
                }
                echo "</tr>";
            }
            echo "</table>";
        }
    }
?>
<?php
// ALBUM METHODS
?>
<?php
//BUILDING ALBUM TABLE
    function buildAlbumTable($rows){
        if($rows == "NoMatchFound"){
                echo "<br><h2>Query Failed! No Match Found, Please Try Again!</h2>";
            }
        else{
            $count = 5;
            if(count($rows)<$count)
                $count = count($rows);
            echo "<table border = '1' align='center'>";
            echo "<tr><th>Image</th><th>Name</th><th>Artist</th><th>Genre(s)</th><th>Year(s)</th><th>Details</th></tr>";
            for($i=0;$i<$count;$i++){
                echo "<tr>";
                for($k=0;$k<1;$k++){
                    //Image                    
                    $image = getImage($rows[$i][$k]);
                    if($image!="NoImage")
                        echo "<td>".$image[0][0]."</td>";//"<td><img src='".$image[0][1]."'alt='No_Image'>"."</td>";
                    else
                        echo "<td><img src='"."AlbumImg.jpg"."'alt='No_Image'/>"."</td>";
                    // Title
                    echo "<td>";
                    $title = getSongTitle($rows[$i][$k]);
                    $title[0][2] = str_replace("&quot;","",$title[0][2]);
                    echo $title[0][2];
                    echo "</td>";
                    // Composers
                    echo "<td>";
                    $regex = '/<div class="artist".*?>([\s\S\n]*?[\s\S]*?)<\/div>/';
                    $composers=getComposers($rows[$i][$k],$regex);
                    if($composers == "N/A")
                        echo "N/A";
                    else if(preg_match_all('/([\s\S]*)/',$composers,$testify,PREG_SET_ORDER)) echo $composers;
                    else{
                        echo $composers[0][2];
                        for($performCount = 1; $performCount<count($composers);$performCount++){
                            
                            echo " / ".$composers
                            [$performCount][2];                            
                        }
                    }
                    echo "</td>";
                    //Genre and Year
                    $genreYear = getGenresYears($rows[$i][$k]);
                    if($genreYear=="N/A"){
                        echo  "<td>"."N/A"."</td>"."<td>"."N/A"."</td>";
                    }
                    else{
                        if(strlen(trim($genreYear[0][2]))==0) echo "<td>"."N/A"."</td>";
                        else{
                            echo "<td>".$genreYear[0][2]."</td>";
                        }
                        if(strlen(trim($genreYear[0][1])==0))echo "<td>"."N/A"."</td>";
                        else{
                            echo "<td>".$genreYear[0][1]."</td>";
                        }
                    
                    }
                    //Details
                    echo "<td><a href='".$title[0][1]."'target ='_blank'> Details </a>";
                    
                    
                }
            echo "</tr>";
            }
            echo "</table>";
        }     
    }
?>
<?php
    // FUNCTION TO START PROCESS --
    function begin(){
        $prefix = 'http://www.allmusic.com/search/';
        $query = $_POST["musicQuery"];
        $queryType = $_POST["musicType"];
        $queryValue = urlencode($query);
        $url = $prefix.$queryType;
        $url = $url."/".$queryValue;
        //echo $url."<br>";
        $contents = file_get_contents($url);
        echo "<h2><i>&quot;".$query."&quot;"." of type "."&quot;".$queryType."&quot;</i></h2><br>";
        $rows = getTableRow($contents);
        if($queryType == "songs")
            buildSongTable($rows);
        if($queryType == "albums")
            buildAlbumTable($rows);
        if($queryType == "artists")
            buildArtistTable($rows);
        
        
    }

?>
<?php
// MAIN
    begin();
?>
</body> 
</html>
Back to Top