#!/usr/bin/python
import sys
import os

slog = open("safari_log", "r")

pages = {}
times = {}

for line in slog:
    if line.startswith("*"):
        # nuke everything
        oldpages = pages
        oldtimes = times
        pages = {}
        times = {}
    else:
        try:
            tag, time, url = line.split(" ",2)
        except:
            print "line:",line
            raise
        if tag == "+":
            pages[url] = 1
            times[url] = time
        elif tag == "-":
            if url in pages:
                del pages[url]
                del times[url]

def emit_pages(pages,times):
    pagelist = []
    for p in pages:
        pagelist.append((times[p], p))
    pagelist.sort()
    for t,p in pagelist:
        u = p.strip()
        print u

# or just "if not pages"? or maybe if len(pages)<3?
if len(sys.argv) == 2 and sys.argv[1] == "old":
    emit_pages(oldpages, oldtimes)
else:
    if len(pages)<3:
        print >> sys.stderr, "Only got", pages, "leftover, assume you want old ones"
        emit_pages(oldpages, oldtimes)
    else:
        emit_pages(pages, times)