-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
executable file
·119 lines (107 loc) · 4.58 KB
/
Program.cs
File metadata and controls
executable file
·119 lines (107 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.IO;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Fitness.v1;
using Google.Apis.Fitness.v1.Data;
using System.Security.Cryptography.X509Certificates;
using System.Collections.Generic;
using System.Threading;
using Google.Apis.Util.Store;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Configuration;
using Weighter.Google;
using Weighter;
using System.Linq;
using System.Globalization;
namespace weighter
{
class Program
{
static void Main(string[] args)
{
var services = new ServiceCollection();
ConfigureServices(services);
var serviceProvider = services.BuildServiceProvider();
var app = serviceProvider.GetService<Application>();
Task.Run(() => app.Run()).Wait();
}
private static void ConfigureServices(IServiceCollection services)
{
services
.AddLogging(opt =>
{
opt.AddConsole();
})
.AddTransient<Application>()
.AddSingleton<IConfig>(new Config())
.AddSingleton<GoogleAuth>()
.AddTransient<GoogleSheets>()
.AddTransient<GoogleFitness>();
}
class Application
{
private readonly ILogger<Application> logger;
private readonly IConfig config;
private readonly GoogleFitness googleFitness;
private readonly GoogleSheets googleSheets;
public Application(ILogger<Application> logger, IConfig config, GoogleFitness googleFitness, GoogleSheets googleSheets)
{
logger.LogInformation("Starting weighter...");
this.logger = logger;
this.config = config;
this.googleFitness = googleFitness;
this.googleSheets = googleSheets;
}
public async Task Run()
{
// Possibly need to bust the cache on fitness API? Look into this later
Random r = new Random();
var fuzz = r.Next(600);
DateTime from = DateTime.Now.AddDays(-7).AddSeconds(fuzz);
DateTime to = DateTime.Now;
var weightResult = await googleFitness.GetWeightsBetween(from, to);
var last = weightResult.Last();
var lastDate = new DateTimeOffset(last.DateTime).ToLocalTime();
logger.LogInformation($"Fetched {weightResult.Count()} weights on Google Fit, last one {last.Weight} on {lastDate}");
var weightToPost = GetWeightToPost(weightResult);
logger.LogInformation($"Posting {weightToPost} weight");
var dateColumnValues = await googleSheets.GetValuesFromColumn(
config.WeighInSpreadsheetId,
config.WeighInSheetName,
config.WeighInSheetDateColumn
);
var rowToPost = GetRowToPost(dateColumnValues);
await googleSheets.PostValueToCell(
weightToPost,
config.WeighInSpreadsheetId,
config.WeighInSheetName,
config.WeighInSheetEntryColumn,
rowToPost
);
}
private int GetRowToPost(IList<string> dateColumnValues)
{
var now = DateTime.Now;
var dates = dateColumnValues.Select(DateStringToDateTime).ToList();
var firstFutureDate = dates.First(date => date.Date >= now.Date);
int dateIndex = dates.IndexOf(firstFutureDate) + 1;
logger.LogInformation($"Posting to date {firstFutureDate} on row {dateIndex}");
return dateIndex;
}
private DateTime DateStringToDateTime(string dateString)
{
DateTime result = default(DateTime);
var success = DateTime.TryParseExact(dateString, "dd-MMM", CultureInfo.InvariantCulture, DateTimeStyles.None, out result);
return result;
}
private static double GetWeightToPost(IEnumerable<WeightValue> weightResult)
{
// TODO maybe a weighted average for the week or something?
return weightResult.Select(w => w.Weight).Average();
}
}
}
}