[Tutor] Python & XML
justinstraube at charter.net
justinstraube at charter.net
Tue May 11 07:31:04 EDT 2004
Sorry, for the repost but I accidentally sent before I could ask for a
small bit of help. Can anyone spare a minute to look over this and give
any points as to what I may be doing wrong or what could be done better?
Ive managed a way to extract the wanted data from the xml string,
though Im not sure if Im going about this in the best way. I think this
should work for any SETI user.
Thanks for your time,
justin
#################################
##### PSETI #################
#################################
import httplib
from xml.dom import minidom
_name_ = 'PSETI'
_auth_ = 'justinstraube at charter.net'
_date_ = 'May 11 2004'
_ver_ = '0.0004'
_inf_ = 'Python 2.3 www.Python.org'
SETI = "setiathome2.ssl.berkeley.edu"
while 1:
print '\t', _name_, _ver_, '\t', _date_
print '\n\tCreated by:', _auth_
print '\tWritten in:', _inf_
usrmail = raw_input('\nYour Email Address: ')
if usrmail == '':
usrmail = _auth_
add = '/fcgi-bin/fcgi?cmd=user_xml&email=' + usrmail
conx = httplib.HTTPConnection(SETI)
conx.request("GET", add)
response = conx.getresponse()
if response.status == 500:
print response.status, response.reason
print """\n\t\tServer Error\n
The page or site is temporarily down or disabled.
Sorry for the inconvienience.
Please try again later.\n"""
break
data = response.read()
if data[-33:] == 'No user with that name was found.':
print """\n\t\tNo user with that name was found.
Please check the email address and try again.\n"""
break
conx.close()
xdata = minidom.parseString(data)
ud = {} #users dictionary
usr = xdata.childNodes[1].childNodes
for item in usr:
if item.nodeType == item.ELEMENT_NODE:
if item.tagName == 'userinfo':
for node in item.childNodes:
if node.nodeType == node.ELEMENT_NODE:
if node.tagName == 'name':
uname = node.toxml()
ud[1] = uname[6:-7] + ' <' + usrmail + '>'
node.unlink
elif node.tagName == 'numresults':
num = node.toxml()
ud[2] = num[12:-13]
node.unlink
elif node.tagName == 'cputime':
cput = node.toxml()
ud[3] = cput[9:-10]
node.unlink
elif node.tagName == 'avecpu':
avg = node.toxml()
ud[4] = avg[8:-9]
node.unlink
elif node.tagName == 'resultsperday':
rpd = node.toxml()
ud[5] = rpd[15:-16]
node.unlink
elif node.tagName == 'lastresulttime':
lrr = node.toxml()
ud[6] = lrr[16:-17]
node.unlink
elif node.tagName == 'regdate':
regd = node.toxml()
ud[7] = regd[9:-10]
node.unlink
elif node.tagName == 'usertime':
usrt = node.toxml()
ud[8] = usrt[10:-11]
node.unlink
else:
node.unlink
else:
node.unlink
elif item.tagName == 'groupinfo':
flist = '' # if user has started multiple groups.
for node in item.childNodes:
if node.nodeType == node.ELEMENT_NODE:
if node.tagName == 'founder':
found = node.toxml()
found = found[82:-14]
flist = flist + ', ' + found
node.unlink
elif node.tagName == 'group':
group = node.toxml()
ud[10] = group[80:-12]
node.unlink
else:
node.unlink
else:
node.unlink
# founder list is added after all tags have been
# sliced,if user has started multiple groups
ud[9] = flist
elif item.tagName == 'rankinfo':
for node in item.childNodes:
if node.nodeType == node.ELEMENT_NODE:
if node.tagName == 'rank':
rank = node.toxml()
ud[11] = rank[6:-7]
node.unlink
elif node.tagName == 'ranktotalusers':
total = node.toxml()
ud[12] = total[16:-17]
node.unlink
elif node.tagName == 'num_samerank':
same = node.toxml()
ud[13] = same[14:-15]
node.unlink
elif node.tagName == 'top_rankpct':
pct = node.toxml()
ud[14] = pct[13:-14]
node.unlink
else:
node.unlink
else:
node.unlink
elif item.tagName == 'certinfo':
for node in item.childNodes:
if node.nodeType == node.ELEMENT_NODE:
if node.tagName == 'cert':
# Should be the highest certificate.
cert = node.toxml()
cert = cert[69:]
cert = cert[len(usrmail):]
ud[15] = cert[45:-11]
node.unlink
break
else:
node.unlink
else:
node.unlink
# if user has not founder of a group
if ud[9] == '':
ud[9] = '<none>'
#if user has no certificates yet
if ud.has_key(15) == False:
ud[15] = '<none>'
print '\nUser:\t\t', ud[1]
print 'Finished Units:', ud[2], 'over', ud[3]
print 'Results/Day:\t', ud[4], ' Avg:', ud[5]
print 'Last Result:\t', ud[6]
print 'Registered on:\t', ud[7]
print 'User for\t', ud[8]
print 'Founder of: \t', ud[9][2:]
print 'Member of: \t', ud[10]
print 'Rank:\t\t', ud[11], 'of', ud[12], 'users total.'
print 'Your Rank:\t', ud[13], ',', ud[14], 'users share the same
rank.'
print 'Highest Cert: \t', ud[15]
break
####
regards,
Justin
More information about the Tutor
mailing list