Advertisement
Guest User

Python wrappers for a couple Nightcafe API endpoints

a guest
Apr 21st, 2024
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. import json
  2. import requests
  3. import sys
  4. import time
  5.  
  6. def get_nightcafe_user_feed (user_id, json_file=None, image_path=None, max_items=999999999):
  7. base_url = "https://api.nightcafe.studio/creations?query=user-recent&user="
  8. last_id = None
  9. items = []
  10. going = True
  11. while going:
  12. url = base_url + user_id
  13. if last_id:
  14. url = url + "&lastVisibleId=" + last_id
  15. r = requests.get (url)
  16. data = r.json ()
  17. last_id = data["lastVisibleId"]
  18. going = last_id != ""
  19. results = data["results"]
  20. items.extend (results)
  21. if len (items) >= max_items:
  22. going = False
  23. time.sleep (1)
  24. if json_file is not None:
  25. with open (json_file, "w") as f:
  26. json.dump (items, f, indent=2)
  27. if image_path is not None:
  28. image_path = image_path.strip ()
  29. if not image_path.endswith ("/"):
  30. image_path = image_path + "/"
  31. for item in items:
  32. url = "https://images.nightcafe.studio/" + item["output"]
  33. image = requests.get (url).content
  34. with open (image_path + item["id"] + ".jpeg", "wb") as f:
  35. f.write (image)
  36. return items
  37.  
  38. def get_nightcafe_followers (user_id, json_file=None, max_items=999999999):
  39. base_url = "https://api.nightcafe.studio/follows?query=user-followers&user="
  40. last_id = None
  41. items = []
  42. going = True
  43. while going:
  44. url = base_url + user_id
  45. if last_id:
  46. url = url + "&lastVisibleId=" + last_id
  47. r = requests.get (url)
  48. data = r.json ()
  49. last_id = data["lastVisibleId"]
  50. going = last_id != ""
  51. results = data["results"]
  52. items.extend (results)
  53. if len (items) >= max_items:
  54. going = False
  55. time.sleep (1)
  56. if json_file is not None:
  57. with open (json_file, "w") as f:
  58. json.dump (items, f, indent=2)
  59. return items
  60.  
  61. if __name__ == "__main__":
  62. user_id = sys.argv[1]
  63. get_nightcafe_user_feed (user_id, json_file=user_id + "_nightcafe_feed.json"),
  64. get_nightcafe_followers (user_id, json_file=user_id + "_nightcafe_followers.json")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement