def readSummary(self, localdir = '', filename = 'archive.json'): """Read archive data from summary file """ # localdir and filename args enable users to specify # alternate JSON summary files if not localdir: localdir = self.localdir # Load JSON data into a dictionary with open(os.path.join(localdir, filename), 'r') as j: try: # Load JSON data from summary file into dict jsond = json.load(j) except: print "Could not load JSON data from %s" % (os.path.join(localdir, filename)) return None self.lastupdate = jsond["lastupdate"] print "This archive summary was last updated: %s" % jsond["lastupdate"] # Loop over forum dict, creating Forum objects self.forum = {} for forumkey, forumdict in jsond["forum"].iteritems(): # Create Forum object for each archived forum # Can't use unicode strings as keywords in dict :( kw = vbutils.convertKeysToStr(forumdict) self.forum[forumkey] = vbforum.Forum(**kw)
def importJSON(self, jsondata): """Populate object from a string of JSON data""" # ''.join is used to accomodate list input clean = vbutils.cleanEncoding(''.join(jsondata), isHTML = False) # Try to load JSON data from the input str try: j = json.loads(clean) except: print "Error: Could not find JSON data." return None # Loop over posts creating Post objs self.post = {} print "Found %s posts." % len(j["post"]) for id, p in j["post"].iteritems(): print "Importing Post #%s ..." % str(id) if type(p) is dict: # Keyword args must be str # but our JSON dict has unicode keys # so we must convert before passing # into the Post.__init__() method kw = vbutils.convertKeysToStr(p) self.post[id] = vbpost.Post(**kw) elif (type(p) in [str, unicode]): self.post[id] = vbpost.Post(jsonstr = p) self.lastupdate = j["lastupdate"] self.forum = j["forum"] self.id = j["id"] self.numpages = j["numpages"] self.title = j["title"] self.url = j["url"]
def __init__(self, localdir = '.', summary = 'archive.json', lastupdate = '', platform = 'unix', forum = {} ): if not localdir: localdir = '.' if not summary: summary = 'archive.json' if platform in ['unix', 'windows']: self.platform = platform else: self.platform = 'unix' # Check if localdir exists if not os.access(localdir, os.F_OK): # Create a new local archive dir os.mkdir(localdir) self.localdir = localdir print "Archive located at %s" % self.localdir self.lastupdate = lastupdate self.forum = forum # Did we receive forum data as an **arg? if forum: print "Forum data received as dict obj." # If so, create Forum objs from dict for id, f in forum.iteritems(): kw = vbutils.convertKeysToStr(f) self.forum[id] = vbforum.Forum(**kw) # If not, does a summary file exist in localdir? elif os.access(os.path.join(localdir, summary), os.F_OK): # If so, load forum data from the summary file print "Reading forum data from JSON summary." self.readSummary() else: # TODO try to discover thread data and create summary # # Otherwise, create empty JSON summary file print "No JSON summary found in archive dir." print "Update this object and it will try to read your archive." self.writeSummary()
def __init__(self, url = '', id = '', lastupdate = '', title = '', forum = '', numpages = 1, post = {}, jsonstr = '', rawhtml = '' ): if jsonstr: self.importJSON(jsonstr) elif rawhtml: self.importHTML(rawhtml) # When creating a thread object with # raw HTML, one should also pass along # the lastupdate string if lastupdate: self.lastupdate = lastupdate else: # Else set lastupdate to now self.lastupdate = vbutils.getDateTime() else: self.lastupdate = lastupdate self.forum = forum self.id = id self.numpages = numpages self.title = title self.url = url if post: self.post = {} for id, p in post.iteritems(): kw = vbutils.convertKeysToStr(p) self.post[id] = vbpost.Post(**kw) else: self.post = post if url: self.update(url)
def __init__(self, url = '', name = '', lastupdate = '', version = '', thread = {}, user = {}, jsonstr = '', rawhtml = '' ): # TODO implement importJSON method a la Thread, Post # TODO should probably be subclassed in a future rev self.url = url self.name = name self.lastupdate = lastupdate self.version = version self.thread = {} for id, t in thread.iteritems(): kw = vbutils.convertKeysToStr(t) self.thread[id] = vbthread.Thread(**kw) self.user = {} """TODO User class not yet implemented