linting
This commit is contained in:
1 parent
2610d484e8
commit
b7e2706b12
6 files changed
+163
-24
No files matched your search
+11
-7
@@ -1,5 +1,5 @@
|
||||
import requests
|
||||
import warnings
|
||||
import httpx
|
||||
|
||||
|
||||
class Api:
|
||||
@@ -8,23 +8,27 @@ class Api:
|
||||
self.base_64 = base_64
|
||||
self.access_token = None
|
||||
self.header = None
|
||||
self._client = httpx.AsyncClient(timeout=httpx.Timeout(5.0, connect=5.0))
|
||||
|
||||
def refreshAuth(self) -> None:
|
||||
async def refreshAuth(self) -> None:
|
||||
uri = "https://accounts.spotify.com/api/token"
|
||||
data = {
|
||||
"grant_type": "refresh_token",
|
||||
"refresh_token": self.refresh_token,
|
||||
}
|
||||
req = requests.post(
|
||||
uri, data=data, headers={"Authorization": "Basic " + self.base_64}
|
||||
).json()
|
||||
res = await self._client.post(
|
||||
uri,
|
||||
data=data,
|
||||
headers={"Authorization": "Basic " + self.base_64},
|
||||
)
|
||||
req = res.json()
|
||||
self.access_token = req["access_token"]
|
||||
self.header = {"Authorization": f"Bearer {self.access_token}"}
|
||||
return req["expires_in"]
|
||||
|
||||
def getPlaying(self):
|
||||
async def getPlaying(self):
|
||||
url = "https://api.spotify.com/v1/me/player/currently-playing"
|
||||
req = requests.get(url, headers=self.header)
|
||||
req = await self._client.get(url, headers=self.header)
|
||||
if req.status_code == 204:
|
||||
return "not-playing"
|
||||
if req.status_code == 401:
|
||||
|
||||
@@ -56,10 +56,14 @@ class RaspberryPi:
|
||||
# self.GPIO.cleanup()
|
||||
self.GPIO.setmode(self.GPIO.BCM)
|
||||
self.GPIO.setwarnings(False)
|
||||
self.GPIO.setup(self.RST_PIN, self.GPIO.OUT)
|
||||
self.GPIO.setup(self.DC_PIN, self.GPIO.OUT)
|
||||
self.GPIO.setup(self.BL_PIN, self.GPIO.OUT)
|
||||
self.GPIO.output(self.BL_PIN, self.GPIO.HIGH)
|
||||
# self.GPIO.setup(self.RST_PIN, self.GPIO.OUT)
|
||||
# self.GPIO.setup(self.DC_PIN, self.GPIO.OUT)
|
||||
# self.GPIO.setup(self.BL_PIN, self.GPIO.OUT)
|
||||
# self.GPIO.output(self.BL_PIN, self.GPIO.HIGH)
|
||||
self.GPIO.setup(self.RST_PIN, self.GPIO.OUT, initial=self.GPIO.HIGH)
|
||||
self.GPIO.setup(self.DC_PIN, self.GPIO.OUT, initial=self.GPIO.LOW)
|
||||
self.GPIO.setup(self.BL_PIN, self.GPIO.OUT, initial=self.GPIO.HIGH)
|
||||
|
||||
# Initialize SPI
|
||||
self.SPI = spidev.SpiDev()
|
||||
self.SPI.open(0, 0)
|
||||
@@ -86,9 +90,13 @@ class RaspberryPi:
|
||||
self._pwm.ChangeFrequency(freq)
|
||||
|
||||
def module_init(self):
|
||||
self.GPIO.setup(self.RST_PIN, self.GPIO.OUT)
|
||||
self.GPIO.setup(self.DC_PIN, self.GPIO.OUT)
|
||||
self.GPIO.setup(self.BL_PIN, self.GPIO.OUT)
|
||||
# self.GPIO.setup(self.RST_PIN, self.GPIO.OUT)
|
||||
# self.GPIO.setup(self.DC_PIN, self.GPIO.OUT)
|
||||
# self.GPIO.setup(self.BL_PIN, self.GPIO.OUT)
|
||||
self.GPIO.setup(self.RST_PIN, self.GPIO.OUT, initial=self.GPIO.HIGH)
|
||||
self.GPIO.setup(self.DC_PIN, self.GPIO.OUT, initial=self.GPIO.LOW)
|
||||
self.GPIO.setup(self.BL_PIN, self.GPIO.OUT, initial=self.GPIO.HIGH)
|
||||
|
||||
self._pwm = self.GPIO.PWM(self.BL_PIN, self.BL_freq)
|
||||
self._pwm.start(100)
|
||||
if self.SPI != None:
|
||||
|
||||
+13
-9
@@ -9,6 +9,7 @@ from datetime import datetime
|
||||
import yaml
|
||||
from typing import Dict
|
||||
import subprocess
|
||||
import asyncio
|
||||
|
||||
import numpy as np
|
||||
import psutil
|
||||
@@ -62,7 +63,7 @@ def parse_config(path="spotiplayer_pi/config.yaml"):
|
||||
return data
|
||||
|
||||
|
||||
def display_loop(api: Api, cfg: Dict):
|
||||
async def display_loop(api: Api, cfg: Dict):
|
||||
try:
|
||||
d = LCD_2inch()
|
||||
d.Init()
|
||||
@@ -98,12 +99,12 @@ def display_loop(api: Api, cfg: Dict):
|
||||
|
||||
while True:
|
||||
if time.time() - last_auth_refresh >= auth_interval:
|
||||
auth_interval = api.refreshAuth() - 120
|
||||
auth_interval = await api.refreshAuth() - 120
|
||||
last_auth_refresh = time.time()
|
||||
print(f"Refreshed auth at {datetime.now().strftime('%d-%m %H:%M:%S')}")
|
||||
|
||||
if time.time() - last_api_call >= cfg["api_interval"]:
|
||||
data = api.getPlaying()
|
||||
data = await api.getPlaying()
|
||||
last_api_call = time.time()
|
||||
if data == None:
|
||||
warnings.warn("No data found")
|
||||
@@ -129,7 +130,10 @@ def display_loop(api: Api, cfg: Dict):
|
||||
idx = cpu_values_idxs[i]
|
||||
val, prev_val = cpu_values[i], cpu_values[i - 1]
|
||||
draw.rectangle(
|
||||
[(idx, 200 - val), (idx - 1, 200 - prev_val)],
|
||||
[
|
||||
(min(idx - 1, idx), min(200 - prev_val, 200 - val)),
|
||||
(max(idx - 1, idx), max(200 - prev_val, 200 - val)),
|
||||
],
|
||||
fill=(255, 253, 195),
|
||||
)
|
||||
draw.text(
|
||||
@@ -185,8 +189,8 @@ def display_loop(api: Api, cfg: Dict):
|
||||
idx = len(binned_data) - idx_ - 1
|
||||
draw.rectangle(
|
||||
[
|
||||
(321 - ((idx + 1) * 16), 240),
|
||||
(319 - (idx * 16), 240 - bin_i // 65),
|
||||
(321 - ((idx + 1) * 16), min(240, 240 - bin_i // 65)),
|
||||
(319 - (idx * 16), max(240, 240 - bin_i // 65)),
|
||||
],
|
||||
fill=tuple(cmap2[idx]),
|
||||
)
|
||||
@@ -293,10 +297,10 @@ def display_loop(api: Api, cfg: Dict):
|
||||
fill=cfg["color_theme"]["text"],
|
||||
)
|
||||
d.ShowImage(bg)
|
||||
time.sleep(cfg["refresh_interval"])
|
||||
await asyncio.sleep(cfg["refresh_interval"])
|
||||
|
||||
except IOError as e:
|
||||
raise e
|
||||
print(f"IOError: {e}")
|
||||
except KeyboardInterrupt:
|
||||
d.module_exit()
|
||||
|
||||
@@ -304,4 +308,4 @@ def display_loop(api: Api, cfg: Dict):
|
||||
if __name__ == "__main__":
|
||||
cfg = parse_config()
|
||||
api = Api(cfg["refresh_token"], cfg["base_64"])
|
||||
display_loop(api, cfg)
|
||||
asyncio.run(display_loop(api, cfg))
|
||||
Reference in new issue
Block a user