def timeghost_json(): """JSON page: generate a random Timeghost and return it as a JSON object""" now = Event.now() middle = Event.get_random(before=now) timeghost = TimeGhostFactory.build(now=now, middle=middle) tg_dict = { 'factoid': timeghost.factoid, 'permalink': timeghost.permalink } return jsonify(tg_dict)
def timeghost_json(): """JSON page: generate a random Timeghost and return it as a JSON object""" middle = Event.get_random(before=Event.now()) tg = TimeGhostFactory.build(middle=middle) tg_dict = dict( factoid=tg.factoid, permalink=tg.permalink_fully_qualified, tweet="{} #timeghost {}".format( tg.factoid[:110], tg.permalink_fully_qualified), ) return jsonify(tg_dict)
def fast_timeghost_server(middle_date_str=None, now_date_str=None): """ Generates a random timeghost, a timeghost between now and a particular time, or a timeghost between two specified times, depending on the number of arguments given. """ try: if now_date_str is None: now = Event.now() else: now = Event.build(date_str=now_date_str) if middle_date_str is None: middle = Event.get_random(before=now) else: middle = Event.build(date_str=middle_date_str) timeghost = TimeGhostFactory.build(middle=middle) return render_template('timeghost.html', timeghost=timeghost) except TimeGhostError as err: return render_template('error.html', err=err), 404
def timeghost_server(middle_date_str=None, now_date_str=None): """ Generates a random timeghost, a timeghost between now and a particular time, or a timeghost between two specified times, depending on the number of arguments given. """ try: if now_date_str is None: now = Event.now() else: now = Event.build(date_str=now_date_str) if middle_date_str is None: middle = Event.get_random(before=now) else: middle = Event.build(date_str=middle_date_str) timeghost = TimeGhostFactory.build(now=now, middle=middle) logging.debug("output timeghost: %s", timeghost) return render_template('timeghost.html', timeghost=timeghost) except TimeGhostError as err: return render_template('error.html', err=err), 404
def build(cls, now=None, middle=None, long_ago=None, get_earliest=False): """ Create TimeGhost objects from triplets of events. """ timeghost = TimeGhost(now=now, middle=middle, long_ago=long_ago) # Set the .now Event to "now" if not specified: if timeghost.now is None: timeghost.set(Event.now(), "now") # Generate a .middle Event if not specified: if timeghost.middle is None: timeghost.set(Event.get_random(), "middle") # Generate a .long_ago Event if not specified: if timeghost.long_ago is None: try: long_ago = timeghost.find_best_long_ago(get_earliest) except TimeGhostError: long_ago = Event.get_earliest() timeghost.set(long_ago, "long_ago") # Return a new instance to insure the init validation is run properlhy: return TimeGhost(timeghost.now, timeghost.middle, timeghost.long_ago)
def build_from_timeghost(cls, timeghost): """Return a completed TimeGhost based on the partial TimeGhost request.""" logging.debug("TimeGhostFactory.build: %s", timeghost) # Timeghost between now and a random event if timeghost.middle is None: timeghost.middle = Event.get_random() # Timeghost for now and given middle, no long_ago elif timeghost.now is not None and \ timeghost.middle is not None and \ timeghost.long_ago is None: pass # Fully specified timeghost. Just return it. elif timeghost.now is not None and \ timeghost.middle is not None and \ timeghost.long_ago is not None: return timeghost # Otherwise error else: raise TimeGhostError("bad case in TimeGhostFactory.build for %s", timeghost) logging.debug("TimeGhostFactory.build: %s", timeghost) try: best_long_ago = timeghost.find_best_long_ago() except TimeGhostError: best_long_ago = Event.get_earliest() logging.debug("TimeGhostFactory.build: %s", best_long_ago) output_timeghost = TimeGhost(now=timeghost.now, middle=timeghost.middle, long_ago=best_long_ago) logging.debug("TimeGhostFactory.build: %s", output_timeghost) return output_timeghost