api.php 4.7 KB

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