Korotkodul

LORD_1

Oct 31st, 2021 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.28 KB | None | 0 0
  1. import requests
  2. import json
  3. #Access token:
  4. #7mbNq6GVWo0LiEQS7GCc
  5.  
  6. address_id = 'https://the-one-api.dev/v2/book/5cf5805fb53e011a64671582/chapter'
  7. book_id = requests.get(address_id)
  8. if book_id.status_code == requests.codes.ok:
  9.     #print("OK")
  10.     book_id = book_id.json()
  11. with open("book_id.json", 'w', encoding = 'utf-8') as fl:
  12.    fl.write(json.dumps(book_id, ensure_ascii = False, indent = 4))
  13. #"_id": "5cf5805fb53e011a64671582",
  14.  
  15. # "name": "The Fellowship Of The Ring"
  16. from requests.structures import CaseInsensitiveDict
  17. url = "https://the-one-api.dev/v2/book/5cf5805fb53e011a64671582/chapter" #/5cf5805fb53e011a64671582/chapter
  18. headers = CaseInsensitiveDict()
  19. headers["Accept"] = "application/json"
  20. headers["Authorization"] = "Bearer 7mbNq6GVWo0LiEQS7GCc"
  21. resp = requests.get(url, headers=headers)
  22. #print(resp.status_code)
  23. if resp.status_code == requests.codes.ok:
  24.     chapt = resp.json()
  25. all_chapt = {}
  26. cnt = 1
  27. for ch in chapt["docs"]:
  28. #    if ch["book"] == "5cf5805fb53e011a64671582":
  29.     all_chapt[cnt] = ch["chapterName"]
  30.     cnt += 1
  31. chapt_num = len(all_chapt)
  32. ans_1 = {}
  33. ans_1["chapters"] = all_chapt
  34. ans_1["num_of_chapters"] = len(all_chapt)
  35. with open("N1.json", 'w', encoding = 'utf-8') as fl:
  36.     fl.write(json.dumps(ans_1, ensure_ascii = False, indent = 4))
  37.  
Add Comment
Please, Sign In to add comment