Skip to content

Commit 8727c05

Browse files
authored
Merge pull request #1989 from PaintYourDragon/pb-magtag-google-sheets
Code now uses TSV instead of JSON to fix Google V4 API issue
2 parents 1199650 + 4659ca8 commit 8727c05

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

MagTag_Google_Sheets/naughty_nice/code.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Google Sheets to MagTag example: Naughty or Nice?
3-
Gets JSON spreadsheet from Google, displays names from one column or other.
3+
Gets tab-separated-value (TSV) spreadsheet from Google, displays names from
4+
one column or other.
45
"Smart cursive" font by Thomas A. Fine, helvB12 from Xorg fonts.
56
"""
67

@@ -13,7 +14,7 @@
1314

1415
# CONFIGURABLE SETTINGS and ONE-TIME INITIALIZATION ------------------------
1516

16-
JSON_URL = 'https://spreadsheets.google.com/feeds/cells/1Tk943egFNDV7TmXGL_VspYyWKELeJO8gguAmNSgLDbk/1/public/full?alt=json'
17+
TSV_URL = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vTA8pXQodbEiz5idGT21YkL1Vy8waW0aAHM1uX7D4TqBq6DrUU8qXVlON1QVaWSlmoC3OBL4Iokyiyy/pub?output=tsv'
1718
NICE = True # Use 'True' for nice list, 'False' for naughty
1819
TWELVE_HOUR = True # If set, show 12-hour vs 24-hour (e.g. 3:00 vs 15:00)
1920
DD_MM = False # If set, show DD/MM instead of MM/DD dates
@@ -85,9 +86,9 @@ def hh_mm(time_struct, twelve_hour=True):
8586
print(NOW)
8687

8788
print('Updating names')
88-
RESPONSE = MAGTAG.network.fetch(JSON_URL)
89+
RESPONSE = MAGTAG.network.fetch(TSV_URL)
8990
if RESPONSE.status_code == 200:
90-
JSON_DATA = RESPONSE.json()
91+
TSV_DATA = RESPONSE.text
9192
print('OK')
9293

9394
# Set the "Updated" date and time label
@@ -98,26 +99,26 @@ def hh_mm(time_struct, twelve_hour=True):
9899
MAGTAG.set_text('Updated %s %s' % (DATE, hh_mm(NOW, TWELVE_HOUR)), 1,
99100
auto_refresh=False)
100101

101-
ENTRIES = JSON_DATA['feed']['entry'] # List of cell data
102+
# Split text response into separate lines
103+
LINES = TSV_DATA.split('\r\n')
102104

103105
# Scan cells in row #1 to find the column number for naughty vs nice.
104106
# This allows the order of columns in the spreadsheet to be changed,
105107
# though they still must have a "Naughty" or "Nice" heading at top.
106-
for entry in ENTRIES:
107-
cell = entry['gs$cell']
108-
if int(cell['row']) == 1: # Only look at top row
109-
head = cell['$t'].lower() # Case-insensitive compare
110-
if ((NICE and head == 'nice') or (not NICE and head == 'naughty')):
111-
NAME_COLUMN = int(cell['col'])
108+
cells = LINES[0].split("\t") # Tab-separated values
109+
for column, entry in enumerate(cells):
110+
head = entry.lower() # Case-insensitive compare
111+
if ((NICE and head == 'nice') or (not NICE and head == 'naughty')):
112+
NAME_COLUMN = column
112113

113114
# Now that we know which column number contains the names we want,
114115
# a second pass is made through all the cells. Items where row > 1
115116
# and column is equal to NAME_COLUMN are joined in a string.
116117
NAME_LIST = '' # Clear name list
117-
for entry in ENTRIES:
118-
cell = entry['gs$cell']
119-
if int(cell['row']) > 1 and int(cell['col']) is NAME_COLUMN:
120-
NAME_LIST += cell['$t'] + '\n' # Name + newline character
118+
for line in LINES[1:]: # Skip first line -- naughty/nice/notes in sheet
119+
cells = line.split("\t") # Tab-separated
120+
if len(cells) >= NAME_COLUMN and cells[NAME_COLUMN] != "":
121+
NAME_LIST += cells[NAME_COLUMN] + '\n' # Name + newline character
121122

122123
MAGTAG.set_text(NAME_LIST) # Update list on the display
123124

MagTag_Google_Sheets/weekly_planner/code.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""
22
Google Sheets to MagTag example: Weekly Planner.
3-
Gets JSON spreadsheet from Google, displays task list from today's column.
4-
This example does NOT deep sleep, a USB power connection is recommended.
3+
Gets tab-separated-value (TSV) spreadsheet from Google, displays task list
4+
from today's column. This example does NOT deep sleep, a USB power connection
5+
is recommended.
56
Fonts from Xorg project.
67
"""
78

@@ -14,7 +15,7 @@
1415

1516
# CONFIGURABLE SETTINGS and ONE-TIME INITIALIZATION ------------------------
1617

17-
JSON_URL = 'https://spreadsheets.google.com/feeds/cells/1vk6jE1-6CMV-hjDgBk-PuFLgG64YemyDoREhGrA6uGI/1/public/full?alt=json'
18+
TSV_URL = 'https://docs.google.com/spreadsheets/d/e/2PACX-1vR1WjUKz35-ek6SiR5droDfvPp51MTds4wUs57vEZNh2uDfihSTPhTaiiRovLbNe1mkeRgurppRJ_Zy/pub?output=tsv'
1819
TWELVE_HOUR = True # If set, show 12-hour vs 24-hour (e.g. 3:00 vs 15:00)
1920
DD_MM = False # If set, show DD/MM instead of MM/DD dates
2021
DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
@@ -91,21 +92,22 @@ def hh_mm(time_struct, twelve_hour=True):
9192
NOW = rtc.RTC().datetime
9293

9394
print('Updating tasks')
94-
RESPONSE = MAGTAG.network.fetch(JSON_URL)
95+
RESPONSE = MAGTAG.network.fetch(TSV_URL)
9596
if RESPONSE.status_code == 200:
96-
JSON_DATA = RESPONSE.json()
97+
TSV_DATA = RESPONSE.text
9798
print('OK')
9899

99-
ENTRIES = JSON_DATA['feed']['entry'] # List of cell data
100+
# Split text response into separate lines
101+
LINES = TSV_DATA.split('\r\n')
100102

101103
# tm_wday uses 0-6 for Mon-Sun, we want 1-7 for Sun-Sat
102104
COLUMN = (NOW.tm_wday + 1) % 7 + 1
103105

104106
TASK_LIST = '' # Clear task list string
105-
for entry in ENTRIES:
106-
cell = entry['gs$cell']
107-
if int(cell['row']) > 1 and int(cell['col']) is COLUMN:
108-
TASK_LIST += cell['$t'] + '\n' # Task + newline character
107+
for line in LINES[1:]: # Skip first line -- days of week in sheet
108+
cells = line.split("\t") # Tab-separated!
109+
if len(cells) >= COLUMN:
110+
TASK_LIST += cells[COLUMN - 1] + '\n'
109111

110112
# Refreshing the display is jarring, so only do it if the task list
111113
# or day has changed. This requires preserving state between passes,

0 commit comments

Comments
 (0)