This commit is contained in:
Klafyvel 2017-12-18 15:09:51 +01:00
parent 8bce965dbd
commit ace7b89541

52
main.py
View file

@ -33,6 +33,8 @@ import datetime
import httplib2
import os
import requests
import logging
from logging.handlers import RotatingFileHandler
from apiclient import discovery
from oauth2client import client
@ -41,6 +43,45 @@ from oauth2client.file import Storage
import icalendar
DEBUG = False
APP_DIR = os.path.dirname(os.path.abspath(__file__))
if DEBUG:
DATA_DIR = APP_DIR
else:
home_dir = os.path.expanduser('~')
DATA_DIR = os.path.join(home_dir, ".ntnoe", "")
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
##### Logger stuff #####
logger = logging.getLogger()
if DEBUG:
logger.setLevel(logging.DEBUG)
else:
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
file_handler = RotatingFileHandler(
os.path.join(DATA_DIR, 'log.txt'), 'a', 1000000, 1)
if DEBUG:
file_handler.setLevel(logging.DEBUG)
else:
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
if DEBUG:
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
########################
TIMEDELTA_SYNCHRO = datetime.timedelta(days=15) # Number of days to look for
# for synchronization
@ -112,8 +153,7 @@ def get_credentials():
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
credential_dir = os.path.join(DATA_DIR, 'credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, 'ntnoe.json')
@ -123,7 +163,7 @@ def get_credentials():
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
print('Storing credentials to ' + credential_path)
logger.info('Storing credentials to ' + credential_path)
credentials = tools.run_flow(flow, store)
return credentials
@ -158,7 +198,7 @@ def main():
ntnoe_calendar_id = c['id']
if not ntnoe_calendar_id:
print("Creating ntnoe calendar...")
logger.info("Creating ntnoe calendar...")
created = service.calendars().insert(body={
'defaultReminders' : [],
'selected' : True,
@ -175,7 +215,7 @@ def main():
).execute()
for event in former_ones['items']:
print('Deleting event : {}'.format(event['id']))
logger.info('Deleting event : {}'.format(event['id']))
service.events().delete(
calendarId=ntnoe_calendar_id,
eventId=event['id']
@ -196,7 +236,7 @@ def main():
calendarId=ntnoe_calendar_id,
body=event.as_google()
).execute()
print("Adding event : {}".format(event['id']))
logger.info("Adding event : {}".format(event['id']))
if __name__ == '__main__':