api.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. $stream_context = [
  3. 'http' => [
  4. 'header' =>
  5. 'User-Agent: Mozilla/5.0' . PHP_EOL .
  6. 'Cookie: PHPSESSID=' . SESSION_ID,
  7. 'ignore_errors' => true
  8. ]
  9. ];
  10. if (HTTP_PROXY) {
  11. $stream_context['http']['proxy'] = 'tcp://' . HTTP_PROXY;
  12. }
  13. stream_context_set_default($stream_context);
  14. function get_lang_query() {
  15. if (function_exists('get_string')) {
  16. return 'lang=' . get_string('pixiv-lang');
  17. }
  18. return 'lang=en';
  19. }
  20. function get_json($url, $associative = false) {
  21. $response = file_get_contents($url);
  22. if (!isset($http_response_header)) {
  23. return [false, 'network-error'];
  24. }
  25. $response_code = substr($http_response_header[0], 9, 3);
  26. if ($response_code == '401') {
  27. return [false, 'unauthorized'];
  28. }
  29. return [json_decode($response, $associative), false];
  30. }
  31. function get_discovery() {
  32. return get_json(sprintf(
  33. 'https://www.pixiv.net/ajax/discovery/artworks?mode=%s&limit=%s&%s',
  34. get_closest_mode(), 100, get_lang_query()
  35. ));
  36. }
  37. function get_artwork_info($id) {
  38. list($response, $error) = get_json(sprintf(
  39. 'https://www.pixiv.net/ajax/illust/%s?%s',
  40. $id, get_lang_query()
  41. ));
  42. if ($error) return [false, $error];
  43. if (empty($response->body)) return [false, 'artwork.not-found'];
  44. return [$response, false];
  45. }
  46. function get_artwork_pages($id) {
  47. list($response, $error) = get_json(sprintf(
  48. 'https://www.pixiv.net/ajax/illust/%s/pages',
  49. $id
  50. ), true);
  51. if ($error) return [false, $error];
  52. if (empty($response['body'])) return [false, 'artwork.not-found'];
  53. return [$response, false];
  54. }
  55. function get_artwork_comments($id, $offset, $limit) {
  56. list($response, $error) = get_json(sprintf(
  57. 'https://www.pixiv.net/ajax/illusts/comments/roots?illust_id=%s&offset=%s&limit=%s',
  58. $id, $offset, $limit
  59. ));
  60. if ($error) return [false, $error];
  61. if (empty($response->body)) return [false, 'artwork.not-found'];
  62. if (empty($response->body->comments)) return [false, 'artwork.no-comments'];
  63. return [$response, false];
  64. }
  65. function get_artwork_comment_replies($id, $page) {
  66. list($response, $error) = get_json(sprintf(
  67. 'https://www.pixiv.net/ajax/illusts/comments/replies?comment_id=%s&page=%s',
  68. $id, $page
  69. ));
  70. if ($error) return [false, $error];
  71. if (empty($response->body->comments)) return [false, 'artwork.no-replies'];
  72. return [$response, false];
  73. }
  74. function get_artwork_related($id) {
  75. return get_json(sprintf(
  76. 'https://www.pixiv.net/ajax/illust/%s/recommend/init?limit=%s&%s',
  77. $id, 120, get_lang_query()
  78. ));
  79. }
  80. function get_search($query, $page) {
  81. return get_json(sprintf(
  82. 'https://www.pixiv.net/ajax/search/artworks/%s?mode=%s&p=%s&%s',
  83. urlencode($query), get_closest_mode(), $page, get_lang_query()
  84. ));
  85. }
  86. function get_tag_info($query) {
  87. return get_json(sprintf(
  88. 'https://www.pixiv.net/ajax/search/tags/%s',
  89. urlencode($query)
  90. ));
  91. }
  92. function get_user_info($id) {
  93. list($response, $error) = get_json(sprintf(
  94. 'https://www.pixiv.net/ajax/user/%s?full=1',
  95. $id
  96. ));
  97. if ($error) return [false, $error];
  98. if (empty($response->body)) return [false, 'user.not-found'];
  99. return [$response, false];
  100. }
  101. function get_user_bookmarks($id, $offset, $limit) {
  102. list($response, $error) = get_json(sprintf(
  103. 'https://www.pixiv.net/ajax/user/%s/illusts/bookmarks?tag=&offset=%s&limit=%s&rest=show&%s',
  104. $id, $offset, $limit, get_lang_query()
  105. ));
  106. if ($error) return [false, $error];
  107. if (empty($response->body)) return [false, 'user.not-found'];
  108. return [$response, false];
  109. }
  110. function get_user_artworks_with_tag($id, $category, $tag, $offset, $limit) {
  111. $request_path = [
  112. 'artworks' => 'illustmanga',
  113. 'illustrations' => 'illusts',
  114. 'manga' => 'manga'
  115. ][$category];
  116. list($response, $error) = get_json(sprintf(
  117. 'https://www.pixiv.net/ajax/user/%s/%s/tag?tag=%s&offset=%s&limit=%s&%s',
  118. $id, $request_path, urlencode($tag), $offset, $limit, get_lang_query()
  119. ));
  120. if ($error) return [false, $error];
  121. if (empty($response->body)) return [false, 'user.not-found'];
  122. return [$response, false];
  123. }
  124. function get_user_artworks_list($id) {
  125. list($response, $error) = get_json(sprintf(
  126. 'https://www.pixiv.net/ajax/user/%s/profile/all',
  127. $id
  128. ), true);
  129. if ($error) return [false, $error];
  130. if (empty($response['body'])) return [false, 'user.not-found'];
  131. return [$response, false];
  132. }
  133. function get_user_artworks($user_id, $artwork_ids) {
  134. list($response, $error) = get_json(sprintf(
  135. 'https://www.pixiv.net/ajax/user/%s/profile/illusts?work_category=illustManga&is_first_page=0&%s&%s',
  136. $user_id, get_lang_query(), http_build_query(['ids' => $artwork_ids])
  137. ));
  138. if ($error) return [false, $error];
  139. if (empty($response->body)) return [false, 'user.not-found'];
  140. return [$response, false];
  141. }
  142. function get_frequent_tags($artwork_ids) {
  143. return get_json(sprintf(
  144. 'https://www.pixiv.net/ajax/tags/frequent/illust?%s&%s',
  145. get_lang_query(), http_build_query(['ids' => $artwork_ids])
  146. ));
  147. }