def get_places(stays): place_stay = dict() for stay in stays: lat = stay[0][0] / (10**7) long = stay[0][1] / (10**7) print(lat, long) found_places = httprequests.getLocation(lat, long) if found_places: place_name = found_places[0]['name'] start = datetime.datetime.fromtimestamp(stay[1] / 1000.0) end = datetime.datetime.fromtimestamp(stay[2] / 1000.0) duration = end - start if place_name not in place_stay: place_stay[place_name] = duration else: place_stay[place_name] = place_stay[place_name] + duration sorted_by_stay = sorted(place_stay.items(), key=lambda kv: kv[1], reverse=True) print(len(sorted_by_stay)) with open('stay.csv', 'w') as f: csv_out = csv.writer(f) for row in sorted_by_stay: csv_out.writerow(row) for place, stay in place_stay.items(): print("%s at %s" % (stay, place))
def get_places(stays): place_stay = dict() fsq_info = dict() for stay in stays: (lat, long) = stay[0] found_places = httprequests.getLocation(lat, long) if found_places: most_likely_place = found_places[0] place_info = { 'lat': most_likely_place['lat'], 'long': most_likely_place['lon'], 'id': most_likely_place['_id'], 'name': most_likely_place['name'] } place_id = found_places[0]['_id'] fsq_info[place_id] = place_info start = datetime.datetime.fromtimestamp( stay[1] / 1000.0, tz=pytz.timezone('America/New_York')) end = datetime.datetime.fromtimestamp( stay[2] / 1000.0, tz=pytz.timezone('America/New_York')) duration = end - start if place_id not in place_stay: place_stay[place_id] = duration else: place_stay[place_id] = place_stay[place_id] + duration sorted_by_stay = sorted(place_stay.items(), key=lambda kv: kv[1], reverse=True) with open('stay.csv', 'w') as f: filed_names = ['id', 'lat', 'long', 'name', 'time'] writer = csv.DictWriter(f=f, fieldnames=filed_names) writer.writeheader() for row in sorted_by_stay: id = row[0] time = row[1] place_info = fsq_info[id] place_info['time'] = time writer.writerow(place_info) for place, stay in place_stay.items(): print("%s at %s" % (stay, place))
def analyse_location_history(json_file): last_location_name = "" last_location_recorded = False # A 'total' dict, the key is the place ID and the value is the number of visits place_time_dict = dict() place_stay_length_dict = dict() # A dict for each day place_time_dict_by_date = dict() place_stay_length_dict_by_date = dict() place_info_dict = dict() # Only consider the last 30 days # time_threshold = datetime.datetime.today() - datetime.timedelta(days=30) time_threshold = datetime.datetime(2019, 7, 1) time_upper_limit = datetime.datetime(2019, 7, 30) last_timestamp = None data = json.load(json_file) for location in data['locations']: # Only consider it if it's a stay if is_stay(location): timestamp = int(location['timestampMs']) if not last_timestamp: last_timestamp = timestamp date_time = datetime.datetime.fromtimestamp(timestamp // 1000.0) if date_time > time_threshold and date_time < time_upper_limit: date = date_time.date() if not (date in place_time_dict_by_date): place_time_dict_by_date[date] = dict() place_stay_length_dict_by_date[date] = dict() lat = location['latitudeE7'] / (10**7) long = location['longitudeE7'] / (10**7) found_places = httprequests.getLocation(lat, long) if found_places: most_confident_place = found_places[0] print(most_confident_place['confidence']) found_place_name = most_confident_place['name'] print(found_place_name) found_place_id = most_confident_place['_id'] if not (found_place_id in place_info_dict): place_info_dict[found_place_id] = most_confident_place if (found_place_name == last_location_name): if not last_location_recorded: # The user is still at the same place and the place is not recorded yet # so we consider the user stays there for a while if found_place_id in place_time_dict: place_time_dict[found_place_id] += 1 else: place_time_dict[found_place_id] = 1 if found_place_id in place_time_dict_by_date[date]: place_time_dict_by_date[date][ found_place_id] += 1 else: place_time_dict_by_date[date][ found_place_id] = 1 last_location_recorded = True else: # The user spends more time at the place duration = timestamp - last_timestamp if found_place_id in place_stay_length_dict: place_stay_length_dict[ found_place_id] += duration else: place_stay_length_dict[found_place_id] = 0 if found_place_id in place_stay_length_dict_by_date[ date]: place_stay_length_dict_by_date[date][ found_place_id] += duration else: place_stay_length_dict_by_date[date][ found_place_id] = 0 else: if found_place_name != last_location_name: last_location_recorded = False last_location_name = most_confident_place['name'] last_timestamp = timestamp # for found_place in found_places: # print (found_place['confidence']) # found_place_name = found_place['name'] # print (found_place_name) # found_place_id = found_place['_id'] # if not (found_place_id in place_info_dict): # place_info_dict[found_place_id] = found_place # if (found_place_name == last_location_name) and (last_location_recorded == False): # # The user is still at the same place and the place is not recorded yet # # so we consider the user stays there for a while # if found_place_id in place_time_dict: # place_time_dict[found_place_id] += 1 # else: # place_time_dict[found_place_id] = 1 # if found_place_id in place_time_dict_by_date[date]: # place_time_dict_by_date[date][found_place_id] += 1 # else: # place_time_dict_by_date[date][found_place_id] = 1 # last_location_recorded = True # else: # if (found_place_name != last_location_name): # last_location_recorded = False # last_location_name = found_place['name'] # return # Sort the places by the degree of segregation sorted_places = sorted(place_info_dict.items(), key=lambda x: x[1]['segregation']) for (place_id, duration) in place_stay_length_dict.items(): time = datetime.timedelta(milliseconds=duration) print("You spent %s time at %s" % (time, place_info_dict[place_id]['name']))