Exemple #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())
    def parse_raw_data_to_namedtuple(self, run_data, old_gpx_ids, with_gpx=False):
        run_data = run_data["data"]
        log_id = run_data["id"]

        start_time = run_data.get("start_time")
        if not start_time:
            return
        end_time = run_data["end_time"]
        run_points_data = run_data["points"] if "points" in run_data else None

        latlng_data = self.parse_latlng(run_points_data)
        if with_gpx:
            # pass the track no points
            if str(log_id) not in old_gpx_ids and run_points_data:
                gpx_data = self.parse_points_to_gpx(run_points_data)
                download_codoon_gpx(gpx_data, str(log_id))
        heart_rate = None

        polyline_str = polyline.encode(latlng_data) if latlng_data else ""
        start_latlng = start_point(*latlng_data[0]) if latlng_data else None
        start_date = self._gt(start_time)
        end_date = self._gt(end_time)
        location_country = None
        sport_type = run_data["sports_type"]
        # only support run now, if you want all type comments these two lines
        if IS_ONLY_RUN and sport_type != 1:
            return
        cast_type = TYPE_DICT[sport_type] if sport_type in TYPE_DICT else sport_type
        d = {
            "id": log_id,
            "name": str(cast_type) + " from codoon",
            "type": cast_type,
            "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, "%Y-%m-%d %H:%M:%S"),
            "end_local": datetime.strftime(end_date, "%Y-%m-%d %H:%M:%S"),
            "length": run_data["total_length"],
            "average_heartrate": heart_rate,
            "map": run_map(polyline_str),
            "start_latlng": start_latlng,
            "distance": run_data["total_length"],
            "moving_time": timedelta(seconds=run_data["total_time"]),
            "elapsed_time": timedelta(
                seconds=int((end_date.timestamp() - start_date.timestamp()))
            ),
            "average_speed": run_data["total_length"] / run_data["total_time"],
            "location_country": location_country,
            "source": "Codoon",
        }
        return namedtuple("x", d.keys())(*d.values())
Exemple #3
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())