#!/usr/bin/python import requests import sys import os import json import time import polyline from shapely.geometry import Point, Polygon from datetime import datetime, timedelta import subprocess # from stravalib.client import Client # from stravalib.streams import StreamSet # from strava2gpx import strava2gpx # import asyncio now = datetime.now() dat = formatted_string = now.strftime("%Y%m%d_%H%M%S") # print("dat ", dat) myfilename = f"/tmp/Strava_review__FP--{dat}.txt" print ("myfilename ", myfilename) one_days_ago = now - timedelta(days=1) one_days_agoepoch = one_days_ago.timestamp() two_days_ago = now - timedelta(days=2) two_days_agoepoch = two_days_ago.timestamp() seven_days_ago = now - timedelta(days=7) seven_days_agoepoch = seven_days_ago.timestamp() twelve_days_ago = now - timedelta(days=12) twelve_days_agoepoch = twelve_days_ago.timestamp() epochuse = one_days_agoepoch # epochuse = two_days_agoepoch NwAcTkn = "" url = f"https://www.strava.com/api/v3/athlete/activities?access_token={NwAcTkn}&after={epochuse}" polygonroughpoplar = Polygon([(42.039504, -88.145025), (42.060274, -88.144898), (42.066303, -88.161407), (42.066247, -88.192758), (42.066271, -88.203740), (42.030322, -88.228334), (42.028388, -88.223401), (42.031798, -88.202157), (42.043887, -88.194595), (42.039184, -88.178744), (42.039466, -88.144910)]) polygonroughbusse = Polygon([(42.050531, -88.028554), (42.050928, -88.007339), (42.041016, -87.985926), (42.027918, -87.988193), (42.018289, -87.991657), (42.012361, -88.003293), (42.014739, -88.003029), (42.014105, -88.019392), (42.004240, -88.018812), (42.003907, -88.030892), (42.050360, -88.028554)]) polygonroughpauldoug = Polygon([(42.0673202288, -88.1215582704), (42.067275, -88.092755), (42.067222, -88.078874), (42.068550, -88.078095), (42.072426, -88.078115), (42.075069, -88.078066), (42.078832, -88.078129), (42.079320, -88.078083), (42.079429, -88.078445), (42.079606, -88.078831), (42.080726, -88.080886), (42.081852, -88.083088), (42.082738, -88.085186), (42.083493, -88.087183), (42.084145, -88.089335), (42.084773, -88.092140), (42.084922, -88.092615), (42.084600, -88.092850), (42.085480, -88.096734), (42.086024, -88.099167), (42.088288, -88.109199), (42.091106, -88.121626), (42.067263, -88.121598)]) client_id = "" client_secret = "" refresh_token = "" in_fpdcc = False # print(url) # sys.exit() try: # response = requests.get(url, stream=True) response = requests.get(url) response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx) except requests.exceptions.RequestException as e: print(f"Error getting: {e}") print(response.status_code) # GtgNewAcTkn=`curl -X POST 'https://www.strava.com/oauth/token' -H 'Content-Type: application/json' -d '{ "client_id": "", # "client_secret": "", # "refresh_token": "", # "grant_type": "refresh_token" # }'` urlrefrsh = "https://www.strava.com/oauth/token" payloadrefrsh = {"client_id": "", "client_secret": "", "refresh_token": "", "grant_type": "refresh_token"} headersrefrsh = {"Content-Type": "application/json"} responserefrsh = requests.post(urlrefrsh, json=payloadrefrsh, headers=headersrefrsh) print("responserefrsh",responserefrsh) print(responserefrsh.text) responserefrshtextjson = json.loads(responserefrsh.text) # print("") # print(responserefrshtextjson["access_token"]) NwAcTkn = responserefrshtextjson["access_token"] print("") print(NwAcTkn) # url = f"https://www.strava.com/api/v3/athlete/activities?access_token={NwAcTkn}&after=1748630243" url = f"https://www.strava.com/api/v3/athlete/activities?access_token={NwAcTkn}&after={epochuse}" response = requests.get(url) # print(response.text) Activities=json.loads(response.text) # print(Activities[0]) print("--------------_________________===================") for item in Activities: print(f"Name: {item['name']}, id: {item['id']}") Activ0id=item['id'] # Activity=`wget -O - "https://www.strava.com/api/v3/activities/$Activ0id?access_token=$NwAcTkn"` url = f"https://www.strava.com/api/v3/activities/{Activ0id}?access_token={NwAcTkn}" urlstream = f"https://www.strava.com/api/v3/activities/{Activ0id}/streams?keys=time,distance,latlng,altitude,velocity_smooth,heartrate,cadence,watts,temp,moving,grade_smooth&access_token={NwAcTkn}" # print("url ", url) # print("urlstream ", urlstream) response = requests.get(url) responsestream = requests.get(urlstream) # print("responsestream ", responsestream) Activity=json.loads(response.text) # print("Activity ", Activity) Activitystream=json.loads(responsestream.text) # print("Activitystream ", Activitystream) # print("====---") ActivityName=Activity['name'] print("ActivityName ", ActivityName) Activitystart_date_local=Activity['start_date_local'] print("Activitystart_date_local ", Activitystart_date_local) Activitystreamlatlng = Activitystream[2] # print("====---___") ActivityPolyline=Activity['map']['polyline'] # print("ActivityPolyline ", ActivityPolyline) Activitystreamlatlngdata = Activitystreamlatlng["data"] in_poplar = False in_busse = False in_pauldoug = False # time.sleep(3) # coordinates = polyline.decode(ActivityPolyline) coordinates = Activitystreamlatlngdata print('') for coord in coordinates: point=Point(coord) # print(point) is_within = point.within(polygonroughpoplar) # print(' ',is_within) if is_within and not in_poplar: in_poplar = True in_fpdcc = True enterpoplarcoord=coord if not is_within and in_poplar: exitpoplarcoord=coord else: exitpoplarcoord="[0, 0]" is_within = point.within(polygonroughbusse) if is_within and not in_busse: in_busse = True in_fpdcc = True enterbussecoord=coord if not is_within and in_busse: exitbussecoord=coord else: exitbussecoord="[0, 0]" is_within = point.within(polygonroughpauldoug) if is_within and not in_pauldoug: in_pauldoug = True in_fpdcc = True enterpauldougcoord=coord if not is_within and in_pauldoug: exitpauldougcoord=coord else: exitpauldougcoord="[0, 0]" print('Did this go through poplar ',in_poplar) if in_poplar: print('enterpoplarcoord ',enterpoplarcoord) print('exitpoplarcoord ',exitpoplarcoord) try: first_index = Activitystreamlatlngdata.index(enterpoplarcoord) print(f"First index where value equals {enterpoplarcoord}: {first_index}") print(f"Entering happened at {enterpoplarcoord}; {first_index} seconds into the ride") Activitystart_date_local formatdate = "%Y-%m-%dT%H:%M:%SZ" Activitystart_date_local_dateob = datetime.strptime(Activitystart_date_local, formatdate) time_delta = timedelta(seconds=first_index) Entrerstart_date_local_dateob = Activitystart_date_local_dateob + time_delta print(f"The local time the ride started was at {Activitystart_date_local_dateob}") print(f"The local time the entering was at {Entrerstart_date_local_dateob}") with open(myfilename, "a") as file: file.write(f"ActivityName : {ActivityName}\n") file.write(f"Strava URL : https://www.strava.com/activities/{Activ0id} \n") file.write("This is in Poplar Creek\n") file.write(f"First index where value equals {enterpoplarcoord}: {first_index}.\n") file.write(f"Entering happened at {enterpoplarcoord}; {first_index} seconds into the ride.\n") file.write("\n") file.write(f"The local time the ride started was at {Activitystart_date_local_dateob}\n") file.write(f"The local time the entering was at {Entrerstart_date_local_dateob}\n **********") file.write("\n") except ValueError: print(f"{enterpoplarcoord} not found in the list.") with open(myfilename, "a") as file: file.write(f"{enterpoplarcoord} not found in the list.\n") file.write("\n") try: first_index = Activitystreamlatlngdata.index(exitpoplarcoord) print(f"First index where value equals {exitpoplarcoord}: {first_index}") print(f"Existing happened at {exitpoplarcoord}; {first_index} seconds into the ride") with open(myfilename, "a") as file: file.write(f"First index where value equals {exitpoplarcoord}: {first_index}\n") file.write(f"Existing happened at {exitpoplarcoord}; {first_index} seconds into the ride\n") file.write("\n") time_delta = timedelta(seconds=first_index) Existstart_date_local_dateob = Activitystart_date_local_dateob + time_delta print(f"Tpe local time the ride started was at {Activitystart_date_local_dateob}\n") print(f"The local time the exiting was at {Existstart_date_local_dateob}") with open(myfilename, "a") as file: file.write(f"Tpe local time the ride started was at {Activitystart_date_local_dateob}\n") file.write(f"The local time the exiting was at {Existstart_date_local_dateob}\n *********") file.write("\n") except ValueError: print(f"{exitpoplarcoord} not found in the list.") with open(myfilename, "a") as file: file.write(f"{exitpoplarcoord} not found in the list.\n") file.write("\n") time.sleep(4) print("") print('Did this go through busse ',in_busse) if in_busse: print('enterbussecoord ',enterbussecoord) print('exitbussecoord ',exitbussecoord) try: first_index = Activitystreamlatlngdata.index(enterbussecoord) print(f"First index where value equals {enterbussecoord}: {first_index}") print(f"Entering happened at {enterbussecoord}; {first_index} seconds into the ride") Activitystart_date_local formatdate = "%Y-%m-%dT%H:%M:%SZ" Activitystart_date_local_dateob = datetime.strptime(Activitystart_date_local, formatdate) time_delta = timedelta(seconds=first_index) Entrerstart_date_local_dateob = Activitystart_date_local_dateob + time_delta print(f"The local time the ride started was at {Activitystart_date_local_dateob}") print(f"The local time the entering was at {Entrerstart_date_local_dateob}") with open(myfilename, "a") as file: file.write(f"ActivityName : {ActivityName}\n") file.write(f"Strava URL : https://www.strava.com/activities/{Activ0id} \n") file.write("This is in Busse Woods\n") file.write(f"First index where value equals {enterbussecoord}: {first_index}.\n") file.write(f"Entering happened at {enterbussecoord}; {first_index} seconds into the ride.\n") file.write("\n") file.write(f"The local time the ride started was at {Activitystart_date_local_dateob}\n") file.write(f"The local time the entering was at {Entrerstart_date_local_dateob}\n **********") file.write("\n") except ValueError: print(f"{enterbussecoord} not found in the list.") with open(myfilename, "a") as file: file.write(f"{enterbussecoord} not found in the list.\n") file.write("\n") try: first_index = Activitystreamlatlngdata.index(exitbussecoord) print(f"First index where value equals {exitbussecoord}: {first_index}") print(f"Existing happened at {exitbussecoord}; {first_index} seconds into the ride") with open(myfilename, "a") as file: file.write(f"First index where value equals {exitbussecoord}: {first_index}\n") file.write(f"Existing happened at {exitbussecoord}; {first_index} seconds into the ride\n") file.write("\n") time_delta = timedelta(seconds=first_index) Existstart_date_local_dateob = Activitystart_date_local_dateob + time_delta print(f"Tpe local time the ride started was at {Activitystart_date_local_dateob}") print(f"The local time the exiting was at {Existstart_date_local_dateob}") with open(myfilename, "a") as file: file.write(f"Tpe local time the ride started was at {Activitystart_date_local_dateob}\n") file.write(f"The local time the exiting was at {Existstart_date_local_dateob}\n *********") file.write("\n") except ValueError: print(f"{exitbussecoord} not found in the list.") with open(myfilename, "a") as file: file.write(f"{exitbussecoord} not found in the list.\n") file.write("\n") print("") time.sleep(2) print('Did this go through paul douglas ',in_pauldoug) if in_pauldoug: print('enterpauldougcoord ',enterpauldougcoord) print('exitpauldougcoord ',exitpauldougcoord) try: first_index = Activitystreamlatlngdata.index(enterpauldougcoord) print(f"First index where value equals {enterpauldougcoord}: {first_index}") print(f"Entering happened at {enterpauldougcoord}; {first_index} seconds into the ride") Activitystart_date_local formatdate = "%Y-%m-%dT%H:%M:%SZ" Activitystart_date_local_dateob = datetime.strptime(Activitystart_date_local, formatdate) time_delta = timedelta(seconds=first_index) Entrerstart_date_local_dateob = Activitystart_date_local_dateob + time_delta print(f"The local time the ride started was at {Activitystart_date_local_dateob}") print(f"The local time the entering was at {Entrerstart_date_local_dateob}") with open(myfilename, "a") as file: file.write(f"ActivityName : {ActivityName}\n") file.write(f"Strava URL : https://www.strava.com/activities/{Activ0id} \n") file.write("This is in Paul Douglas\n") file.write(f"First index where value equals {enterpauldougcoord}: {first_index}.\n") file.write(f"Entering happened at {enterpauldougcoord}; {first_index} seconds into the ride.\n") file.write("\n") file.write(f"The local time the ride started was at {Activitystart_date_local_dateob}\n") file.write(f"The local time the entering was at {Entrerstart_date_local_dateob}\n **********") file.write("\n") except ValueError: print(f"{enterpauldougcoord} not found in the list.") with open(myfilename, "a") as file: file.write(f"{enterpauldougcoord} not found in the list.\n") file.write("\n") try: first_index = Activitystreamlatlngdata.index(exitpauldougcoord) print(f"First index where value equals {exitpauldougcoord}: {first_index}") print(f"Existing happened at {exitpauldougcoord}; {first_index} seconds into the ride") with open(myfilename, "a") as file: file.write(f"First index where value equals {exitpauldougcoord}: {first_index}\n") file.write(f"Existing happened at {exitpauldougcoord}; {first_index} seconds into the ride\n") file.write("\n") time_delta = timedelta(seconds=first_index) Existstart_date_local_dateob = Activitystart_date_local_dateob + time_delta print(f"Tpe local time the ride started was at {Activitystart_date_local_dateob}") print(f"The local time the exiting was at {Existstart_date_local_dateob}") with open(myfilename, "a") as file: file.write(f"Tpe local time the ride started was at {Activitystart_date_local_dateob}\n") file.write(f"The local time the exiting was at {Existstart_date_local_dateob}\n *********") file.write("\n") except ValueError: print(f"{exitpualdougcoord} not found in the list.") with open(myfilename, "a") as file: file.write(f"{exitpauldougcoord} not found in the list.\n") file.write("\n") print("") time.sleep(2) print("") print("======___________----------") # with open(myfilename, "a") as file: # file.write(f"\n---______-----\n") time.sleep(2) print('did any go through fpdcc ',in_fpdcc) print("done with loop activities") if os.path.exists(myfilename): print(f"The file '{myfilename}' exists.") with open(myfilename, "a") as file: file.write("\n \n \n") file.write(f"report with FPDCC at https://volunteer-fpdcc.givepulse.com/group/804565-Volunteer-FPDCC .\n") file.write(f" https://volunteer-fpdcc.givepulse.com/impact/add?group_id=927201&event_id=413728 .\n") tmp = f"/home/mswier/scrpts/copy_Strava_review_Google_mswier9.sh {myfilename}" os.system(tmp) print("Done")