Advertisement
Keltere

Untitled

May 21st, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. function getRecipeFromUrl($url){
  2.  
  3. $html=getHtml($url);
  4.  
  5. $dom = new DomDocument();
  6.  
  7. $internalErrors = libxml_use_internal_errors(true);
  8. $dom->loadHTML($html);
  9. libxml_use_internal_errors($internalErrors);
  10.  
  11.  
  12. $finder = new DomXPath($dom);
  13.  
  14. //recipe name
  15. $classname="recipe-panel-title";
  16. $nodes = $finder->query("//*[@class='" . $classname . "']");
  17. $recipe['name']= $nodes[0]->textContent;
  18.  
  19.  
  20. //recipe author
  21. $classname="btn btn-success btn-sm";
  22. $nodes = $finder->query("//*[@class='" . $classname . "']");
  23. $recipe['author']= trim(str_replace('©','', $nodes[0]->textContent));
  24.  
  25. //recipe evaluation
  26. $classname="small";
  27. $nodes = $finder->query("//*[@class='" . $classname . "']");
  28. if($nodes[0])
  29. $recipe['score']= $nodes[0]->getAttribute('data-score');
  30.  
  31. //recipe flavours
  32. $classname1="even";
  33. $classname2="odd";
  34. $nodes = $finder->query("//*[@class='$classname1' or @class='$classname2']");
  35.  
  36. $recipe['flavours']=[];
  37. foreach ($nodes as $node){
  38. array_push($recipe['flavours'],
  39. $node->getElementsByTagName('a')[1]->textContent. ' '.
  40. '('. $node->getElementsByTagName('a')[0]->textContent.') '.
  41. $node->getElementsByTagName('div')[0]->textContent
  42. );
  43. }
  44.  
  45. //stepping and vg
  46. $classname="table table-sm flavors";
  47. $nodes = $finder->query("//*[@class='" . $classname . "']")[0]->getElementsByTagName('tr');
  48.  
  49. preg_match_all('!\d+(?:\.\d+)?!', $nodes[$nodes->length-2]->textContent, $matches);
  50.  
  51. $recipe['stepping']=$matches[0][1];
  52. $recipe['vg']=$matches[0][2];
  53. $recipe['pg']=abs($recipe['vg']-100);
  54.  
  55. return $recipe;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement