Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python3
  2. # https://apod.nasa.gov/apod/
  3. #
  4. # ----------------------------------------------------------------------------
  5. # "THE BEER-WARE LICENSE" (Revision 42):
  6. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  7. # you can do whatever you want with this stuff. If we meet some day, and you
  8. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  9. # ----------------------------------------------------------------------------
  10. import time
  11. import sys
  12. import os
  13. import util
  14. from image import ImageScreen
  15. class APOD:
  16. def __init__(self, g, i, to = 10.0, r = 6 * 60 * 60):
  17. self.gui = g
  18. self.input = i
  19. self.timeout = to
  20. self.refresh = r
  21. self.get = None
  22. self.path = "https://apod.nasa.gov/apod"
  23. self.img_url = None
  24. self.img_path = None
  25. self.image = None
  26. self.last = None
  27. self.restart()
  28. def restart(self):
  29. if (self.last == None) or ((time.time() - self.last) >= self.refresh):
  30. try:
  31. print("APOD refresh")
  32. self.img_url = self.get_image_path()
  33. self.img_path = self.download_image(self.img_url)
  34. self.image = ImageScreen(self.gui, self.img_path, 0.2, 1, 5.0, None, None, False)
  35. self.last = time.time()
  36. except Exception as e:
  37. print()
  38. if hasattr(sys, "print_exception"):
  39. sys.print_exception(e)
  40. else:
  41. print(e)
  42. print()
  43. self.show = time.time()
  44. def finished(self):
  45. return (self.image == None) or ((time.time() - self.show) >= self.timeout)
  46. def fetch(self, url):
  47. # lazily initialize WiFi
  48. if self.get == None:
  49. self.get, post = util.getRequests()
  50. if self.get == None:
  51. return None
  52. try:
  53. #print("GET " + url)
  54. r = self.get(url)
  55. # explitic close on Response object not needed,
  56. # handled internally by r.content / r.text / r.json()
  57. # to avoid this automatic behaviour, first access r.content
  58. # to trigger caching it in response object, then close
  59. # socket.
  60. tmp = r.content
  61. if hasattr(r, "raw"):
  62. if r.raw != None:
  63. r.raw.close()
  64. r.raw = None
  65. return r
  66. except Exception as e:
  67. print()
  68. print(url)
  69. if hasattr(sys, "print_exception"):
  70. sys.print_exception(e)
  71. else:
  72. print(e)
  73. print()
  74. return None
  75. def get_image_path(self, path = ""):
  76. print("Checking for new APOD")
  77. r = self.fetch(self.path + "/" + path).text
  78. for line in r.splitlines():
  79. start = line.find('IMG SRC="')
  80. if start < 0:
  81. continue
  82. start += 9
  83. end = line.find('"', start)
  84. img = line[start : end]
  85. return self.path + "/" + img
  86. return None
  87. def download_image(self, path):
  88. print("Loading " + path)
  89. r = self.fetch(path).content
  90. scriptDir = os.path.dirname(os.path.realpath(__file__))
  91. imageDir = os.path.join(scriptDir, "images")
  92. imagePath = os.path.join(imageDir, "apod_" + os.path.basename(path))
  93. if os.path.isfile(imagePath):
  94. print("Image already loaded. Skip.")
  95. return imagePath
  96. print("Storing at " + imagePath)
  97. with open(imagePath, 'wb') as f:
  98. f.write(r)
  99. return imagePath
  100. def draw(self):
  101. if self.image != None:
  102. self.image.draw()
  103. if __name__ == "__main__":
  104. i = util.getInput()
  105. t = util.getTarget(i)
  106. s = APOD(t, i)
  107. util.loop(t, s.draw)