xprize-discord-bot / redis2sheets_img.py
daviddao's picture
updatable script
c62379b
import redis
import gspread
redis_url = 'redis://default:[email protected]:7369'
r = redis.from_url(redis_url)
gc = gspread.service_account()
# Open a sheet from a spreadsheet in one go
wks = gc.open("Labels").sheet1
rows = [['image id', 'label id', 'author', 'author_id', 'role', 'submitted_on', 'label', 'confidence (0 = low, 1 = medium, 2 = high)', 'image url', 'dashboard url', 'image preview']]
redis_keys = r.keys('label:image:*')
for i_key in redis_keys:
image_data_bytes = r.hgetall(i_key)
image_data = {k.decode('utf-8'): v.decode('utf-8') for k, v in image_data_bytes.items()}
image_id = i_key.decode().split(':')[-2]
raw_image_data_bytes = r.hgetall(f'image:{image_id}'.encode('utf-8'))
raw_image_data = {k.decode('utf-8'): v.decode('utf-8') for k, v in raw_image_data_bytes.items()}
try:
path = f'https://gainforest-transparency-dashboard.s3.amazonaws.com/{raw_image_data["awsCID"]}'
except:
path = f'https://twitter.com/GainForestNow/photo.jpg'
try:
dashboard_url = f'https://gainforest.app/observations/{raw_image_data["uuid"]}'
except:
dashboard_url = f'no'
row = [image_id, i_key.decode().split(':')[-1], image_data['author'], image_data['author_id'], image_data['role'], image_data['timestamp'], image_data['label'], image_data['confidence'], path, dashboard_url,'=IMAGE("{}", 2)'.format(path)]
rows.append(row)
# Update a range of cells using the top left corner address
wks.update('A1', rows, value_input_option='USER_ENTERED')
wks.format('A1:I1', {'textFormat': {'bold': True}})