Skip to content

Commit

Permalink
[fix]根据单条纪录的响应寄过生成对应的json
Browse files Browse the repository at this point in the history
  • Loading branch information
hxiong committed May 2, 2024
1 parent 8da08a1 commit af03fc7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ _site/

config.yaml
.venv/
GPX_OUT
TCX_OUT
FIT_OUT
RESPONSE_OUT
5 changes: 3 additions & 2 deletions run_page/codoon_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
)
from generator import Generator
from tzlocal import get_localzone
from utils import adjust_time_to_utc, adjust_timestamp_to_utc, to_date
from utils import adjust_time_to_utc, adjust_timestamp_to_utc, to_date, write_response

# struct body
FitType = np.dtype(
Expand Down Expand Up @@ -478,7 +478,8 @@ def parse_points_to_gpx(self, run_points_data):
point = gpxpy.gpx.GPXTrackPoint(**p)
gpx_segment.points.append(point)
return gpx.to_xml()


@write_response("codoon")
def get_single_run_record(self, route_id):
print(f"Get single run for codoon id {route_id}")
payload = {
Expand Down
1 change: 1 addition & 0 deletions run_page/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
GPX_FOLDER = os.path.join(parent, "GPX_OUT")
TCX_FOLDER = os.path.join(parent, "TCX_OUT")
FIT_FOLDER = os.path.join(parent, "FIT_OUT")
RESPONSE_OUT = os.path.join(parent, "RESPONSE_OUT")
ENDOMONDO_FILE_DIR = os.path.join(parent, "Workouts")
FOLDER_DICT = {
"gpx": GPX_FOLDER,
Expand Down
5 changes: 3 additions & 2 deletions run_page/joyrun_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from config import BASE_TIMEZONE, GPX_FOLDER, JSON_FILE, SQL_FILE, run_map, start_point
from generator import Generator

from utils import adjust_time
from utils import adjust_time, write_response

get_md5_data = lambda data: md5(str(data).encode("utf-8")).hexdigest().upper()

Expand Down Expand Up @@ -243,7 +243,8 @@ def parse_points_to_gpx(
gpx_segment.points.append(point)

return gpx.to_xml()


@write_response("joyrun")
def get_single_run_record(self, fid):
payload = {
"fid": fid,
Expand Down
4 changes: 2 additions & 2 deletions run_page/keep_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from config import GPX_FOLDER, JSON_FILE, SQL_FILE, run_map, start_point
from Crypto.Cipher import AES
from generator import Generator
from utils import adjust_time
from utils import adjust_time,write_response
import xml.etree.ElementTree as ET

KEEP_SPORT_TYPES = ["running", "hiking", "cycling"]
Expand Down Expand Up @@ -73,7 +73,7 @@ def get_to_download_runs_ids(session, headers, sport_type):
break
return result


@write_response('keep')
def get_single_run_data(session, headers, run_id, sport_type):
r = session.get(
RUN_LOG_API.format(sport_type=sport_type, run_id=run_id), headers=headers
Expand Down
51 changes: 51 additions & 0 deletions run_page/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import time
import os
from datetime import datetime
from config import RESPONSE_OUT

import pytz

Expand Down Expand Up @@ -117,3 +119,52 @@ def upload_file_to_strava(client, file_name, data_type, force_to_run=True):
print(
f"Uploading {data_type} file: {file_name} to strava, upload_id: {r.upload_id}."
)

# 根据单条纪录的响应寄过生成对应的文件名
def keep_handler(response):
s_type = response['data']['type']
s_subtype = response['data']['subtype']
s_time = response['data']['startTime']
return f'{s_type}_{s_subtype}_{s_time}.json'

def codoon_handler(response):
s_type = response['data']['activity_type']
s_subtype = response['data']['sports_type']
s_time = response['data']['StartDateTime']
return f'{s_type}_{s_subtype}_{s_time}.json'

def joyrun_handler(response):
s_type = response['runrecord']['type']
s_time = response['runrecord']['starttime']
return f'{s_type}_{s_time}.json'

def write_response(file_type):
file_name_handler = None
if file_type == "keep":
file_name_handler = keep_handler
elif file_type == "codoon":
file_name_handler = codoon_handler
elif file_type == "joyrun":
file_name_handler = joyrun_handler

def decorator(func):
def wrapper(*args, **kwargs):
try:
response = func(*args, **kwargs)
if file_name_handler is None:
return response

file_path = os.path.join(RESPONSE_OUT, file_type)
file_name = file_name_handler(response)
full_file_path = os.path.join(file_path, file_name)
if not os.path.exists(file_path):
os.makedirs(file_path)
response_json = json.dumps(response, indent=4)
with open(full_file_path, "w") as fb:
fb.write(response_json)
return response
except Exception as e:
print(f"Error: {str(e)}")
return None
return wrapper
return decorator

0 comments on commit af03fc7

Please sign in to comment.