调用高德API 进行对接钉钉的签到的经纬度计算行程轨迹
调用高德API 进行对接钉钉的签到的经纬度计算行程轨迹
import json import requests key_amap = '1c807c7b070eac7b6ffa7cd894bb112a49'
# 生产环境中,可以根据钉钉签到中获取到的经纬度进行构造这个origin 和 destination origin = '28.856221,120.741824' destination = '28.919571,121.250241' # url = f"https://restapi.amap.com/v3/direction/driving?origin={origin}&destination={destination}&extensions=all&output=xml&key={key_amap}" # url = f"https://restapi.amap.com/v3/direction/driving?key={key_amap}&origin=120.741824,28.856221&destination=121.250241,28.919571&extensions=base&strategy=5&waypoints=+0&avoidpolygons=" url = f"https://restapi.amap.com/v5/direction/driving?origin=120.741824,28.856221&destination=121.250241,28.919571&key={key_amap}&strategy=0" data = requests.get(url).text count_paths = json.loads(data).get('count') print('导航路线数量:', count_paths) distance_count_list = json.loads(data).get('route').get('paths') distance_count_list = [item['distance'] for item in distance_count_list] print(distance_count_list) """ API :https://lbs.amap.com/api/webservice/guide/api/newroute """ # # pattern = re.compile(r'"instruction":"(.*?)".*?"road":"(.*?)".*?"distance":"(\d+)".*?"duration":"(\d+)"', re.S) # # # Extract data # matches = pattern.findall(data) # # print(["-->\n\n".join(match) for match in matches]) # # Format into a list of dictionaries # extracted_routes = [{"instruction": match[0], "road": match[1], "distance": match[2], "duration": match[3]} for match in matches] # # print(extracted_routes) # # Convert to JSON # route_json = json.dumps(extracted_routes, ensure_ascii=False, indent=2) # pprint(route_json) # # print(extracted_routes)
下面是获取钉钉中签到中的经纬度的代码
from datetime import datetime import dingtalk.api def date_to_milliseconds(date_str, date_format="%Y-%m-%d %H:%M:%S"): """ 将日期字符串转换为毫秒级时间戳。 :param date_str: 日期字符串 :param date_format: 日期字符串的格式,默认为"%Y-%m-%d %H:%M:%S" :return: 毫秒级时间戳 """ dt = datetime.strptime(date_str, date_format) timestamp_ms = int(dt.timestamp() * 1000) return timestamp_ms def get_checkin_records(access_token): req = dingtalk.api.OapiCheckinRecordRequest("https://oapi.dingtalk.com/checkin/record") req.department_id = 1 req.start_time = date_to_milliseconds("2024-12-01 00:00:00") req.end_time = date_to_milliseconds("2024-12-02 00:00:00") req.order = "desc" resp = req.getResponse(access_token) # print(resp.get('data')) for item in resp.get('data'): longitude_str = f"{item['longitude']:.6f}" latitude_str = f"{item['latitude']:.6f}" location_str = f"({longitude_str},{latitude_str})" print(location_str) return resp if __name__ == '__main__': get_checkin_records('your access key')
特别注意的是:dingtalk.opi 这个库要去钉钉的API网站去下载。
目前这个还没有完成,现在记录下来。备用