Exemplo n.º 1
0
def generate_user_home_work(user):
    user_home=detect_home(user)
    # print user_home
    zip_is_valid = _check_zip_validity(user_home, user)
    logging.debug('starting for %s' % user)
    if Profiles.find({'user_id':user}).count()==0:
        profile_todo={'source':'Shankari','user_id': user,'home':user_home}
        Profiles.insert(profile_todo)
    else:
        #TODO: make detect_home return something better than a N/A string
        Profiles.update({"$and":[{'source':'Shankari'},
                                     {'user_id':user}]},{"$set":{'home':user_home}})
    user_work=detect_work_office(user)
    Profiles.update({"$and":[{'source':'Shankari'},{'user_id':user}]},{"$set":{'work_place':user_work}})
    user_zip=get_userZipcode(user, zip_is_valid)
    Profiles.update({"$and":[{'source':'Shankari'},{'user_id':user}]},{"$set":{'zip':user_zip}})
    if user_zip!='N/A':
        geoinfo= Geocoder.geocode(user_zip)
        # geocoder returns data in lat,lng format.
        # we convert to lng,lat internally, since that is the format that the
        # rest of our code is in
        zipCen=[geoinfo[0].coordinates[1],geoinfo[0].coordinates[0]]
    else:
        zipCen='N/A'
    Profiles.update({"$and":[{'source':'Shankari'},{'user_id':user}]},{"$set":{'zip_centroid':zipCen}})


    for day in range(1,6):
        key='work'+str(day)
        Profiles.update({"$and":[{'source':'Shankari'},
                                     {'user_id':user}]},{"$set":{key:detect_daily_work_office(user,day)}})
Exemplo n.º 2
0
def get_daily_evening_commute_sections(user_id, day):
    # say should be from 1 to 5
    # get a list of all the sections for Mon, or ...
    earlist_start = 5
    earlist_end = 15
    Sections = edb.get_section_db()
    list_of_commute = []
    candidate_sections = []
    home = eamh.detect_home(user_id)
    work = eamw.detect_daily_work_office(user_id, day)
    if work == "N/A":
        return []
    else:
        # print(list_first_pnt)
        for section in Sections.find(
            {"$and": [{"user_id": user_id}, {"section_start_point": {"$ne": None}}, {"commute": {"$exists": False}}]}
        ):
            time2 = ec.parse_time(section["section_start_time"])
            if (
                ec.Is_date(section["section_start_time"], day)
                and (time2 - time2.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 3600
                > earlist_end
            ) or (
                ec.Is_date(section["section_start_time"], day + 1)
                and (time2 - time2.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds() / 3600
                < earlist_start
            ):
                candidate_sections.append(section)

        if len(candidate_sections) > 0:
            candidate_sections = sorted(candidate_sections, key=lambda k: parser.parse(k["section_start_time"]))
            min_sec = len(candidate_sections)
            for i in range(len(candidate_sections) - 1, -1, -1):
                if i <= min_sec:
                    if ec.Is_place(candidate_sections[i]["section_end_point"], home, 200):
                        for j in range(i, -1, -1):
                            if (
                                ec.Is_place(candidate_sections[j]["section_start_point"], work, 200)
                                and ec.travel_time(
                                    candidate_sections[j]["section_start_time"],
                                    candidate_sections[i]["section_end_time"],
                                )
                                <= 24 * 60 * 60
                            ):
                                sections_todo = []
                                sections_todo.extend(candidate_sections[j : i + 1])
                                list_of_commute.append(sections_todo)
                                min_sec = j - 1
                                break
        return list_of_commute
Exemplo n.º 3
0
def get_daily_evening_commute_sections(user_id, day):
    # say should be from 1 to 5
    # get a list of all the sections for Mon, or ...
    earlist_start = 5
    earlist_end = 15
    Sections = edb.get_section_db()
    list_of_commute = []
    candidate_sections = []
    home = eamh.detect_home(user_id)
    work = eamw.detect_daily_work_office(user_id, day)
    if work == 'N/A':
        return []
    else:
        # print(list_first_pnt)
        for section in Sections.find({"$and":[{"user_id": user_id},{ "section_start_point": { "$ne": None }},\
                                              {'commute':{ "$exists": False }}]}):
            time2 = ec.parse_time(section['section_start_time'])
            if (ec.Is_date(section['section_start_time'],day) and (time2 - time2.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()/3600>earlist_end) or \
                    (ec.Is_date(section['section_start_time'],day+1) and (time2 - time2.replace(hour=0, minute=0, second=0, microsecond=0)).total_seconds()/3600<earlist_start):
                candidate_sections.append(section)

        if len(candidate_sections) > 0:
            candidate_sections = sorted(
                candidate_sections,
                key=lambda k: parser.parse(k['section_start_time']))
            min_sec = len(candidate_sections)
            for i in range(len(candidate_sections) - 1, -1, -1):
                if i <= min_sec:
                    if ec.Is_place(candidate_sections[i]['section_end_point'],
                                   home, 200):
                        for j in range(i, -1, -1):
                            if ec.Is_place(candidate_sections[j]['section_start_point'],work,200) and \
                                            ec.travel_time(candidate_sections[j]['section_start_time'],\
                                                        candidate_sections[i]['section_end_time'])<=24*60*60:
                                sections_todo = []
                                sections_todo.extend(candidate_sections[j:i +
                                                                        1])
                                list_of_commute.append(sections_todo)
                                min_sec = j - 1
                                break
        return list_of_commute
Exemplo n.º 4
0
def get_daily_morning_commute_sections(user_id, day):
    # say should be from 1 to 5
    # get a list of all the sections for Mon, or ...
    Sections = edb.get_section_db()
    list_of_commute = []
    candidate_sections = []
    home = eamh.detect_home(user_id)
    work = eamw.detect_daily_work_office(user_id, day)
    if work == "N/A":
        return []
    else:
        # print(list_first_pnt)
        for section in Sections.find(
            {"$and": [{"user_id": user_id}, {"section_start_point": {"$ne": None}}, {"commute": {"$exists": False}}]}
        ):
            if ec.Is_date(section["section_start_time"], day):
                candidate_sections.append(section)

        if len(candidate_sections) > 0:
            candidate_sections = sorted(candidate_sections, key=lambda k: parser.parse(k["section_start_time"]))
            max_sec = 0
            for i in range(len(candidate_sections)):
                if i >= max_sec:
                    if ec.Is_place(candidate_sections[i]["section_start_point"], home, 200):
                        for j in range(i, len(candidate_sections)):
                            if (
                                ec.Is_place(candidate_sections[j]["section_end_point"], work, 200)
                                and ec.travel_time(
                                    candidate_sections[i]["section_start_time"],
                                    candidate_sections[j]["section_end_time"],
                                )
                                <= 24 * 60 * 60
                            ):
                                sections_todo = []
                                sections_todo.extend(candidate_sections[i : j + 1])
                                list_of_commute.append(sections_todo)
                                max_sec = j + 1
                                break
        return list_of_commute
Exemplo n.º 5
0
def get_daily_morning_commute_sections(user_id, day):
    # say should be from 1 to 5
    # get a list of all the sections for Mon, or ...
    Sections = edb.get_section_db()
    list_of_commute = []
    candidate_sections = []
    home = eamh.detect_home(user_id)
    work = eamw.detect_daily_work_office(user_id, day)
    if work == 'N/A':
        return []
    else:
        # print(list_first_pnt)
        for section in Sections.find({"$and":[{"user_id": user_id},{ "section_start_point": { "$ne": None }},\
                                              {'commute':{ "$exists": False }}]}):
            if ec.Is_date(section['section_start_time'], day):
                candidate_sections.append(section)

        if len(candidate_sections) > 0:
            candidate_sections = sorted(
                candidate_sections,
                key=lambda k: parser.parse(k['section_start_time']))
            max_sec = 0
            for i in range(len(candidate_sections)):
                if i >= max_sec:
                    if ec.Is_place(
                            candidate_sections[i]['section_start_point'], home,
                            200):
                        for j in range(i, len(candidate_sections)):
                            if ec.Is_place(candidate_sections[j]['section_end_point'],work,200) and \
                                            ec.travel_time(candidate_sections[i]['section_start_time'],\
                                                        candidate_sections[j]['section_end_time'])<=24*60*60:
                                sections_todo = []
                                sections_todo.extend(candidate_sections[i:j +
                                                                        1])
                                list_of_commute.append(sections_todo)
                                max_sec = j + 1
                                break
        return list_of_commute
Exemplo n.º 6
0
def generate_user_home_work(user):
    user_home = detect_home(user)
    # print user_home
    zip_is_valid = _check_zip_validity(user_home, user)
    logging.debug('starting for %s' % user)
    if Profiles.find({'user_id': user}).count() == 0:
        profile_todo = {
            'source': 'Shankari',
            'user_id': user,
            'home': user_home
        }
        Profiles.insert(profile_todo)
    else:
        #TODO: make detect_home return something better than a N/A string
        Profiles.update({"$and": [{
            'source': 'Shankari'
        }, {
            'user_id': user
        }]}, {"$set": {
            'home': user_home
        }})
    user_work = detect_work_office(user)
    Profiles.update({"$and": [{
        'source': 'Shankari'
    }, {
        'user_id': user
    }]}, {"$set": {
        'work_place': user_work
    }})
    user_zip = get_userZipcode(user, zip_is_valid)
    Profiles.update({"$and": [{
        'source': 'Shankari'
    }, {
        'user_id': user
    }]}, {"$set": {
        'zip': user_zip
    }})
    if user_zip != 'N/A':
        geoinfo = Geocoder.geocode(user_zip)
        # geocoder returns data in lat,lng format.
        # we convert to lng,lat internally, since that is the format that the
        # rest of our code is in
        zipCen = [geoinfo[0].coordinates[1], geoinfo[0].coordinates[0]]
    else:
        zipCen = 'N/A'
    Profiles.update({"$and": [{
        'source': 'Shankari'
    }, {
        'user_id': user
    }]}, {"$set": {
        'zip_centroid': zipCen
    }})

    for day in range(1, 6):
        key = 'work' + str(day)
        Profiles.update({"$and": [{
            'source': 'Shankari'
        }, {
            'user_id': user
        }]}, {"$set": {
            key: detect_daily_work_office(user, day)
        }})