api.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import time
  2. import requests
  3. import json
  4. class ChanAPI:
  5. '''Loading and handling the storage of Thread objects'''
  6. def __init__(self, board):
  7. self.__board = board
  8. self.__delay = 1 # required by api
  9. def __get(self, no=None, requestType=None):
  10. ''' Return a dict of the JSON structure'''
  11. if no and requestType=="thread":
  12. fchanUrl = f"https://a.4cdn.org/{self.__board}/thread/{no}.json"
  13. return json.loads(requests.get(fchanUrl).content)
  14. elif requestType:
  15. fchanUrl = f"http://a.4cdn.org/{self.__board}/{requestType}.json"
  16. return json.loads(requests.get(fchanUrl).content)
  17. time.sleep( self.__delay )
  18. def getThread(self,no):
  19. ''' Return a dict of the JSON structure'''
  20. return self.__get(no,"thread")
  21. def getCatalog(self):
  22. ''' Return a dict of the JSON structure'''
  23. return self.__get(requestType="catalog")
  24. def getArchive(self):
  25. ''' Return a dict of the JSON structure'''
  26. return self.__get(requestType="archive")