Skip to content

Instantly share code, notes, and snippets.

@GINK03
Last active December 30, 2020 07:26
Show Gist options
  • Save GINK03/077b582427f369c4c1d6b1b4e9c46b4e to your computer and use it in GitHub Desktop.
Save GINK03/077b582427f369c4c1d6b1b4e9c46b4e to your computer and use it in GitHub Desktop.
google_cals_cui
#!/usr/bin/env python
"""
Example application for the 'blessed' Terminal library for python.
This isn't a real progress bar, just a sample "animated prompt" of sorts that demonstrates the
separate move_x() and move_yx() capabilities, made mainly to test the `hpa' compatibility for
'screen' terminal type which fails to provide one, but blessed recognizes that it actually does, and
provides a proxy.
"""
from __future__ import print_function
# std imports
import sys
# local
from blessed import Terminal
from google_cals_base import base
import datetime
from dataclasses import dataclass
from dataclasses import asdict
import pandas as pd
import numpy as np
@dataclass
class Event:
d: datetime
summary: float
week: str
def build_events():
base_events = base()
events = []
for r in range(len(base_events)):
start = base_events[r]['start'].get('dateTime', base_events[r]['start'].get('date'))
d = datetime.datetime.strptime(start, "%Y-%m-%dT%H:%M:00+09:00")
events.append(Event(d=d, summary=base_events[r]["summary"], week=d.strftime("%a")))
# eventsでソートして
events.sort(key=lambda x: x.d)
# 一週間以内でfilterする
events = list(filter(lambda x: x.d - datetime.datetime.now() <= datetime.timedelta(days=7), events))
events = pd.DataFrame([asdict(event) for event in events])
events["day"] = events["d"].dt.strftime("%Y-%m-%d %a")
events["hour"] = events["d"].dt.strftime("%H")
events = events.pivot(index="hour", columns="day", values="summary")
return events[events.columns.tolist()[-7:]]
def fix_width(text):
fix = 0
for ch in text:
if ch.isascii():
fix += 0
else:
fix += 1
return fix
def main():
"""Program entry point."""
term = Terminal()
assert term.hpa(1) != u'', 'Terminal does not support hpa (Horizontal position absolute)'
col, offset = 1, 1
ROW_SIZE = 25
events = build_events()
for r in range(ROW_SIZE):
print()
sys.stdout.flush()
X = np.empty((25, len(events.columns)), dtype="<U256")
X[:, :] = np.nan
X[0, :] = events.columns.tolist()
with term.cbreak():
for x, column in enumerate(events.columns):
aday = events[[column]]
aday = aday.reset_index()
for hour, summary in zip(aday["hour"], aday[column]):
X[int(hour), x] = summary if pd.notna(summary) else np.nan
cur_row = 1
cur_col = 0
inp = None
while inp != "q":
for row in range(X.shape[0]):
y = term.height - 26 + row
if row - 1 == -1:
continue
sys.stderr.write(term.move_yx(y, 0) + f"{row-1: >03d}|")
sys.stderr.flush()
if inp == "j":
cur_row += 1
cur_row = min(X.shape[0], cur_row)
elif inp == "k":
cur_row -= 1
cur_row = max(0, cur_row)
elif inp == "h":
cur_col -= 1
cur_col = max(0, cur_col)
elif inp == "l":
cur_col += 1
cur_col = min(X.shape[1], cur_col)
buff = ""
for row in range(X.shape[0]):
for col in range(X.shape[1]):
y = term.height - 26 + row
x = 5 + (term.width // X.shape[1]) * col
width = term.width // X.shape[1] - 2
text = X[row, col]
if row == cur_row and col == cur_col:
buff += (
term.move_yx(y, x)
+ term.underline
+ term.bold
+ "{text:{fill}{align}{width}}|".format(
text=(str(text)[: width - 6] if text != "nan" else "-"),
fill=" ",
align="<" if text != "nan" and row != 0 else "^",
width=width - 3 - fix_width(text),
)
+ term.normal
)
buff += (
term.green
+ term.underline
+ term.bold
+ term.move_yx(term.height, 0)
+ f"{text: ^{term.width//2}}"
+ term.normal
)
elif row == cur_row or col == cur_col:
buff += (
term.move_yx(y, x)
+ term.bold
+ "{text:{fill}{align}{width}}|".format(
text=(str(text)[: width - 6] if text != "nan" else "-"),
fill=" ",
align="<" if text != "nan" and row != 0 else "^",
width=width - 3 - fix_width(text),
)
+ term.normal
)
else:
buff += term.move_yx(y, x) + "{text:{fill}{align}{width}}|".format(
text=str(text)[: width - 6] if text != "nan" else "-",
fill=" ",
align="<" if text != "nan" and row != 0 else "^",
width=width - 3 - fix_width(text),
)
sys.stderr.write(buff)
sys.stderr.flush()
inp = term.inkey(0.04)
print(file=sys.stderr)
if __name__ == '__main__':
main()
from __future__ import print_function
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']
def base():
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file('client_secret_1061473190594-fkm2feaplgmcfk2ppl4pfmtbk58npgri.apps.googleusercontent.com.json', SCOPES)
creds = flow.run_console()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('calendar', 'v3', credentials=creds)
timeMin = datetime.datetime.utcnow() - datetime.timedelta(hours=24 * 7)
timeMin = timeMin.isoformat() + 'Z'
events_result = service.events().list(calendarId='primary', timeMin=timeMin, maxResults=30, singleEvents=True, orderBy='startTime').execute()
events = events_result.get('items', [])
if not events:
print('No upcoming events found.')
return None
return events
if __name__ == '__main__':
base()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment