PNA.fi koodi

pnalib.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright 2018 Toni Fadjukoff. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os.path
  15. import urllib.request
  16. import urllib.error
  17. import json
  18. def get_jsonp_file(url, temp_fname, use_old, allow_old=True):
  19. try:
  20. return get_file(url, temp_fname, use_old, jsonp_load, allow_old)
  21. except json.decoder.JSONDecodeError as e:
  22. print("Failed to parse JSON from {}".format(temp_fname))
  23. raise
  24. def get_json_file(url, temp_fname, use_old, allow_old=True):
  25. try:
  26. return get_file(url, temp_fname, use_old, json.load, allow_old)
  27. except json.decoder.JSONDecodeError as e:
  28. print("Failed to parse JSON from {}".format(temp_fname))
  29. raise
  30. def jsonp_load(fp):
  31. return json.loads(fp.read()[1:-2])
  32. def read_all(fp):
  33. return fp.read()
  34. def get_file(url, temp_fname, use_old, consumer=read_all, allow_old=True):
  35. if not use_old or not os.path.isfile(temp_fname):
  36. try:
  37. urllib.request.urlretrieve(url, temp_fname)
  38. except urllib.error.HTTPError as e:
  39. print("Failed to download {url}".format(url=url))
  40. # Juvenes may fail with error code 500 if food is not available
  41. if not allow_old:
  42. return None
  43. try:
  44. with open(temp_fname, "r", encoding="utf-8") as fin:
  45. return consumer(fin)
  46. except OSError as e:
  47. pass