added content submission to a drupal server
This commit is contained in:
parent
d230706d90
commit
66fab78c30
2 changed files with 104 additions and 5 deletions
93
ical_aggregator/drupal_submit.py
Executable file
93
ical_aggregator/drupal_submit.py
Executable file
|
@ -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())
|
||||||
|
|
|
@ -6,8 +6,9 @@ import urllib
|
||||||
import vobject
|
import vobject
|
||||||
import sys
|
import sys
|
||||||
import os
|
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"]
|
UNIQUE_KEY = ["dtstart", "summary"]
|
||||||
|
|
||||||
|
|
||||||
|
@ -57,9 +58,14 @@ def add_to_processed_list(fileout, event):
|
||||||
fileout.write(get_unique_id(event) + "\n")
|
fileout.write(get_unique_id(event) + "\n")
|
||||||
|
|
||||||
|
|
||||||
def submit_event_to_drupal(event):
|
def submit_event_to_drupal(auth_file, event):
|
||||||
print "Added event: %s" % encode_safely(event["summary"])
|
body = "%(description)s\n\nOrt: %(location)s\n\nKategorie: %(categories)s" % event
|
||||||
return True
|
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__":
|
if __name__ == "__main__":
|
||||||
|
@ -104,7 +110,7 @@ if __name__ == "__main__":
|
||||||
for event in icals:
|
for event in icals:
|
||||||
if not get_unique_id(event) in finished_items:
|
if not get_unique_id(event) in finished_items:
|
||||||
# process new events
|
# process new events
|
||||||
if submit_event_to_drupal(event):
|
if submit_event_to_drupal(AUTH_FILE, event):
|
||||||
add_to_processed_list(finished_file, event)
|
add_to_processed_list(finished_file, event)
|
||||||
finished_items.append(get_unique_id(event))
|
finished_items.append(get_unique_id(event))
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue