|
#!/usr/bin/env python |
|
|
|
from __future__ import print_function |
|
import httplib2 |
|
import os |
|
import sys |
|
import re |
|
from ofls import Shift |
|
|
|
|
|
from apiclient import discovery |
|
from oauth2client import client |
|
from oauth2client import tools |
|
from oauth2client.file import Storage |
|
|
|
import datetime |
|
|
|
try: |
|
import argparse |
|
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args() |
|
except ImportError: |
|
flags = None |
|
|
|
# If modifying these scopes, delete your previously saved credentials |
|
# at ~/.credentials/calendar-python-quickstart.json |
|
SCOPES = 'https://www.googleapis.com/auth/calendar' |
|
CLIENT_SECRET_FILE = 'client_secret.json' |
|
APPLICATION_NAME = 'OFLS shift calendar register' |
|
|
|
PERIOD_TIME = [("09:00:00", "10:30:00"), |
|
("10:40:00", "12:10:00"), |
|
("12:10:00", "13:10:00"), |
|
("13:10:00", "14:40:00"), |
|
("14:50:00", "16:20:00"), |
|
("16:30:00", "18:00:00"), |
|
("18:00:00", "19:00:00")] |
|
|
|
|
|
def get_credentials(): |
|
"""Gets valid user credentials from storage. |
|
If nothing has been stored, or if the stored credentials are invalid, |
|
the OAuth2 flow is completed to obtain the new credentials. |
|
Returns: |
|
Credentials, the obtained credential. |
|
""" |
|
home_dir = os.path.expanduser('~') |
|
credential_dir = os.path.join(home_dir, '.credentials') |
|
if not os.path.exists(credential_dir): |
|
os.makedirs(credential_dir) |
|
credential_path = os.path.join(credential_dir, |
|
'calendar-python-quickstart.json') |
|
|
|
store = Storage(credential_path) |
|
credentials = store.get() |
|
if not credentials or credentials.invalid: |
|
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) |
|
flow.user_agent = APPLICATION_NAME |
|
if flags: |
|
credentials = tools.run_flow(flow, store, flags) |
|
else: # Needed only for compatibility with Python 2.6 |
|
credentials = tools.run(flow, store) |
|
print('Storing credentials to ' + credential_path) |
|
return credentials |
|
|
|
|
|
def inseart_ofls_shift(week_num, shift=None): |
|
|
|
credentials = get_credentials() |
|
http = credentials.authorize(httplib2.Http()) |
|
service = discovery.build('calendar', 'v3', http=http) |
|
|
|
try: |
|
key = os.environ["OFLS_KEY"] |
|
gid = os.environ["OFLS_GID"] |
|
my_name = os.environ["OFLS_NAME"] |
|
except Exception as e: |
|
print("key or gid is invalid", e) |
|
sys.exit(1) |
|
|
|
if shift is None: |
|
shift = Shift(key, gid) |
|
|
|
week_shift = shift.get_week_shift_list(str(week_num)) |
|
|
|
r = re.compile(my_name + "$|^" + my_name + ",") |
|
|
|
rdate = re.compile("\d*\/\d*") |
|
today = datetime.datetime.today() |
|
|
|
for shift in week_shift: |
|
for idx, names in enumerate(shift.table): |
|
if r.search(names): |
|
date_str = rdate.match(shift.date).group() |
|
|
|
print(idx) |
|
start = datetime.datetime.strptime("%s:%s" % ( |
|
date_str, PERIOD_TIME[idx][0]), "%m/%d:%H:%M:%S").replace(year=today.year) |
|
|
|
end = datetime.datetime.strptime("%s:%s" % ( |
|
date_str, PERIOD_TIME[idx][1]), "%m/%d:%H:%M:%S").replace(year=today.year) |
|
|
|
body = { |
|
"summary": "支援室", |
|
"start": { |
|
"dateTime": start.strftime("%Y-%m-%dT%H:%M:%S"), |
|
"timeZone": "Asia/Tokyo", |
|
}, |
|
"end": { |
|
"dateTime": end.strftime("%Y-%m-%dT%H:%M:%S"), |
|
"timeZone": "Asia/Tokyo", |
|
}, |
|
} |
|
|
|
event = service.events().insert(calendarId='primary', body=body).execute() |
|
|
|
|
|
def main(): |
|
# inseart_ofls_shift(0) |
|
inseart_ofls_shift(1) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |