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網站去下載。
目前這個還沒有完成,現在記錄下來。備用