WallpaperStopBridge.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. class WallpaperStopBridge extends BridgeAbstract {
  3. private $category;
  4. private $subcategory;
  5. private $resolution;
  6. public function loadMetadatas() {
  7. $this->maintainer = "nel50n";
  8. $this->name = "WallpaperStop Bridge";
  9. $this->uri = "http://www.wallpaperstop.com/";
  10. $this->description = "Returns the latests wallpapers from WallpaperStop";
  11. $this->update = "2014-11-05";
  12. $this->parameters[] =
  13. '[
  14. {
  15. "name" : "Category",
  16. "identifier" : "c"
  17. },
  18. {
  19. "name" : "subcategory",
  20. "identifier" : "s"
  21. },
  22. {
  23. "name" : "Max number of wallpapers",
  24. "identifier" : "m",
  25. "type" : "number"
  26. },
  27. {
  28. "name" : "resolution",
  29. "identifier" : "r",
  30. "exampleValue" : "1920x1200, 1680x1050, ..."
  31. }
  32. ]';
  33. }
  34. public function collectData(array $param){
  35. $html = '';
  36. if (!isset($param['c'])) {
  37. $this->returnError('You must specify at least a category (?c=...).', 400);
  38. } else {
  39. $baseUri = 'http://www.wallpaperstop.com';
  40. $this->category = $param['c'];
  41. $this->subcategory = $param['s'] ?: '';
  42. $this->resolution = $param['r'] ?: '1920x1200'; // Wide wallpaper default
  43. $num = 0;
  44. $max = $param['m'] ?: 20;
  45. $lastpage = 1;
  46. for ($page = 1; $page <= $lastpage; $page++) {
  47. $link = $baseUri.'/'.$this->category.'-wallpaper/'.(!empty($this->subcategory)?$this->subcategory.'-wallpaper/':'').'desktop-wallpaper-'.$page.'.html';
  48. $html = $this->file_get_html($link) or $this->returnError('No results for this query.', 404);
  49. if ($page === 1) {
  50. preg_match('/-(\d+)\.html$/', $html->find('.pagination > .last', 0)->href, $matches);
  51. $lastpage = min($matches[1], ceil($max/20));
  52. }
  53. foreach($html->find('article.item') as $element) {
  54. $wplink = $element->getAttribute('data-permalink');
  55. if (preg_match('%^http://www\.wallpaperstop\.com/(.+)/([^/]+)-(\d+)\.html$%', $wplink, $matches)) {
  56. $thumbnail = $element->find('img', 0);
  57. $item = new \Item();
  58. $item->uri = $baseUri.'/wallpapers/'.str_replace('wallpaper', 'wallpapers', $matches[1]).'/'.$matches[2].'-'.$this->resolution.'-'.$matches[3].'.jpg';
  59. $item->id = $matches[3];
  60. $item->timestamp = time();
  61. $item->title = $thumbnail->title;
  62. $item->thumbnailUri = $baseUri.$thumbnail->src;
  63. $item->content = $item->title.'<br><a href="'.$wplink.'"><img src="'.$item->thumbnailUri.'" /></a>';
  64. $this->items[] = $item;
  65. $num++;
  66. if ($num >= $max)
  67. break 2;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. public function getName(){
  74. return 'WallpaperStop - '.$this->category.(!empty($this->subcategory) ? ' > '.$this->subcategory : '').' ['.$this->resolution.']';
  75. }
  76. public function getURI(){
  77. return 'http://www.wallpaperstop.com';
  78. }
  79. public function getCacheDuration(){
  80. return 43200; // 12 hours
  81. }
  82. }