-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.cs
121 lines (111 loc) · 4.42 KB
/
Function.cs
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
120
121
using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Threading.Tasks;
using System.Linq;
using System.IO;
using Azure.Storage.Blobs;
using System.Text;
using Azure.Storage.Blobs.Specialized;
using Azure.Storage.Blobs.Models;
namespace TripItExport
{
public class Function
{
[FunctionName("TripItExport")]
public async Task Run(
[TimerTrigger("58 3 * * *")]TimerInfo myTimer,
ILogger log,
[Blob("tripitexport//flights.json", FileAccess.ReadWrite)] BlockBlobClient flightsJson,
[Blob("tripitexport//flights.geojson", FileAccess.ReadWrite)] BlockBlobClient flightsGeoJson
)
{
log.LogInformation($"TripItExport started at: {DateTime.Now}");
string? access_token = Environment.GetEnvironmentVariable("access_token");
if (access_token == null)
{
log.LogError("access_token settings not set!");
return;
}
string? access_secret = Environment.GetEnvironmentVariable("access_secret");
if (access_secret == null)
{
log.LogError("access_secret settings not set!");
return;
}
string? client_token = Environment.GetEnvironmentVariable("client_token");
if (client_token == null)
{
log.LogError("client_token settings not set!");
return;
}
string? client_secret = Environment.GetEnvironmentVariable("client_secret");
if (client_secret == null)
{
log.LogError("client_secret settings not set!");
return;
}
var client = new Client(
client_token,
client_secret,
access_token,
access_secret);
var userUUID = await client.GetUserUUID();
if (userUUID == null)
{
log.LogError("User UUID not returned!");
return;
}
log.LogInformation($"User UUID {userUUID} receieved.");
var flights = await client.GetFlights();
if (flights is null)
{
log.LogError("Flights not returned!");
return;
}
log.LogInformation($"{flights.Length} flights received.");
var geojson = new
{
type = "FeatureCollection",
features = flights.Select(flight => new
{
geometry = new
{
coordinates = new[]
{
new[]
{
new[]
{
flight.Origin.Longitude,
flight.Origin.Latitude,
},
new[]
{
flight.Destination.Longitude,
flight.Destination.Latitude,
},
},
},
type = "MultiLineString",
},
properties = new
{
name = $"{flight.Origin.Code} - {flight.Destination.Code}"
},
type = "Feature",
})
};
Stream outputStream = new MemoryStream();
JsonSerializer.Serialize(outputStream, geojson, new JsonSerializerOptions() { WriteIndented = true });
outputStream.Position = 0;
await flightsGeoJson.UploadAsync(outputStream, new BlobHttpHeaders { ContentType = "application/json" });
outputStream = new MemoryStream();
JsonSerializer.Serialize(outputStream, flights, new JsonSerializerOptions() { WriteIndented = true });
outputStream.Position = 0;
await flightsJson.UploadAsync(outputStream, new BlobHttpHeaders { ContentType = "application/json" });
log.LogInformation($"TripItExport finished at: {DateTime.Now}");
}
}
}