# -*- coding: utf-8
from SOAPpy import SOAPProxy
from SOAPpy import Types
from SOAPpy import Config
import re
import sys
import cElementTree
import random
from formatter import fixvCard
#url = "http://opah.htk.tlu.ee:8080/WaramuWeb/waramuService"
url = "http://opah:8080/waramuService/waramu"
url = "http://localhost:8080/waramuService/waramu"
#url = "http://localhost:8080/WaramuWeb/waramuService"
namespace= "http://wssoap.waramu.htk.tlu.ee/"
username='admin'
password='adminadmin'
#username='vahur'
#password='parool'
c = Config
c.debug = 0
s = SOAPProxy(url, config=c)
# ======= Login in
sessIdresponse = s._ns(namespace).newSession(uid=username, pwd=password)
respCode = int(re.search('(.*?)', sessIdresponse).group(1))
if respCode: # not 0
msg = re.search('(.*?)', sessIdresponse).group(1)
print msg
sys.exit();
sessId = re.search('(.*?)', sessIdresponse).group(1)
print "Session id:", sessId
# ======= list types
listTypesresponse = s._ns(namespace).listTypes(sessId = sessId)
xml = cElementTree.fromstring(listTypesresponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "List types failed. Reason:",msg
xtypes = xml.find('types')
wtypes = {}
for t in xtypes:
tid = t.find('id').text
tname = t.find('name').text
wtypes[tid] = {'name': tname, 'fields':{} }
print "Types on server:"
for w in wtypes.keys():
print "\t", w, ":", wtypes.get(w).get('name')
# ======= list type fields
for w in wtypes.keys():
fieldsResponse = s._ns(namespace).describeType(sessId = sessId, typeId = w)
xml = cElementTree.fromstring(fieldsResponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "Getting type description failed. Reason:", msg
continue
print "\nDescription for %s" % wtypes.get(w).get('name')
xfields = xml.find('description').find('fields')
for f in xfields:
print "\t", f.get('name'), f.get('multiLingual'), f.get('type')
wtypes[w]['fields'][f.get('name')] = {'multiLingual': f.get('multiLingual'), 'type': f.get('type')}
# ======== create object
wobjUids = []
f = open('XXMassUpload071126_3010.txt', 'r')
lines = f.readlines()
f.close()
import time
startt = time.time()
for x in range(0, len(lines)):
ll = lines[x]
ll = ll.replace('&', '&')
l = ll.split('\t')
mdLang = l[2]
objLang = l[5]
lotype = l[9]
if '-' not in l[10]:
l[10] = l[10]+"-998"
ageMin, ageMax = l[10].split('-')
ageMin = ageMin.strip()
ageMax = ageMax.strip()
try: int(ageMin)
except ValueError: ageMin = "998"
try: int(ageMax)
except ValueError: ageMax = "-1"
authorr = fixvCard(l[12])
mdmdLang = ""
try: mdmdLang = l[13]
except: pass
trans_title = ""
try: trans_title = l[14]
except: pass
trans_desc = ""
try: trans_desc = l[15]
except: pass
trans_keyw = ""
try: trans_keyw = l[16]
except: pass
objurl = l[8]
woxml = "\n"
woxml += "\n"
woxml += ""+l[1]+"\n"
woxml += ""+objLang+"\n"
woxml += ""+mdLang+"\n"
woxml += ""+mdmdLang+"\n"
woxml += ""+lotype+"\n"
woxml += ""+l[6]+"\n"
woxml += ""+authorr+"\n"
woxml += ""+objurl+"\n"
woxml += ""+l[3]+"\n"
woxml += ""+trans_title+"\n"
woxml += ""+l[4]+"\n"
woxml += ""+trans_desc+"\n"
woxml += ""+l[7]+"\n"
woxml += ""+trans_keyw+"\n"
woxml += ""+l[11]+"\n"
woxml += ""+ageMin+"\n"
woxml += ""+ageMax+"\n"
woxml += ""
try:
woxml = woxml.decode('utf-8')
except UnicodeDecodeError:
woxml = woxml.decode('latin-1')
newResourceResponse = s._ns(namespace).newResource(sessId = sessId, typeId = w, data = woxml)
xml = cElementTree.fromstring(newResourceResponse)
respCode = int(xml.find('status').text)
if respCode: # not 0
msg = xml.find('message').text
print "Object creation failed. Reason:", msg
print "New object with %s uid created" % xml.find('uid').text
wobjUids.append(xml.find('uid').text)
print "Created total of %s object(s)" % len(wobjUids)
# ======= Logging out
print "\nEnd. Bye-bye."
logoutResponse = s._ns(namespace).closeSession(sessId=sessId)
respCode = int(re.search('(.*?)', logoutResponse).group(1))
if respCode:
msg = re.search('(.*?)', logoutResponse).group(1)
print "Logout failed. Reason:", msg
else:
print "Logout successful"
endt = time.time()
print "time:", endt-startt