WallpaperStopBridge.php 3.0 KB

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