diff options
author | emkael <emkael@tlen.pl> | 2016-03-08 14:12:04 +0100 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-03-08 14:23:59 +0100 |
commit | cc974c9ba84353d41e3c23d253f0311e6a6f092f (patch) | |
tree | 53424867130f820115b646f19ca81dbc66923add | |
parent | 7a566eb93a863174d66c399a5d7d1982fe4dcc78 (diff) |
* verbose calendar data import
-rw-r--r-- | bin/import_cals.py | 106 |
1 files changed, 79 insertions, 27 deletions
diff --git a/bin/import_cals.py b/bin/import_cals.py index c21eb0c..8b32691 100644 --- a/bin/import_cals.py +++ b/bin/import_cals.py @@ -5,6 +5,8 @@ import time import urllib2 import urlparse +from sqlalchemy import inspect + from rcal.db import Session from rcal.model import Calendar, Category @@ -17,9 +19,7 @@ CONFIG = json.load(open( 'r')) -def main(): - cal_list = None - +def get_cal_list(): cache_path = os.path.join( os.path.dirname(os.path.realpath(__file__)), CONFIG['cache']) @@ -35,35 +35,87 @@ def main(): cache_file.close() else: cal_list = open(cache_path, 'r').read() + return cal_list - session = Session.create() - ical_markdown = re.compile(r'^\[iCal\]\((.*)\)$') +def update_calendar(cal, session): + db_cal = Calendar.fetch(cal['uid'], session) - cells = [row.split('|') for row in cal_list.split('\n')] + if inspect(db_cal).pending: + print 'Adding calendar %s (%s)' % (cal['name'], cal['uid']) + db_cal.name = cal['name'] + db_cal.url = cal['url'] + db_cal.website = cal['website'] + db_cal.category = Category.fetch(cal['category'], session) + + if db_cal.name != cal['name']: + print 'Updating calendar name: %s -> %s (%s)' % ( + db_cal.name, cal['name'], db_cal.uid) + db_cal.name = cal['name'] + if db_cal.url != cal['url']: + print 'Updating calendar url: %s -> %s (%s)' % ( + db_cal.url, cal['url'], db_cal.uid) + db_cal.url = cal['url'] + if db_cal.website != cal['website']: + print 'Updating calendar website: %s -> %s (%s)' % ( + db_cal.website, cal['website'], db_cal.uid) + db_cal.website = cal['website'] + if db_cal.category.name != cal['category']: + print 'Updating calendar category: %s -> %s (%s)' % ( + db_cal.category.name, cal['category'], db_cal.uid) + db_cal.category = Category.fetch(cal['category'], session) + + +def get_imported_calendars(cells, ical_markdown): + imported_calendars = [] for row in cells: - if len(row) == 7: - row = [r for r in row if r] - if len(row) == 5: - markdown_match = re.match(ical_markdown, row[2]) - if markdown_match: - ical_url = urlparse.urlparse(markdown_match.group(1)) - if ical_url.netloc == 'calendar.google.com': - ical_path = re.sub( - '^/?calendar/ical/', '', ical_url.path).split('/') - if len(ical_path) == 3: - calendar_uid = ical_path[0] - calendar = Calendar.fetch( - calendar_uid, - session, - row[0], - ical_url.geturl()) - calendar.website = row[4] - calendar.category = Category.fetch(row[1], session) - else: - print ical_url.geturl() + row = [r for r in row if r] + if len(row) == 5: + markdown_match = re.match(ical_markdown, row[2]) + if markdown_match: + ical_url = urlparse.urlparse(markdown_match.group(1)) + if ical_url.netloc == 'calendar.google.com': + ical_path = re.sub( + '^/?calendar/ical/', '', ical_url.path).split('/') + if len(ical_path) == 3: + imported_calendars.append({ + 'uid': ical_path[0], + 'url': ical_url.geturl(), + 'name': row[0], + 'website': row[4], + 'category': row[1] + }) else: - print ical_url.geturl() + print 'Unknown iCal URL format: %s' % ( + ical_url.geturl()) + else: + print 'Unknown iCal URL format: %s' % ( + ical_url.geturl()) + return imported_calendars + + +def main(): + session = Session.create() + + cal_list = get_cal_list() + + ical_markdown = re.compile(r'^\[iCal\]\((.*)\)$') + cells = [row.split('|') for row in cal_list.split('\n')] + + imported_calendars = get_imported_calendars(cells, ical_markdown) + imported_calendar_uids = [c['uid'] for c in imported_calendars] + + db_only_calendars = session.query(Calendar).filter( + ~Calendar.uid.in_(imported_calendar_uids)).all() + + if len(db_only_calendars): + print 'Local calendars not in remote source:' + for cal in db_only_calendars: + print '%s (%s)' % (cal.name, cal.uid) + print + + for cal in imported_calendars: + update_calendar(cal, session) session.commit() |