def find_photo(ts, photos):
    """Find photo nearest by time, return index for last previous photo
    """
    def binsearch(lo, hi):
        if (hi - lo) <= 0:
            return hi
        if ts <= photos[lo]['ts']:
            return lo
        if ts >= photos[hi]['ts']:
            return hi
        mid = int((hi - lo) / 2)
        if ts <= photos[lo+mid]['ts']:
            return binsearch(lo, lo+mid)
        return binsearch(lo+mid+1, hi)

    idx = binsearch(0, len(photos) - 1)
    if photos[idx]['ts'] < ts or idx == 0:
        return idx
    else:
        return idx - 1