Add in more garbage collecting and initial implementation of file download.
This commit is contained in:
47
main.py
47
main.py
@@ -1,8 +1,51 @@
|
||||
import sdcard
|
||||
import uNextcloud
|
||||
import inky_helper as ih
|
||||
import gc
|
||||
import sdcard, uos
|
||||
import random
|
||||
from picographics import PicoGraphics, DISPLAY_INKY_FRAME_7 as DISPLAY
|
||||
from jpegdec import JPEG
|
||||
|
||||
sd_spi = machine.SPI(0, sck=machine.Pin(18, machine.Pin.OUT), mosi=machine.Pin(19, machine.Pin.OUT), miso=machine.Pin(16, machine.Pin.OUT))
|
||||
from secrets import WIFI_SSID, WIFI_PASSWORD, NEXTCLOUD_USERNAME, NEXTCLOUD_PASSWORD
|
||||
|
||||
def get_new_image():
|
||||
sd_spi = machine.SPI(0, sck=machine.Pin(18, machine.Pin.OUT), mosi=machine.Pin(19, machine.Pin.OUT),
|
||||
miso=machine.Pin(16, machine.Pin.OUT))
|
||||
sd = sdcard.SDCard(sd_spi, machine.Pin(22))
|
||||
uos.mount(sd, "/sd")
|
||||
gc.collect()
|
||||
|
||||
ih.network_connect(WIFI_SSID, WIFI_PASSWORD)
|
||||
|
||||
nc = uNextcloud.uNextcloud("https://cloud.eyecreate.org")
|
||||
nc.set_auth(NEXTCLOUD_USERNAME, NEXTCLOUD_PASSWORD)
|
||||
response = nc.get_folder_items("eink-frame")
|
||||
|
||||
try:
|
||||
last = open('/sd/last_wallpaper', 'r')
|
||||
last_file = last.read()
|
||||
last.close()
|
||||
except OSError:
|
||||
last_file = ""
|
||||
|
||||
if response != None:
|
||||
for item in response:
|
||||
if item.url_path == last_file:
|
||||
response.remove(item)
|
||||
ran = random.choice(response)
|
||||
if ran.mimetype == "image/jpeg":
|
||||
update_last = open('/sd/last_wallpaper', 'w+')
|
||||
update_last.write(ran.url_path)
|
||||
update_last.close()
|
||||
print(ran.url_path)
|
||||
gc.collect()
|
||||
nc.download_file_to_path(ran.url_path, "/sd/current_image.jpg")
|
||||
graphics = PicoGraphics(DISPLAY)
|
||||
j = JPEG(graphics)
|
||||
|
||||
j.open_file("/sd/current_image.jpg")
|
||||
j.decode()
|
||||
graphics.update()
|
||||
#sleep?
|
||||
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ import ElementTree
|
||||
import urequests
|
||||
from urequests import auth
|
||||
import gc
|
||||
import uos
|
||||
|
||||
|
||||
class uNextcloud:
|
||||
@@ -41,12 +40,35 @@ class uNextcloud:
|
||||
content_type = prop.find_first_by_tag("getcontenttype")
|
||||
if content_type != None:
|
||||
file_list.append(self.File(href.text, content_type.text))
|
||||
response.close()
|
||||
gc.collect()
|
||||
return file_list
|
||||
except AttributeError:
|
||||
response.close()
|
||||
gc.collect()
|
||||
return None
|
||||
else:
|
||||
response.close()
|
||||
gc.collect()
|
||||
return None
|
||||
|
||||
def download_file_to_path(self, folder_path, file, destination_path):
|
||||
pass
|
||||
|
||||
def download_file_to_path(self, url_path, destination_path):
|
||||
response = urequests.request("GET", self.url+url_path, auth=urequests.auth.HTTPBasicAuth(self.username, self.password))
|
||||
if 200 <= response.status_code < 300:
|
||||
dest = open(destination_path, 'wb')
|
||||
data = bytearray(1024)
|
||||
len = 0
|
||||
while True:
|
||||
print(f"downloaded {len}")
|
||||
gc.collect()
|
||||
if response.raw.readinto(data) == 0:
|
||||
break
|
||||
len += 1024
|
||||
dest.write(data)
|
||||
print(gc.mem_free())
|
||||
dest.close()
|
||||
response.close()
|
||||
gc.collect()
|
||||
else:
|
||||
response.close()
|
||||
raise Exception()
|
||||
|
||||
Reference in New Issue
Block a user