Add basic auth ureqeusts and elementtree for xml.

Implement nextcloud directory listing.
This commit is contained in:
2023-10-28 21:45:05 -04:00
parent 42a6326591
commit ae8bafc8c3
6 changed files with 336 additions and 8 deletions

View File

@@ -1,25 +1,51 @@
import ElementTree
from urllib import urequest
import urequests
from urequests import auth
import gc
import uos
class uNextcloud:
def __init__(self):
self.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))
self.sd = sdcard.SDCard(sd_spi, machine.Pin(22))
class File:
def __init__(self, url_path, mimetype):
self.url_path = url_path
self.mimetype = mimetype
def get_url(self):
return self.url_path
def get_mimetype(self):
return self.mimetype
def __init__(self, nextcloud_url):
self.url = nextcloud_url
self.username = None
self.password = None
uos.mount(sd, "/sd")
gc.collect()
def set_auth(self, username, password):
self.username = username
self.password = password
def get_folder_items(self, folder_path):
pass
def get_folder_items(self, folder_path=""):
response = urequests.request("PROPFIND", self.url+"/remote.php/dav/files/"+self.username+"/"+folder_path, auth=urequests.auth.HTTPBasicAuth(self.username, self.password))
if 200 <= response.status_code < 300:
file_list = []
try:
xml = ElementTree.fromstring(response.text)
for resp in xml:
href = resp.find_first_by_tag("href")
propstat = resp.find_first_by_tag("propstat")
prop = propstat.find_first_by_tag("prop")
content_type = prop.find_first_by_tag("getcontenttype")
if content_type != None:
file_list.append(self.File(href.text, content_type.text))
return file_list
except AttributeError:
return None
else:
return None
def download_file_to_path(self, folder_path, file, destination_path):
pass