def fetch_message(object_id): """Returns a message, from local cache if available, otherwise loads via REST API, you probably should be calling recursive_fetch_message first""" if debug: print "fetch_message(%s) called" % repr(object_id) if debug_tracebacks: traceback.print_stack() object_id = str(object_id) # cast to normal str if storage.in_cache_byid(object_id): obj = storage.get_byid(object_id) if ((obj.has_key('truncated') and obj['truncated']) or (obj.has_key('QaikuBackup_stale') and obj['QaikuBackup_stale'])): # Object is stale, do not return from cache if debug: print "storage.objectcache[%s] is stale" % repr(object_id) if debug_tracebacks: print json.dumps(storage.get_byid(object_id), sort_keys=True, indent=4) pass else: if debug: print "message %s returned from cache" % object_id return storage.get_byid(object_id) else: #print "objectcache has no key %s" % repr(object_id) #print json.dumps(storage.objectcache, sort_keys=True, indent=4) pass url = "http://www.qaiku.com/api/statuses/show/%s.json?apikey=%s" % ( object_id, apikey) parsed = json_parse_url(url) if not parsed: # parse failed, return stale object if we have one if storage.in_cache_byid(object_id): return storage.get_byid(object_id) else: return None storage.update(parsed) return storage.get_byid(object_id)
def fetch_message(object_id): """Returns a message, from local cache if available, otherwise loads via REST API, you probably should be calling recursive_fetch_message first""" if debug: print "fetch_message(%s) called" % repr(object_id) if debug_tracebacks: traceback.print_stack() object_id = str(object_id) # cast to normal str if storage.in_cache_byid(object_id): obj = storage.get_byid(object_id) if ( ( obj.has_key('truncated') and obj['truncated']) or ( obj.has_key('QaikuBackup_stale') and obj['QaikuBackup_stale']) ): # Object is stale, do not return from cache if debug: print "storage.objectcache[%s] is stale" % repr(object_id) if debug_tracebacks: print json.dumps(storage.get_byid(object_id), sort_keys=True, indent=4) pass else: if debug: print "message %s returned from cache" % object_id return storage.get_byid(object_id) else: #print "objectcache has no key %s" % repr(object_id) #print json.dumps(storage.objectcache, sort_keys=True, indent=4) pass url = "http://www.qaiku.com/api/statuses/show/%s.json?apikey=%s" % (object_id, apikey) parsed = json_parse_url(url) if not parsed: # parse failed, return stale object if we have one if storage.in_cache_byid(object_id): return storage.get_byid(object_id) else: return None storage.update(parsed) return storage.get_byid(object_id)
if not fill_image_urls(message_id): return False obj = fetcherparser.fetch_message(message_id) for prop in ['QaikuBackup_image_url_view', 'QaikuBackup_image_url_orig']: if not obj.has_key(prop): continue res = fetcherparser.fetch_resource(obj[prop]) if res: obj[prop] = res # Force objectcache update to make sure we don't have funky COW issues storage.update(obj) return True if __name__ == '__main__': import sys, os, json msgid = sys.argv[1] print "*** STARTING ***" if fill_image_urls(msgid): urllib_cached.clean() print "*** DONE ***" print "message %s contents:" % msgid print json.dumps(storage.get_byid(msgid), sort_keys=True, indent=4) else: urllib_cached.clean() print "*** FAILED ***" sys.exit(1)
"""Loads the given object, hits the screenscraper to fill the extra image properties and then fetches those images""" if not fill_image_urls(message_id): return False obj = fetcherparser.fetch_message(message_id) for prop in ['QaikuBackup_image_url_view', 'QaikuBackup_image_url_orig']: if not obj.has_key(prop): continue res = fetcherparser.fetch_resource(obj[prop]) if res: obj[prop] = res # Force objectcache update to make sure we don't have funky COW issues storage.update(obj) return True if __name__ == '__main__': import sys,os,json msgid = sys.argv[1] print "*** STARTING ***" if fill_image_urls(msgid): urllib_cached.clean() print "*** DONE ***" print "message %s contents:" % msgid print json.dumps(storage.get_byid(msgid), sort_keys=True, indent=4) else: urllib_cached.clean() print "*** FAILED ***" sys.exit(1)