api.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import urllib.parse
  2. import requests
  3. import config
  4. DEFAULT_RECOMMENDS_PAGE_SIZE = 18
  5. RECOMMENDS_PAGE_SIZE = DEFAULT_RECOMMENDS_PAGE_SIZE * 2
  6. MAX_RECOMMENDS_PAGE_SIZE = 161
  7. def gen_auth_headers():
  8. return {
  9. 'Cookie': f'PHPSESSID={config.SESSION_ID}',
  10. 'User-Agent': 'Mozilla/5.0'
  11. }
  12. def fetch_illust_pages(illust_id):
  13. url = f'https://www.pixiv.net/ajax/illust/{illust_id}/pages'
  14. resp = requests.get(url, headers=gen_auth_headers(), proxies=config.PROXIES)
  15. resp.raise_for_status()
  16. return resp
  17. def fetch_illust(illust_id):
  18. url = f'https://www.pixiv.net/ajax/illust/{illust_id}'
  19. resp = requests.get(url, headers=gen_auth_headers(), proxies=config.PROXIES)
  20. resp.raise_for_status()
  21. return resp
  22. def fetch_comments(illust_id):
  23. url = f'https://www.pixiv.net/ajax/illusts/comments/roots?illust_id={illust_id}'
  24. resp = requests.get(url, proxies=config.PROXIES)
  25. resp.raise_for_status()
  26. return resp
  27. def fetch_user_top(user_id):
  28. url = f'https://www.pixiv.net/ajax/user/{user_id}/profile/top'
  29. resp = requests.get(url, headers=gen_auth_headers(), proxies=config.PROXIES)
  30. resp.raise_for_status()
  31. return resp
  32. def fetch_user_all(user_id):
  33. url = f'https://www.pixiv.net/ajax/user/{user_id}/profile/all'
  34. resp = requests.get(url, headers=gen_auth_headers(), proxies=config.PROXIES)
  35. resp.raise_for_status()
  36. return resp
  37. def fetch_user_bookmarks(user_id, offset=0, limit=48):
  38. url = f'https://www.pixiv.net/ajax/user/{user_id}/illusts/bookmarks?offset={offset}&limit={limit}&rest=show&tag='
  39. resp = requests.get(url, headers=gen_auth_headers(), proxies=config.PROXIES)
  40. resp.raise_for_status()
  41. return resp
  42. def fetch_user_illusts(user_id, illust_ids):
  43. url = f'https://www.pixiv.net/ajax/user/{user_id}/profile/illusts'
  44. params = '?work_category=illustManga&is_first_page=0'
  45. for illust_id in illust_ids:
  46. params += '&ids[]=' + illust_id
  47. resp = requests.get(url + params, headers=gen_auth_headers(), proxies=config.PROXIES) # auth and proxy needed?
  48. resp.raise_for_status()
  49. return resp
  50. def fetch_illust_recommends_init(illust_id, limit=RECOMMENDS_PAGE_SIZE):
  51. url = f'https://www.pixiv.net/ajax/illust/{illust_id}/recommend/init?limit={limit}'
  52. resp = requests.get(url, headers=gen_auth_headers(), proxies=config.PROXIES)
  53. resp.raise_for_status()
  54. return resp
  55. #def fetch_illust_recommends_next(ids):
  56. # url = f'https://www.pixiv.net/ajax/illust/recommend/'
  57. # resp = requests.get(url, headers=gen_auth_headers(), proxies=config.PROXIES) # auth and proxy needed??
  58. # resp.raise_for_status()
  59. # return resp
  60. def fetch_user_banner(user_id):
  61. resp = requests.get(f'https://embed.pixiv.net/user_profile.php?id={user_id}', proxies=config.PROXIES)
  62. resp.raise_for_status()
  63. return resp
  64. def fetch_search_results(search_term):
  65. search_term_encoded = urllib.parse.quote(search_term)
  66. resp = requests.get(f'https://www.pixiv.net/ajax/search/artworks/{search_term_encoded}', headers=gen_auth_headers(), proxies=config.PROXIES)
  67. resp.raise_for_status()
  68. return resp
  69. def fetch_landing_page():
  70. resp = requests.get(f'https://www.pixiv.net/ajax/top/illust', headers=gen_auth_headers(), proxies=config.PROXIES)
  71. resp.raise_for_status()
  72. return resp