Due to high demand expect some shipping delays at this time, orders may not ship for 3-4 business days. On MLK Day no orders will be shipped.
0

Pyportal - Dropbox Get file
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Pyportal - Dropbox Get file

by grafer01 on Wed Dec 08, 2021 6:42 pm

Hello,

Just got my Pyportal and I'm trying to follow the guides to download a csv file form a dropbox location so I can parse it and display on my pyportal.

I'm able to do it on my linux machine with the package requests. but I'm running into issues using adafruit_requests because there doesn't seem to be an ability to authenticate.

Does anyone know the equivalent package for the circuitpython that will do the following below? Or can I just place my requests package onto my pyportal and will it work?

Thanks in advance.

Code: Select all | TOGGLE FULL SIZE
import requests as requests
from requests.auth import HTTPBasicAuth
import pandas as pd
import time
import board
from adafruit_pyportal import PyPortal

url = 'https://www.dropbox.com/s/csvfile.csv?dl=1'
db_username = 'tochange'
db_password = 'tochange'

downloaded_file = requests.get(url, auth=HTTPBasicAuth(db_username, db_password))

dest_file = open('/presently-backup.csv', 'wb+')

dest_file.write(downloaded_file.content)

#df = pd.read_csv('/home/pumpkin/Documents/PyPortal/presently/presently-backup.csv', usecols= ['entryDate','entryContent'])
#print(df)
import csv

with open('/presently-backup.csv', newline='') as f:
    reader = csv.reader(f)
    data = list(reader)

# Using for loop
for i in data:
    print(i)

grafer01
 
Posts: 1
Joined: Wed Sep 02, 2020 10:42 pm

Re: Pyportal - Dropbox Get file

by mikeysklar on Thu Dec 09, 2021 6:01 pm

You can run adafruit_requests right on your pyportal. Is that what you are asking?

https://learn.adafruit.com/pyportal-obl ... 3034798-22

Code: Select all | TOGGLE FULL SIZE
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

# adafruit_requests usage with an esp32spi_socket
import board
import busio
from digitalio import DigitalInOut
import adafruit_esp32spi.adafruit_esp32spi_socket as socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_requests as requests

# Add a secrets.py to your filesystem that has a dictionary called secrets with "ssid" and
# "password" keys with your WiFi credentials. DO NOT share that file or commit it into Git or other
# source control.
# pylint: disable=no-name-in-module,wrong-import-order
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

# If you are using a board with pre-defined ESP32 Pins:
esp32_cs = DigitalInOut(board.ESP_CS)
esp32_ready = DigitalInOut(board.ESP_BUSY)
esp32_reset = DigitalInOut(board.ESP_RESET)

# If you have an externally connected ESP32:
# esp32_cs = DigitalInOut(board.D9)
# esp32_ready = DigitalInOut(board.D10)
# esp32_reset = DigitalInOut(board.D5)

# If you have an AirLift Featherwing or ItsyBitsy Airlift:
# esp32_cs = DigitalInOut(board.D13)
# esp32_ready = DigitalInOut(board.D11)
# esp32_reset = DigitalInOut(board.D12)

spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)

print("Connecting to AP...")
while not esp.is_connected:
    try:
        esp.connect_AP(secrets["ssid"], secrets["password"])
    except RuntimeError as e:
        print("could not connect to AP, retrying: ", e)
        continue
print("Connected to", str(esp.ssid, "utf-8"), "\tRSSI:", esp.rssi)

# Initialize a requests object with a socket and esp32spi interface
socket.set_interface(esp)
requests.set_socket(socket, esp)

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
JSON_GET_URL = "https://httpbin.org/get"
JSON_POST_URL = "https://httpbin.org/post"

print("Fetching text from %s" % TEXT_URL)
response = requests.get(TEXT_URL)
print("-" * 40)

print("Text Response: ", response.text)
print("-" * 40)
response.close()

print("Fetching JSON data from %s" % JSON_GET_URL)
response = requests.get(JSON_GET_URL)
print("-" * 40)

print("JSON Response: ", response.json())
print("-" * 40)
response.close()

data = "31F"
print("POSTing data to {0}: {1}".format(JSON_POST_URL, data))
response = requests.post(JSON_POST_URL, data=data)
print("-" * 40)

json_resp = response.json()
# Parse out the 'data' key from json_resp dict.
print("Data received from server:", json_resp["data"])
print("-" * 40)
response.close()

json_data = {"Date": "July 25, 2019"}
print("POSTing data to {0}: {1}".format(JSON_POST_URL, json_data))
response = requests.post(JSON_POST_URL, json=json_data)
print("-" * 40)

json_resp = response.json()
# Parse out the 'json' key from json_resp dict.
print("JSON Data received from server:", json_resp["json"])
print("-" * 40)
response.close()

mikeysklar
 
Posts: 6466
Joined: Mon Aug 01, 2016 8:10 pm

Please be positive and constructive with your questions and comments.