"""
Goes through an exported_comments XML file from LJ and determines who's been commenting in a journal
Mike Nolan
13 June 2004
Feel free to modify it to your brain's desire
"""
import math, sys
if len(sys.argv) < 2:
print "Usage: " + sys.argv[0] + " xmlfile endpoint"
sys.exit(1)
try:
xmlfilename = sys.argv[1]
fhandle = open(xmlfilename)
lines = fhandle.readlines()
except IOError, (errno, strerror):
print "File error(%s): %s" % (errno, strerror)
sys.exit(1)
def clean_line(l):
for i in range(len(l)):
if l[i] == "<": return l[i:]
return l
# This maps the numeric posterid to a tuple containing the
# LJ username and the number of comments made by that user
#
# ljusers[posterid : string] = (username : string, numberPosts : int)
ljusers = {}
"""
* We are really only dealing with two kinds of tags, so don't bother with fancy XML parsing. The tags are:
*
*
In the first, we are only concerned with posterid. It will be the key in the ljusers key/value pair. ID is unimportant
since it just gives the post number.
In the second, we need both id and user to map id number to username.
"""
for line in lines:
line = clean_line(line)
tokens = line.split(" ")
# Found:
if tokens[0] == "
elif tokens[0] == "Who's been commenting in your journal?
"
print ""
"""Show only first $endpoint comments?"""
if len(sys.argv) == 3:
givenmax = int(sys.argv[2])
if len(sorted) < givenmax:
endpoint = len(sorted)
else:
endpoint = givenmax
else:
endpoint = len(sorted)
for i in range(endpoint):
rank = str(i+1)
uname = sorted[i][0]
num_comments = sorted[i][1]
perc_commentsf = float((sorted[i][1])/totalf * 100.0)
perc_comments = str(perc_commentsf)
"""If anonymous, we don't want to print a link to their journal!"""
if uname == "anonymous":
ljusertag = "Anonymous"
else:
ljusertag = "
+ uname + "
# Text output
# print rank + ".", uname, num_comments, perc_comments + "%"
# HTML output
img_width = str(int(math.log(float(num_comments), logbase) * 350.0))
print ""
print "" + rank + ""
print "" + ljusertag + ""
print "
"
print "" + str(num_comments) + " comments"
print "" + str(round(perc_commentsf,2)) + "% of total"
print ""
print "These statistics were generated using the
LJ Stats Web Interface by
\"mpnolan\". Original idea from
\"scrapdog\"'s LJ Comment Stats Wizard."