From 66fab78c302445f49ee5c46d1104eac2c4ce5658 Mon Sep 17 00:00:00 2001 From: lars Date: Sun, 4 Oct 2009 21:44:10 +0000 Subject: [PATCH] added content submission to a drupal server --- ical_aggregator/drupal_submit.py | 93 ++++++++++++++++++++++++++++++ ical_aggregator/ical_aggregator.py | 16 +++-- 2 files changed, 104 insertions(+), 5 deletions(-) create mode 100755 ical_aggregator/drupal_submit.py diff --git a/ical_aggregator/drupal_submit.py b/ical_aggregator/drupal_submit.py new file mode 100755 index 0000000..6d26839 --- /dev/null +++ b/ical_aggregator/drupal_submit.py @@ -0,0 +1,93 @@ +#!/usr/bin/env python +# +# a python module for submitting content to a drupal server +# + +import xmlrpclib +import time +import random +import hmac +import hashlib +import datetime + +AUTH_FILE = "stadtgestalten_auth.conf" + + +def send_xmlrpc_request(server, session, api_key, api_domain, method, args=[]): + timestamp = str(int(time.time())) + nonce = str(random.Random().randint(0, 1000000000)) + hash_base_string = ";".join([timestamp, api_domain, nonce, method]) + hash = hmac.HMAC(api_key, hash_base_string, hashlib.sha256).hexdigest() + + api_uri = server + for item in method.split("."): + api_uri = getattr(api_uri, item) + if isinstance(args, dict): + arr = [] + for key, value in args.items(): + arr.append(key) + arr.append(value) + return api_uri(hash, api_domain, timestamp, nonce, session['sessid'], args) + elif not isinstance(args, list): + args = [args] + if len(args) == 0: + return api_uri(hash, api_domain, timestamp, nonce, session['sessid']) + elif len(args) == 1: + return api_uri(hash, api_domain, timestamp, nonce, session['sessid'], args[0]) + elif len(args) == 2: + return api_uri(hash, api_domain, timestamp, nonce, session['sessid'], args[0], args[1]) + elif len(args) == 3: + return api_uri(hash, api_domain, timestamp, nonce, session['sessid'], args[0], args[1], args[2]) + else: + return False + + +def generate_node(title, body, username, time_start, time_finish=None, time_duration_minutes=None): + # calculate "time_finish" if it is not defined + if time_finish is None: + if time_duration_minutes is None: + time_finish = time_start + datetime.timedelta(hours=2) + else: + time_finish = time_start + time_duration_minutes + return {'type': 'termin', + 'title': title, + 'body': body, + 'name': username, + # new nodes will not be published immediately (status=0) + 'status': 0, + 'field_kategorie': { 'value': [] }, + 'field_termin': { + 'value': { + 'date': time_start.strftime("%d.%m.%Y"), + 'time': time_start.strftime("%H:%M"), + }, + 'value2': { + 'date': time_finish.strftime("%d.%m.%Y"), + 'time': time_finish.strftime("%H:%M"), + }, + 'rrule': { + 'FREQ': 'NONE', + 'INTERVAL': 0, + 'advanced': { 'BYDAY': [''], 'BYMONTH': [''], 'BYMONTHDAY': [''], }, + }, + } + } + + +def submit_drupal_node(auth_file, title, body, time_start, time_finish=None, time_duration_minutes=None): + url, api_key, api_domain, username, password = file(auth_file).read().splitlines() + + server = xmlrpclib.ServerProxy(url) + # create a session + session = server.system.connect() + # login + session = send_xmlrpc_request(server, session, api_key, api_domain, "user.login", [username, password]) + node = generate_node(title, body, username, time_start, time_finish, time_duration_minutes) + result = send_xmlrpc_request(server, session, api_key, api_domain, "node.save", node) + send_xmlrpc_request(server, session, api_key, api_domain, "user.logout") + return result > 0 + + +if __name__ == "__main__": + print submit_drupal_node(AUTH_FILE, "Der Titel", "der Inhalt", datetime.datetime.now()) + diff --git a/ical_aggregator/ical_aggregator.py b/ical_aggregator/ical_aggregator.py index 42e910b..0f94eae 100755 --- a/ical_aggregator/ical_aggregator.py +++ b/ical_aggregator/ical_aggregator.py @@ -6,8 +6,9 @@ import urllib import vobject import sys import os +import drupal_submit -EVENT_KEYS = ["uid", "url", "location", "categories", "summary", "description", "dtstart" , "dtstamp"] +EVENT_KEYS = ["uid", "url", "location", "categories", "summary", "description", "dtstart" , "dtend", "dtstamp"] UNIQUE_KEY = ["dtstart", "summary"] @@ -57,9 +58,14 @@ def add_to_processed_list(fileout, event): fileout.write(get_unique_id(event) + "\n") -def submit_event_to_drupal(event): - print "Added event: %s" % encode_safely(event["summary"]) - return True +def submit_event_to_drupal(auth_file, event): + body = "%(description)s\n\nOrt: %(location)s\n\nKategorie: %(categories)s" % event + result = drupal_submit.submit_drupal_node(auth_file, event["summary"], body, event["dtstart"], event["dtend"]) + if result: + print "Added event: %s" % encode_safely(event["summary"]) + else: + print "Failed to add event: %s" % encode_safely(event["summary"]) + return result if __name__ == "__main__": @@ -104,7 +110,7 @@ if __name__ == "__main__": for event in icals: if not get_unique_id(event) in finished_items: # process new events - if submit_event_to_drupal(event): + if submit_event_to_drupal(AUTH_FILE, event): add_to_processed_list(finished_file, event) finished_items.append(get_unique_id(event))