diff options
author | emkael <emkael@tlen.pl> | 2016-05-03 21:35:29 +0200 |
---|---|---|
committer | emkael <emkael@tlen.pl> | 2016-05-04 10:05:31 +0200 |
commit | 0c6e3ee216b30acfca4e3b0fab0b085b8e34960b (patch) | |
tree | 2c7dd43bfa2881fafe50ab13d7463989586d64b4 /app | |
parent | 67580665e4379c1fa9d748b65823b9163c16bd0c (diff) |
* Python scripts moved
Diffstat (limited to 'app')
-rw-r--r-- | app/python/fetch_cals.py | 93 | ||||
-rw-r--r-- | app/python/import_cals.py | 128 | ||||
-rw-r--r-- | app/python/init_db.py | 16 |
3 files changed, 237 insertions, 0 deletions
diff --git a/app/python/fetch_cals.py b/app/python/fetch_cals.py new file mode 100644 index 0000000..30e7abc --- /dev/null +++ b/app/python/fetch_cals.py @@ -0,0 +1,93 @@ +import datetime + +import dateutil.parser as dateparser +import ics +import pytz +import requests +from rcal.db import Session +from rcal.model import Calendar, Entry + + +def update_event_data(db_event, ical_event): + db_event.name = ical_event.name + db_event.location = ical_event.location + db_event.begin_date = ical_event.begin.datetime + db_event.end_date = ical_event.end.datetime + db_event.all_day = ( + (db_event.end_date - db_event.begin_date).seconds % 86400 == 0 + ) and ( + db_event.begin_date.time() == datetime.time.min) + if db_event.all_day: + db_event.end_date = db_event.end_date - datetime.timedelta(days=1) + db_event.last_modified = get_last_modification_time(ical_event) + return db_event + + +def update_event(db_event, ical_event): + update_event_data(db_event, ical_event) + + +def add_event(event, calendar, session): + entry = Entry() + entry.uid = event.uid + entry.calendar = calendar + entry = update_event_data(entry, event) + session.add(entry) + + +def remove_event(event, session): + session.delete(event) + + +def get_last_modification_time(event): + for unused in event.__dict__['_unused']: + if unused.name == 'LAST-MODIFIED': + return dateparser.parse(unused.value) + return None + + +def fetch_calendar(calendar, session): + cal_data = requests.get(calendar.url) + cal_object = ics.Calendar(cal_data.content.decode(cal_data.encoding)) + cal_events = {e.uid: e for e in cal_object.events} + db_events = {e.uid: e for e in calendar.entries} + new_events = [e for u, e in cal_events.iteritems() + if u not in db_events.keys()] + old_events = [e for u, e in db_events.iteritems() + if u not in cal_events.keys()] + mod_events = [{'ics': cal_events[u], 'db': e} + for u, e in db_events.iteritems() if u in cal_events.keys()] + changes_present = False + for event in mod_events: + modified_date = get_last_modification_time(event['ics']) + if not modified_date or \ + not event['db'].last_modified or \ + modified_date > event['db'].last_modified.replace(tzinfo=pytz.UTC): + print 'Updating event %s' % event['db'].uid + update_event(event['db'], event['ics']) + changes_present = True + for event in new_events: + print 'Adding event %s' % event.uid + add_event(event, calendar, session) + changes_present = True + for event in old_events: + print 'Removing event %s' % event.uid + remove_event(event, session) + changes_present = True + if changes_present: + calendar.last_updated = datetime.datetime.now() + + +def main(): + session = Session.create() + + calendars = session.query(Calendar).all() + for calendar in calendars: + # print 'Fetching %s' % calendar.url + fetch_calendar(calendar, session) + + session.commit() + + +if __name__ == '__main__': + main() diff --git a/app/python/import_cals.py b/app/python/import_cals.py new file mode 100644 index 0000000..a08bfb2 --- /dev/null +++ b/app/python/import_cals.py @@ -0,0 +1,128 @@ +import json +import os +import re +import time +import urllib2 +import urlparse + +from sqlalchemy import inspect + +from rcal.db import Session +from rcal.model import Calendar, Category + +BASEPATH = os.path.join( + os.environ['PYTHONPATH'], + '..', + '..') + +CONFIG = json.load(open( + os.path.join( + BASEPATH, + 'config', + 'reddit-import.json'), + 'r')) + + +def get_cal_list(): + cache_path = os.path.join( + BASEPATH, + CONFIG['cache']) + if not os.path.exists(cache_path) or \ + int(time.time()) - int(os.path.getmtime(cache_path)) > \ + CONFIG['cache_time']: + opener = urllib2.build_opener() + opener.addheaders = [('User-Agent', CONFIG['user_agent'])] + cal_list = json.loads(opener.open(CONFIG['reddit_url']).read()) + cal_list = cal_list['data']['content_md'] + with open(cache_path, 'w') as cache_file: + cache_file.write(cal_list) + cache_file.close() + else: + cal_list = open(cache_path, 'r').read() + return cal_list + + +def update_calendar(cal, session): + db_cal = Calendar.fetch(cal['uid'], session) + + 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'] + + # informational only + if db_cal.category.name != cal['category']: + print 'Calendar category changed: %s -> %s (%s)' % ( + db_cal.category.name, cal['category'], db_cal.uid) + + +def get_imported_calendars(cells, ical_markdown): + imported_calendars = [] + for row in cells: + row = [r for r in row if r.strip()] + 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].split()[0], + 'category': row[1] + }) + else: + 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() + +if __name__ == '__main__': + main() diff --git a/app/python/init_db.py b/app/python/init_db.py new file mode 100644 index 0000000..7d48e80 --- /dev/null +++ b/app/python/init_db.py @@ -0,0 +1,16 @@ +import sys + +from rcal.db import Session +from rcal.model import BASE + + +def main(): + session = Session.create() + + if len(sys.argv) > 1 and sys.argv[1] == 'force': + BASE.metadata.drop_all(session.get_bind()) + + BASE.metadata.create_all(session.get_bind()) + +if __name__ == '__main__': + main() |