PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/PHP_DataScraping/phptest.php

https://github.com/NodoFox/Projects
PHP | 370 lines | 315 code | 22 blank | 33 comment | 58 complexity | 1e6c135daa241815fc0b308f326f01d1 MD5 | raw file
  1. <html>
  2. <head>
  3. <title> Search Result </title>
  4. <meta charset="UTF-8">
  5. </head>
  6. <style type="text/css">
  7. table{
  8. text-align: center;
  9. }
  10. td, th{
  11. text-align: center;
  12. height:50px;
  13. padding:7px;
  14. }
  15. img{
  16. height:180px;
  17. width:160px;
  18. }
  19. body{
  20. text-align: center;
  21. }
  22. #search{
  23. font-family:courier;
  24. }
  25. </style>
  26. <body>
  27. <h1 id="search"> SEARCH RESULT </h1><br>
  28. <?php
  29. error_reporting(0);
  30. // FUNCTION TO GET THE ROWS OF TABLE --
  31. function getTableRow($page){
  32. if(preg_match_all('/<tr.*?>[\s\S\n]*?([\s\S]*?)<\/tr>/', $page, $tableRow, PREG_SET_ORDER)){
  33. return $tableRow;
  34. }
  35. else{
  36. //echo $page;
  37. //echo "Hey there!<br>";
  38. return "NoMatchFound";
  39. }
  40. }
  41. function getEachPerformer($allPerformers){
  42. if(preg_match_all('/<a href="(.*?)">(.*?)<\/a>/',$allPerformers[0][0],$each,PREG_SET_ORDER))
  43. return $each;
  44. else
  45. return "N/A";
  46. }
  47. // GETS EACH PERFORMER AS AN ASSOSIATIVE ARRAY
  48. function getPerformers($tRow){
  49. if(preg_match_all('/<span class="performer".*?>[\s\S\n]*?([\s\S]*?)<\/span>/',$tRow,$performers,PREG_SET_ORDER)){
  50. $performerEach = getEachPerformer($performers);
  51. return $performerEach;
  52. }
  53. else{
  54. return "N/A";
  55. }
  56. }
  57. // GETS TITLE OF THE SONG
  58. function getSongTitle($tRow){
  59. if(preg_match_all('/<div class="title".*?>[\s\S\n]*?<a href="(.*?)".*?>[\s\S\n]*?([\s\S]*?)<\/a>/',$tRow,$title, PREG_SET_ORDER)){
  60. return $title;
  61. }
  62. else
  63. return "N/A";
  64. }
  65. function getEachComposer($compData){
  66. //if (preg_match_all('/Various Artists/',$compData[0][0],$result,PREG_SET_ORDER)) return "Various Artists";
  67. if(preg_match_all('/<a href="(.*?)">[\s\S\n]*?([\s\S]*?)<\/a>/',$compData[0][0], $allComposers, PREG_SET_ORDER)){
  68. return $allComposers;
  69. }
  70. else if(preg_match_all('/([\s\S]*)/',$compData[0][0],$result,PREG_SET_ORDER)) {
  71. return $result[0][0]; }
  72. else {return "N/A";}
  73. }
  74. // GETS ALL THE COMPOSERS OF THE SONG
  75. function getComposers($tRow, $regexString){
  76. if(preg_match_all($regexString,$tRow, $composerData, PREG_SET_ORDER)){
  77. $composers = getEachComposer($composerData);
  78. return $composers;
  79. }
  80. else
  81. return "N/A";
  82. }
  83. // GETS ALL THE COMPOSERS OF THE SONG
  84. function getComposers2($tRow, $regexString){
  85. if(preg_match_all($regexString,$tRow, $composerData, PREG_SET_ORDER)){
  86. $composers = getEachComposer2($composerData);
  87. return $composers;
  88. }
  89. else
  90. return "N/A";
  91. }
  92. function getEachComposer2($compData){
  93. //if (preg_match_all('/Various Artists/',$compData[0][0],$result,PREG_SET_ORDER)) return "Various Artists";
  94. if(preg_match_all('/<a href="(.*?)">[\s\S\n]*?([\s\S]*?)<\/a>/',$compData[0][0], $allComposers, PREG_SET_ORDER)){
  95. return $allComposers;
  96. }
  97. else {return "N/A";}
  98. }
  99. function getSongSample($tRow){
  100. if(preg_match_all('/<div class="[\S]*? icon-search-song-new[\s\S\n]*?<a href="(.*)?"[\s\S]*? title="(.*)?"/',$tRow,$songSample,PREG_SET_ORDER)){
  101. return $songSample;
  102. }
  103. else
  104. return "NoSample";
  105. }
  106. function getImageLink($tRow){
  107. }
  108. ?>
  109. <?php
  110. // FOR A SONG
  111. function buildSongTable($rows){
  112. if($rows == "NoMatchFound"){
  113. echo "<br><h2>Query Failed! No Match Found, Please Try Again!</h2>";
  114. }
  115. else{
  116. $count = 5;
  117. if(count($rows)<$count)
  118. $count = count($rows);
  119. echo "<table border = '1' align='center'>";
  120. echo "<tr><th>Sample</th><th>Title</th><th>Performer(s)</th><th>Composer(s)</th><th>Details</th></tr>";
  121. for($i=0;$i<$count;$i++){
  122. echo "<tr>";
  123. for($k=0;$k<1;$k++){
  124. // Sample
  125. echo "<td>";
  126. $sample=getSongSample($rows[$i][$k]);
  127. if($sample =="NoSample")
  128. echo "<img src='SongImg.jpg' />";
  129. else
  130. echo "<a href='".$sample[0][1]."'target ='_blank'>"."<img src='SongImg.jpg' />"."</a>";
  131. echo "</td>";
  132. // Title
  133. echo "<td>";
  134. $title = getSongTitle($rows[$i][$k]);
  135. $title[0][2] = str_replace("&quot;","",$title[0][2]);
  136. echo $title[0][2];
  137. echo "</td>";
  138. //Performers
  139. echo "<td>";
  140. $performers=getPerformers($rows[$i][$k]);
  141. if($performers == "N/A")
  142. echo "N/A";
  143. else{
  144. echo $performers[0][2];
  145. for($performCount = 1; $performCount<count($performers);$performCount++){
  146. echo " / ".$performers[$performCount][2];
  147. }
  148. }
  149. echo "</td>";
  150. // Composers
  151. echo "<td>";
  152. $regex = '/<div class="info".*?>([\s\S\n]*?[\s\S]*?)<\/div>/';
  153. $composers=getComposers2($rows[$i][$k],$regex);
  154. if($composers == "N/A")
  155. echo "N/A";
  156. else{
  157. echo $composers[0][2];
  158. for($performCount = 1; $performCount<count($composers);$performCount++){
  159. echo " / ".$composers[$performCount][2];
  160. }
  161. }
  162. echo "</td>";
  163. //Details
  164. echo "<td><a href='".$title[0][1]."'target ='_blank'> Details </a>";
  165. }
  166. echo "</tr>";
  167. }
  168. echo "</table>";
  169. }
  170. }
  171. ?>
  172. <?php
  173. //ARTIST'S METHODS
  174. function getArtistName($tRow){
  175. if(preg_match_all('/<div class="name".*?>[\s\S\n]*?<a href="(.*?)".*?>(.*?)<\/a>[\s\S\n]*?<\/div>/',$tRow,$artist,PREG_SET_ORDER)){
  176. return $artist;
  177. }
  178. else{
  179. return "N/A";
  180. }
  181. }
  182. function getImage($tRow){
  183. if(preg_match_all('/<img src=(.*?)>/',$tRow,$putImage,PREG_SET_ORDER)){
  184. return $putImage;
  185. }
  186. else
  187. return "NoImage";
  188. }
  189. function getGenreOrYear($tDiv){
  190. if(preg_match_all('/[\s\S\n]*?([\s\S]*?)<br\/>[\s\S\n]*?([\s\S]*)/',$tDiv[0][1],$GenreYear,PREG_SET_ORDER))
  191. return $GenreYear;
  192. else
  193. return "N/A";
  194. }
  195. function getGenresYears($tRow){
  196. if(preg_match_all('/<div class="info".*?>[\s\S\n]*?([\s\S]*?)<\/div>/',$tRow,$GenresYears,PREG_SET_ORDER)){
  197. //$GenresYears = str_replace("\n","",$GenresYears);
  198. $genAndYear = getGenreOrYear($GenresYears);
  199. return $genAndYear;
  200. }
  201. else
  202. return "N/A";
  203. }
  204. ?>
  205. <?php
  206. // BUILDING ARTIST TABLE
  207. function buildArtistTable($rows){
  208. if($rows == "NoMatchFound"){
  209. echo "<br><h2>Query Failed! No Match Found, Please Try Again!</h2>";
  210. }
  211. else{
  212. $count = 5;
  213. if(count($rows)<$count)
  214. $count = count($rows);
  215. echo "<table border = '1' align='center'>";
  216. echo "<tr><th>Image</th><th>Name</th><th>Genre(s)</th><th>Year(s)</th><th>Details</th></tr>";
  217. for($i=0;$i<$count;$i++){
  218. echo "<tr>";
  219. for($k=0;$k<1;$k++){
  220. //Image
  221. $image = getImage($rows[$i][$k]);
  222. if($image!="NoImage")
  223. echo "<td>".$image[0][0]."</td>";//"<td><img src='".$image[0][1]."'alt='No_Image'>"."</td>";
  224. else
  225. echo "<td><img src='"."ArtistImg.jpg"."'alt='No_Image'/>"."</td>";
  226. // ArtistName
  227. $artist = getArtistName($rows[$i][$k]);
  228. echo "<td>".$artist[0][2]."</td>";
  229. //Genre and Year
  230. $genreYear = getGenresYears($rows[$i][$k]);
  231. if($genreYear=="N/A"){
  232. echo "<td>"."N/A"."</td>"."<td>"."N/A"."</td>";
  233. }
  234. else{
  235. if(strlen(trim($genreYear[0][1]))==0) echo "<td>"."N/A"."</td>";
  236. else{
  237. echo "<td>".$genreYear[0][1]."</td>";
  238. }
  239. if(strlen(trim($genreYear[0][2])==0))echo "<td>"."N/A"."</td>";
  240. else{
  241. echo "<td>".$genreYear[0][2]."</td>";
  242. }
  243. }
  244. //Details
  245. echo "<td><a href='".$artist[0][1]."'target ='_blank'>Details</a></td>";
  246. }
  247. echo "</tr>";
  248. }
  249. echo "</table>";
  250. }
  251. }
  252. ?>
  253. <?php
  254. // ALBUM METHODS
  255. ?>
  256. <?php
  257. //BUILDING ALBUM TABLE
  258. function buildAlbumTable($rows){
  259. if($rows == "NoMatchFound"){
  260. echo "<br><h2>Query Failed! No Match Found, Please Try Again!</h2>";
  261. }
  262. else{
  263. $count = 5;
  264. if(count($rows)<$count)
  265. $count = count($rows);
  266. echo "<table border = '1' align='center'>";
  267. echo "<tr><th>Image</th><th>Name</th><th>Artist</th><th>Genre(s)</th><th>Year(s)</th><th>Details</th></tr>";
  268. for($i=0;$i<$count;$i++){
  269. echo "<tr>";
  270. for($k=0;$k<1;$k++){
  271. //Image
  272. $image = getImage($rows[$i][$k]);
  273. if($image!="NoImage")
  274. echo "<td>".$image[0][0]."</td>";//"<td><img src='".$image[0][1]."'alt='No_Image'>"."</td>";
  275. else
  276. echo "<td><img src='"."AlbumImg.jpg"."'alt='No_Image'/>"."</td>";
  277. // Title
  278. echo "<td>";
  279. $title = getSongTitle($rows[$i][$k]);
  280. $title[0][2] = str_replace("&quot;","",$title[0][2]);
  281. echo $title[0][2];
  282. echo "</td>";
  283. // Composers
  284. echo "<td>";
  285. $regex = '/<div class="artist".*?>([\s\S\n]*?[\s\S]*?)<\/div>/';
  286. $composers=getComposers($rows[$i][$k],$regex);
  287. if($composers == "N/A")
  288. echo "N/A";
  289. else if(preg_match_all('/([\s\S]*)/',$composers,$testify,PREG_SET_ORDER)) echo $composers;
  290. else{
  291. echo $composers[0][2];
  292. for($performCount = 1; $performCount<count($composers);$performCount++){
  293. echo " / ".$composers
  294. [$performCount][2];
  295. }
  296. }
  297. echo "</td>";
  298. //Genre and Year
  299. $genreYear = getGenresYears($rows[$i][$k]);
  300. if($genreYear=="N/A"){
  301. echo "<td>"."N/A"."</td>"."<td>"."N/A"."</td>";
  302. }
  303. else{
  304. if(strlen(trim($genreYear[0][2]))==0) echo "<td>"."N/A"."</td>";
  305. else{
  306. echo "<td>".$genreYear[0][2]."</td>";
  307. }
  308. if(strlen(trim($genreYear[0][1])==0))echo "<td>"."N/A"."</td>";
  309. else{
  310. echo "<td>".$genreYear[0][1]."</td>";
  311. }
  312. }
  313. //Details
  314. echo "<td><a href='".$title[0][1]."'target ='_blank'> Details </a>";
  315. }
  316. echo "</tr>";
  317. }
  318. echo "</table>";
  319. }
  320. }
  321. ?>
  322. <?php
  323. // FUNCTION TO START PROCESS --
  324. function begin(){
  325. $prefix = 'http://www.allmusic.com/search/';
  326. $query = $_POST["musicQuery"];
  327. $queryType = $_POST["musicType"];
  328. $queryValue = urlencode($query);
  329. $url = $prefix.$queryType;
  330. $url = $url."/".$queryValue;
  331. //echo $url."<br>";
  332. $contents = file_get_contents($url);
  333. echo "<h2><i>&quot;".$query."&quot;"." of type "."&quot;".$queryType."&quot;</i></h2><br>";
  334. $rows = getTableRow($contents);
  335. if($queryType == "songs")
  336. buildSongTable($rows);
  337. if($queryType == "albums")
  338. buildAlbumTable($rows);
  339. if($queryType == "artists")
  340. buildArtistTable($rows);
  341. }
  342. ?>
  343. <?php
  344. // MAIN
  345. begin();
  346. ?>
  347. </body>
  348. </html>