Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- from apiclient.discovery import build
- from httplib2 import Http
- from oauth2client import file, client, tools
- from docx import Document
- import sys
- import re
- import requests
- import datetime
- path = "/home/zander/Downloads/Sommerferieplan-2018.docx"
- doc = Document(path)
- name = "malene"
- tables = doc.tables
- def readTable(table):
- w = False
- typ = table.rows[0].cells[0].paragraphs[0].text.lower().strip()
- if 'aften' not in typ and 'weekender' not in typ and 'dag' != typ:
- return [], None
- if typ == 'weekender':
- w = True
- weeks = [x.cells[0].paragraphs[0].text for x in table.rows][1:]
- weeks = [re.match(".*\s(\d+)", x)[1] for x in weeks]
- weeks = [int(x) for x in weeks]
- mIndex = []
- i = 0
- j = 0
- for row in table.rows:
- j = 0
- for cell in row.cells:
- for p in cell.paragraphs:
- if p.text.lower() == name:
- mIndex.append((i-1,j-1))
- j += 1
- i += 1
- if w:
- return [weeks[x[0]] for x in mIndex], typ
- row = table.rows[0]
- times = [x.paragraphs[0].text for x in row.cells][1:]
- times = [re.match('.*?(\d+)-(\d+).*', x) for x in times]
- times = [(int(x[1]), int(x[2])) for x in times]
- return [(weeks[x[0]], times[x[1]]) for x in mIndex], typ
- def dayToDate(h, d, w, year=2018):
- d = d + 1
- d = d % 7
- time = str(h).zfill(2) + ":" + str(0).zfill(2)
- date = datetime.datetime.strptime(str(year)+"-W" + str(w)+"-" + str(d) + "-" + time, "%Y-W%W-%w-%H:%M")
- return date
- def DayToStartEnd(tup, d):
- startW, time = tup
- endW = startW
- startH, endH = time
- startD = d
- endD = d+1 if (endH <= startH) else d
- if endD == 7:
- d = 0
- endW += 1
- start = dayToDate(startH, startD, startW)
- end = dayToDate(endH, endD, endW)
- return (start,end)
- def dayTupToSpan(tup):
- return [DayToStartEnd(tup, d) for d in range(0, 5)]
- def nightTupToSpan(tup):
- return [DayToStartEnd(tup, d) for d in range(0, 4)]
- def weekToSpan(w):
- return [
- DayToStartEnd((w, (12, 20)), 4),
- DayToStartEnd((w, (9, 19)), 5),
- DayToStartEnd((w, (9, 17)), 6)
- ]
- def tableToSpan(table):
- tups, typ = readTable(table)
- if typ is None:
- return []
- if "dag" in typ:
- f = dayTupToSpan
- if "aften" in typ:
- f = nightTupToSpan
- if "week" in typ:
- f = weekToSpan
- arr = [f(tup) for tup in tups]
- return sum(arr, [])
- spans = sum([tableToSpan(x) for x in tables], [])
- spans = sorted(spans)
- def merge_date_ranges(data):
- result = []
- t_old = data[0]
- for t in data[1:]:
- if t_old[1] >= t[0]: #I assume that the data is sorted already
- t_old = ((min(t_old[0], t[0]), max(t_old[1], t[1])))
- else:
- result.append(t_old)
- t_old = t
- else:
- result.append(t_old)
- return result
- spans = merge_date_ranges(spans)
- def spanToEvent(tup):
- s,e = tup
- return {
- 'summary': 'Arbejde',
- 'location': 'VBM Laboratoriet A/S - Industrivej 1, 9440 Aabybro',
- 'start': {
- 'dateTime': s.isoformat('T'),
- 'timeZone': 'Europe/Copenhagen'
- },
- 'end': {
- 'dateTime': e.isoformat('T'),
- 'timeZone': 'Europe/Copenhagen'
- },
- 'reminders': {
- 'useDefault': False
- }
- }
- events = [spanToEvent(x) for x in spans]
- # Setup the Calendar API
- SCOPES = 'https://www.googleapis.com/auth/calendar'
- store = file.Storage('credentials.json')
- creds = store.get()
- if not creds or creds.invalid:
- flow = client.flow_from_clientsecrets('client_secret.json', SCOPES)
- creds = tools.run_flow(flow, store)
- service = build('calendar', 'v3', http=creds.authorize(Http()))
- calendarId = None
- for cal in service.calendarList().list().execute()['items']:
- if 'malene' in cal['summary'].lower() and 'arbejde' in cal['summary'].lower():
- calendarId = cal['id']
- if calendarId is None:
- calendar = {
- 'summary': 'Malene - Arbejde',
- 'timeZone': 'Europe/Copenhagen'
- }
- created_calendar = service.calendars().insert(body=calendar).execute()
- calendarId = created_calendar['id']
- def addEvent(calId, event):
- service.events().insert(calendarId=calId, body=event).execute()
- for e in events:
- addEvent(calendarId, e)
- print("tilføjet :-)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement