Created
August 6, 2012 23:02
-
-
Save enkore/3279298 to your computer and use it in GitHub Desktop.
Uploading files and creating nodes to a Drupal site using Services, Python and XMLRPC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# coding=utf-8 | |
# Copyright (C) 2012 Marian Beermann | |
# | |
# This program is free software: you can rediunicodeibute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is diunicodeibuted in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# Text messages are in german... but it should be no problem at all to understand the program anyway ;-) | |
import xmlrpclib | |
from getpass import getpass | |
import sys | |
from datetime import datetime | |
from glob import glob | |
import base64 | |
import os.path | |
class CookieTransport(xmlrpclib.Transport): | |
def __init__(self, SESSION_ID_STRING='PHPSESSID'): | |
xmlrpclib.Transport.__init__(self) | |
self.mycookies=None | |
self.mysessid=None | |
self.SESSION_ID_STRING = SESSION_ID_STRING | |
def parseCookies(self,s): | |
if s is None: return {self.SESSION_ID_STRING:None} | |
ret = {} | |
tmp = s.split(';') | |
for t in tmp: | |
coppia = t.split('=') | |
if len(coppia) == 2: | |
k = coppia[0].strip() | |
v = coppia[1].strip() | |
ret[k]=v | |
return ret | |
def request(self, host, handler, request_body, verbose=0): | |
# issue XML-RPC request | |
h = self.make_connection(host) | |
if verbose: | |
h.set_debuglevel(1) | |
self.send_request(h, handler, request_body) | |
self.send_host(h, host) | |
if not self.mysessid is None: | |
h.putheader("Cookie", "%s=%s" % (self.SESSION_ID_STRING,self.mysessid) ) | |
self.send_user_agent(h) | |
self.send_content(h, request_body) | |
headers = response = h.getresponse() | |
errcode = response.status | |
errmsg = response.reason | |
if self.mysessid is None: | |
self.mycookies = self.parseCookies( headers.getheader('set-cookie') ) | |
if self.mycookies.has_key(self.SESSION_ID_STRING): | |
self.mysessid = self.mycookies[self.SESSION_ID_STRING] | |
if errcode != 200: | |
raise xmlrpclib.ProtocolError( | |
host + handler, | |
errcode, errmsg, | |
headers | |
) | |
self.verbose = verbose | |
try: | |
sock = h._conn.sock | |
except AttributeError: | |
sock = None | |
return self.parse_response(response) | |
config = { | |
"url": "<URI>", | |
"username": raw_input("Benutzername: "), | |
"password": getpass("Passwort: "), | |
"indir": raw_input("Eingabeordner: "), | |
} | |
transport = CookieTransport() | |
server = xmlrpclib.Server(config["url"], transport=transport) | |
print "Authentifiziere..." | |
try: | |
user = server.user.login(config["username"], config["password"]) | |
except xmlrpclib.ProtocolError: | |
print("Fehler.") | |
sys.exit(1) | |
transport.SESSION_ID_STRING = user['session_name'] | |
transport.mysessid = user['sessid'] | |
print "Sitzungsbegin: %s" % datetime.fromtimestamp(int(user["user"]["login"])).isoformat(" ") | |
files = glob("%s/*.jpeg" % config["indir"]) | |
files.extend(glob("%s/*.jpg" % config["indir"])) | |
num_files = len(files) | |
print "%s Dateien gefunden (nur jpg/jpeg)" % num_files | |
for i, filename in enumerate(files): | |
print "(%s %%) %s" % ((float(i)/num_files)*100, os.path.basename(filename)) | |
upload = { | |
"filesize": os.path.getsize(filename), | |
"filename": os.path.basename(filename), | |
"filemime": "image/jpeg", | |
"uid": user["user"]["uid"], | |
"status": 1, | |
} | |
with open(filename, "rb") as f: | |
upload["file"] = base64.b64encode(f.read()) | |
node = { | |
"uid": user["user"]["uid"], | |
"title": os.path.splitext(os.path.basename(filename))[0], | |
"type": "zeitungsschnipsel", | |
"field_schnipsel": { | |
"und": { | |
"0": { | |
"fid": server.file.create(upload)["fid"], | |
"list": 1, | |
"status": 1, | |
"data": 0, | |
} | |
} | |
}, | |
} | |
server.node.create(node) | |
server.user.logout() | |
print "Sitzungsende: %s" % datetime.now().isoformat(" ") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment