You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

numbers.py 929 B

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839
  1. import sys
  2. import urllib.request
  3. class NumbersApi:
  4. def __init__(self):
  5. self.base_url = "http://numbersapi.com/"
  6. def build_url(self, query, number):
  7. if query == "trivia":
  8. return self.base_url + number
  9. elif query == "math":
  10. return self.base_url + number + "/math"
  11. else:
  12. sys.exit("Unknown query: " + query)
  13. def do_request(self, url):
  14. with urllib.request.urlopen(url) as request:
  15. response = request.read().decode("utf-8")
  16. return response
  17. def get(self, query, number):
  18. url = self.build_url(query, number)
  19. return self.do_request(url)
  20. def main():
  21. if len(sys.argv) < 3:
  22. sys.exit("not enough arguments")
  23. number = sys.argv[1]
  24. query = sys.argv[2]
  25. numbers_api = NumbersApi()
  26. trivia = numbers_api.get(query, number)
  27. print(trivia)
  28. if __name__ == "__main__":
  29. main()