STRAVA API - 故事一

緣由:

過程:

要能提醒自己快逾期要出門跑,而馬拉松世界沒有可提醒的工具,
自己的問題自己想,於是把腦筯動到其它資料源,
GARMIN沒提供給個人使用,不考慮,
但就這麼剛好,STRAVA有提供API,還有完整的文件(http://developers.strava.com/docs/getting-started/),
簡直就是個寶庫,就決定是你了!

程式碼:

//[GET] 取得資料
using (HttpClient client = new HttpClient())
{
	try
	{
		string url = string.Format("https://www.strava.com/api/v3/athlete/activities?access_token={0}&per_page=1", "你的TOKEN");    
		var response = await client.GetStringAsync(url);
		//Console.WriteLine(response);

		IEnumerable<Activity> collections = JsonConvert.DeserializeObject<IEnumerable<Activity>>(response);
		
		foreach (Activity collection in collections)
		{
			Console.WriteLine("目前時間: " + DateTime.Now ); 
			Console.WriteLine("前次運動時間: " + collection.start_date_local);                 
			Console.WriteLine("逾期時間(+60hr): " + collection.start_date_local.AddHours(60) ); 
							
			TimeSpan ts = collection.start_date_local.AddHours(60).Subtract(DateTime.Now);
			Console.WriteLine("距離逾期還有: " + ts.Days.ToString()+"天"+ ts.Hours.ToString()+"小時"+ ts.Minutes.ToString()+"分鐘"  + ts.Seconds.ToString()+"秒"); 
		}
		
		client.Dispose();
	}
	catch (HttpRequestException e)
	{
		Console.WriteLine("\nException Caught!");
		Console.WriteLine("Message :{0} ", e.Message);
	}
}    

//JSON CLASS
public class Activity
{
	public int resource_state { get; set; }
	public Athlete athlete { get; set; }
	public string name { get; set; }
	public float distance { get; set; }
	public int moving_time { get; set; }
	public int elapsed_time { get; set; }
	public float total_elevation_gain { get; set; }
	public string type { get; set; }
	public object workout_type { get; set; }
	public long id { get; set; }
	public string external_id { get; set; }
	public long upload_id { get; set; }
	public DateTime start_date { get; set; }
	public DateTime start_date_local { get; set; }
	public string timezone { get; set; }
	public float utc_offset { get; set; }
	public float[] start_latlng { get; set; }
	public float[] end_latlng { get; set; }
	public object location_city { get; set; }
	public object location_state { get; set; }
	public string location_country { get; set; }
	public float start_latitude { get; set; }
	public float start_longitude { get; set; }
	public int achievement_count { get; set; }
	public int kudos_count { get; set; }
	public int comment_count { get; set; }
	public int athlete_count { get; set; }
	public int photo_count { get; set; }
	public Map map { get; set; }
	public bool trainer { get; set; }
	public bool commute { get; set; }
	public bool manual { get; set; }
	public bool _private { get; set; }
	public string visibility { get; set; }
	public bool flagged { get; set; }
	public object gear_id { get; set; }
	public bool from_accepted_tag { get; set; }
	public string upload_id_str { get; set; }
	public float average_speed { get; set; }
	public float max_speed { get; set; }
	public float average_cadence { get; set; }
	public int average_temp { get; set; }
	public bool has_heartrate { get; set; }
	public float average_heartrate { get; set; }
	public float max_heartrate { get; set; }
	public bool heartrate_opt_out { get; set; }
	public bool display_hide_heartrate_option { get; set; }
	public float elev_high { get; set; }
	public float elev_low { get; set; }
	public int pr_count { get; set; }
	public int total_photo_count { get; set; }
	public bool has_kudoed { get; set; }
}

public class Athlete
{
	public int id { get; set; }
	public int resource_state { get; set; }
}

public class Map
{
	public string id { get; set; }
	public string summary_polyline { get; set; }
	public int resource_state { get; set; }
}

參考: