Пример #1
0
    def parse_raw_data_to_nametuple(self, run_data, old_gpx_ids, with_gpx=False):
        run_data = run_data["runrecord"]
        joyrun_id = run_data["fid"]

        start_time = run_data["starttime"]
        end_time = run_data["endtime"]
        run_points_data = self.parse_content_to_ponits(run_data["content"])
        if with_gpx:
            # pass the track no points
            if run_points_data:
                gpx_data = self.parse_points_to_gpx(
                    run_points_data, start_time, end_time
                )
                download_joyrun_gpx(gpx_data, str(joyrun_id))
        heart_rate_list = eval(run_data["heartrate"]) if run_data["heartrate"] else None
        heart_rate = None
        if heart_rate_list:
            heart_rate = int(sum(heart_rate_list) / len(heart_rate_list))
            # fix #66
            if heart_rate < 0:
                heart_rate = None

        polyline_str = polyline.encode(run_points_data) if run_points_data else ""
        start_latlng = start_point(*run_points_data[0]) if run_points_data else None
        start_date = datetime.utcfromtimestamp(start_time)
        start_date_local = adjust_time(start_date, BASE_TIMEZONE)
        end = datetime.utcfromtimestamp(end_time)
        # only for China now
        end_local = adjust_time(end, BASE_TIMEZONE)
        location_country = None
        # joyrun location is kind of f*****g strage, so I decide not use it, if you want use it, uncomment this two lines
        # if run_data["city"] or run_data["province"]:
        #     location_country = str(run_data["city"]) + " " + str(run_data["province"])
        d = {
            "id": int(joyrun_id),
            "name": "run from joyrun",
            # future to support others workout now only for run
            "type": "Run",
            "start_date": datetime.strftime(start_date, "%Y-%m-%d %H:%M:%S"),
            "end": datetime.strftime(end, "%Y-%m-%d %H:%M:%S"),
            "start_date_local": datetime.strftime(
                start_date_local, "%Y-%m-%d %H:%M:%S"
            ),
            "end_local": datetime.strftime(end_local, "%Y-%m-%d %H:%M:%S"),
            "length": run_data["meter"],
            "average_heartrate": heart_rate,
            "map": run_map(polyline_str),
            "start_latlng": start_latlng,
            "distance": run_data["meter"],
            "moving_time": timedelta(seconds=run_data["second"]),
            "elapsed_time": timedelta(
                seconds=int((run_data["endtime"] - run_data["starttime"]))
            ),
            "average_speed": run_data["meter"] / run_data["second"],
            "location_country": location_country,
        }
        return namedtuple("x", d.keys())(*d.values())
Пример #2
0
def parse_no_gpx_data(activity):
    if not activity.get("metrics"):
        print(f"The activity {activity['id']} doesn't contain metrics information")
        return
    average_heartrate = None
    summary_info = activity.get("summaries")
    distance = 0

    for s in summary_info:
        if s.get("metric") == "distance":
            distance = s.get("value", 0) * 1000
        if s.get("metric") == "heart_rate":
            average_heartrate = s.get("value", None)
    # maybe training that no distance
    if not distance:
        return
    start_stamp = activity["start_epoch_ms"] / 1000
    end_stamp = activity["end_epoch_ms"] / 1000
    moving_time = timedelta(seconds=int(end_stamp - start_stamp))
    elapsed_time = timedelta(seconds=int(activity["active_duration_ms"] / 1000))

    nike_id = activity["end_epoch_ms"]
    start_date = datetime.utcfromtimestamp(activity["start_epoch_ms"] / 1000)
    start_date_local = adjust_time(start_date, BASE_TIMEZONE)
    end_date = datetime.utcfromtimestamp(activity["end_epoch_ms"] / 1000)
    end_date_local = adjust_time(end_date, BASE_TIMEZONE)
    d = {
        "id": int(nike_id),
        "name": "run from nike",
        "type": "Run",
        "start_date": datetime.strftime(start_date, "%Y-%m-%d %H:%M:%S"),
        "end": datetime.strftime(end_date, "%Y-%m-%d %H:%M:%S"),
        "start_date_local": datetime.strftime(start_date_local, "%Y-%m-%d %H:%M:%S"),
        "end_local": datetime.strftime(end_date_local, "%Y-%m-%d %H:%M:%S"),
        "length": distance,
        "average_heartrate": average_heartrate,
        "map": run_map(""),
        "start_latlng": None,
        "distance": distance,
        "moving_time": moving_time,
        "elapsed_time": elapsed_time,
        "average_speed": distance / int(activity["active_duration_ms"] / 1000),
        "location_country": "",
    }
    return namedtuple("x", d.keys())(*d.values())
Пример #3
0
def parse_run_endomondo_to_nametuple(en_dict):
    points = en_dict.get("points", [])
    location_points = []
    for p in points:
        for attr in p:
            if attr.get("location"):
                # WTF TODO? maybe more points?
                lat, lon = attr.get("location")[0]
                location_points.append([lat.get("latitude"), lon.get("longitude")])
    polyline_str = polyline.encode(location_points) if location_points else ""
    start_latlng = start_point(*location_points[0]) if location_points else None
    start_date = en_dict.get("start_time")
    start_date = datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S.%f")
    end_date = en_dict.get("end_time")
    end_date = datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S.%f")
    start_date_local = adjust_time(start_date, BASE_TIMEZONE)
    end_date_local = adjust_time(end_date, BASE_TIMEZONE)
    heart_rate = _make_heart_rate(en_dict)
    d = {
        "id": en_dict.get("id"),
        "name": "run from endomondo",
        "type": "Run",  # TODO others
        "start_date": datetime.strftime(start_date, "%Y-%m-%d %H:%M:%S"),
        "end": datetime.strftime(end_date, "%Y-%m-%d %H:%M:%S"),
        "start_date_local": datetime.strftime(start_date_local, "%Y-%m-%d %H:%M:%S"),
        "end_local": datetime.strftime(end_date_local, "%Y-%m-%d %H:%M:%S"),
        "length": en_dict.get("distance_km", 0) * 1000,
        "average_heartrate": int(heart_rate) if heart_rate else None,
        "map": run_map(polyline_str),
        "start_latlng": start_latlng,
        "distance": en_dict.get("distance_km", 0) * 1000,
        "moving_time": timedelta(seconds=en_dict.get("duration_s", 0)),
        "elapsed_time": timedelta(seconds=en_dict.get("duration_s", 0)),
        "average_speed": en_dict.get("distance_km", 0)
        / en_dict.get("duration_s", 1)
        * 1000,
        "location_country": "",
    }
    return namedtuple("x", d.keys())(*d.values())
Пример #4
0
def parse_raw_data_to_nametuple(run_data,
                                old_gpx_ids,
                                with_download_gpx=False):
    run_data = run_data["data"]
    run_points_data = []

    # 5898009e387e28303988f3b7_9223370441312156007_rn middle
    keep_id = run_data["id"].split("_")[1]

    start_time = run_data["startTime"]
    if run_data.get("vendor", {}).get(
            "source", "") == "Keep" and run_data.get("rawDataURL"):
        raw_data_url = run_data.get("rawDataURL")
        r = requests.get(raw_data_url)
        # string strart with `H4sIAAAAAAAA` --> decode and unzip
        run_points_data = decode_runmap_data(r.text)
        if with_download_gpx:
            if str(keep_id) not in old_gpx_ids:
                gpx_data = parse_points_to_gpx(run_points_data, start_time)
                download_keep_gpx(gpx_data, str(keep_id))
        run_points_data = [[p["latitude"], p["longitude"]]
                           for p in run_points_data]
    heart_rate = None
    if run_data["heartRate"]:
        heart_rate = run_data["heartRate"].get("averageHeartRate", None)
        # fix #66
        if heart_rate and heart_rate < 0:
            heart_rate = None
    polyline_str = polyline.encode(run_points_data) if run_points_data else ""
    start_latlng = start_point(
        *run_points_data[0]) if run_points_data else None
    start_date = datetime.utcfromtimestamp(start_time / 1000)
    tz_name = run_data.get("timezone", "")
    start_date_local = adjust_time(start_date, tz_name)
    end = datetime.utcfromtimestamp(run_data["endTime"] / 1000)
    end_local = adjust_time(end, tz_name)
    d = {
        "id":
        int(keep_id),
        "name":
        "run from keep",
        # future to support others workout now only for run
        "type":
        "Run",
        "start_date":
        datetime.strftime(start_date, "%Y-%m-%d %H:%M:%S"),
        "end":
        datetime.strftime(end, "%Y-%m-%d %H:%M:%S"),
        "start_date_local":
        datetime.strftime(start_date_local, "%Y-%m-%d %H:%M:%S"),
        "end_local":
        datetime.strftime(end_local, "%Y-%m-%d %H:%M:%S"),
        "length":
        run_data["distance"],
        "average_heartrate":
        int(heart_rate) if heart_rate else None,
        "map":
        run_map(polyline_str),
        "start_latlng":
        start_latlng,
        "distance":
        run_data["distance"],
        "moving_time":
        timedelta(seconds=run_data["duration"]),
        "elapsed_time":
        timedelta(seconds=int((run_data["endTime"] - run_data["startTime"]) /
                              1000)),
        "average_speed":
        run_data["distance"] / run_data["duration"],
        "location_country":
        str(run_data.get("region", "")),
    }
    return namedtuple("x", d.keys())(*d.values())