PixivBridge.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. class PixivBridge extends BridgeAbstract {
  3. const MAINTAINER = 'teromene';
  4. const NAME = 'Pixiv Bridge';
  5. const URI = 'https://www.pixiv.net/';
  6. const DESCRIPTION = 'Returns the tag search from pixiv.net';
  7. const PARAMETERS = array( array(
  8. 'tag' => array(
  9. 'name' => 'Tag to search',
  10. 'exampleValue' => 'example',
  11. 'required' => true
  12. ),
  13. ));
  14. public function collectData(){
  15. $html = getContents(static::URI.'search.php?word=' . urlencode($this->getInput('tag')))
  16. or returnClientError('Unable to query pixiv.net');
  17. $regex = '/<input type="hidden"id="js-mount-point-search-result-list"data-items="([^"]*)/';
  18. $timeRegex = '/img\/([0-9]{4})\/([0-9]{2})\/([0-9]{2})\/([0-9]{2})\/([0-9]{2})\/([0-9]{2})\//';
  19. preg_match_all($regex, $html, $matches, PREG_SET_ORDER, 0);
  20. if(!$matches) return;
  21. $content = json_decode(html_entity_decode($matches[0][1]), true);
  22. $count = 0;
  23. foreach($content as $result) {
  24. if($count == 10) break;
  25. $count++;
  26. $item = array();
  27. $item['id'] = $result['illustId'];
  28. $item['uri'] = 'https://www.pixiv.net/member_illust.php?mode=medium&illust_id=' . $result['illustId'];
  29. $item['title'] = $result['illustTitle'];
  30. $item['author'] = $result['userName'];
  31. preg_match_all($timeRegex, $result['url'], $dt, PREG_SET_ORDER, 0);
  32. $elementDate = DateTime::createFromFormat('YmdHis',
  33. $dt[0][1] . $dt[0][2] . $dt[0][3] . $dt[0][4] . $dt[0][5] . $dt[0][6]);
  34. $item['timestamp'] = $elementDate->getTimestamp();
  35. $item['content'] = "<img src='" . $this->cacheImage($result['url'], $item['id']) . "' />";
  36. $this->items[] = $item;
  37. }
  38. }
  39. public function cacheImage($url, $illustId) {
  40. $url = str_replace('_master1200', '', $url);
  41. $url = str_replace('c/240x240/img-master/', 'img-original/', $url);
  42. $path = CACHE_DIR . '/pixiv_img';
  43. if(!is_dir($path))
  44. mkdir($path, 0755, true);
  45. if(!is_file($path . '/' . $illustId . '.jpeg')) {
  46. $headers = array('Referer: https://www.pixiv.net/member_illust.php?mode=medium&illust_id=' . $illustId);
  47. $illust = getContents($url, $headers);
  48. if(strpos($illust, '404 Not Found') !== false) {
  49. $illust = getContents(str_replace('jpg', 'png', $url), $headers);
  50. }
  51. file_put_contents($path . '/' . $illustId . '.jpeg', $illust);
  52. }
  53. return 'cache/pixiv_img/' . $illustId . '.jpeg';
  54. }
  55. }