?

Log in

No account? Create an account
Purged! - Spleenier Than Thou [entries|archive|friends|userinfo]
schlake

[ website | http://infohost.nmt.edu/~schlake/ ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Purged! [Jan. 6th, 2013|02:03 am]
schlake
I purged for a second time.

#!/usr/bin/env python

from getpass import getpass
import urllib2
from hashlib import md5
import xmlrpclib
import pprint

def calcchallenge(challenge, password):
  return md5(challenge+md5(password).hexdigest()).hexdigest()

def flatresponse(response):
  r = {}
  while True:
    name = response.readline()
    if len(name) == 0:
      break
    if name[-1] == '\n':
      name = name[:len(name)-1]
    value = response.readline()
    if value[-1] == '\n':
      value = value[:len(value)-1]
    r[name] = value
  return r

def dochallenge(server, params, password):
  challenge = server.LJ.XMLRPC.getchallenge()
  data = {
    'auth_method': "challenge",
    'auth_challenge': challenge['challenge'],
    'auth_response': calcchallenge(challenge['challenge'], password)
  }
  params.update( data )
  return params

def getljsession( server, username, password ):
  r = urllib2.urlopen(server+"/interface/flat", "mode=getchallenge")
  response = flatresponse(r)
  r.close()
  r = urllib2.urlopen(server+"/interface/flat", "mode=sessiongenerate&user=%s&auth_method=challenge&auth_challenge=%s&auth_response=%s" % (username, response['challenge'], calcchallenge(response['challenge'], password)))
  response = flatresponse(r)
  r.close()
  return response['ljsession']
  
site = 'http://livejournal.com'
username = raw_input( "Username: " )
password = getpass( "Password: " )
journal = username

ljsession = getljsession( site, username, password )
server = xmlrpclib.ServerProxy(site+"/interface/xmlrpc")


lastsync = ""

while True:
  r = server.LJ.XMLRPC.syncitems(dochallenge(server, {
            'username': username,
            'ver': 1,
            'lastsync': lastsync,
            'usejournal': username,
        }, password))
  if len( r['syncitems'] ) == 0: break
  for item in r['syncitems']:
    print item
    if item['item'][0] == 'C':
      print 'Ignoring comment'
      
    if item['item'][0] == 'L':
      print 'Purging journal entry'
      try:
        e = server.LJ.XMLRPC.editevent( dochallenge(server, {
            'username': username,
            'ver': 1,
            'itemid': item['item'][2:],
            'usejournal': username,
	    'event': '',
	    }, password ) )	  
      except xmlrpclib.Fault, x:
        print 'Error'
	pprint.pprint( x )
    lastsync = item['time']
LinkReply