Skip to content

Instantly share code, notes, and snippets.

@ahelland
Created July 19, 2017 11:04
Show Gist options
  • Save ahelland/4d6768bddc3a3793aee9d4e26a631006 to your computer and use it in GitHub Desktop.
Save ahelland/4d6768bddc3a3793aee9d4e26a631006 to your computer and use it in GitHub Desktop.
Ruter API - Azure Function
using System;
using System.IO;
using System.Threading;
using System.Net.Http.Headers;
using Newtonsoft.Json;
using Microsoft.Azure.Management.DataLake.Store;
using Microsoft.Azure.Management.DataLake.Store.Models;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.Rest.Azure.Authentication;
public static void Run(TimerInfo input, TraceWriter log)
{
log.Info($"C# timer triggered function called.");
DataLakeStoreAccountManagementClient _adlsClient;
DataLakeStoreFileSystemManagementClient _adlsFileSystemClient;
string _adlsAccountName;
string _subId;
_adlsAccountName = "contoso_adls";
_subId = "guid";
//auth stuff
var domain = "contoso.onmicrosoft.com";
var webApp_clientId = "adls_app_id";
var clientSecret = "adls_app_secret";
var clientCredential = new ClientCredential(webApp_clientId, clientSecret);
var creds = ApplicationTokenProvider.LoginSilentAsync(domain, clientCredential).Result;
// Create client objects and set the subscription ID
_adlsClient = new DataLakeStoreAccountManagementClient(creds) { SubscriptionId = _subId };
_adlsFileSystemClient = new DataLakeStoreFileSystemManagementClient(creds);
//Look up Ruter API
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("User-Agent", "AzureFunctions");
//ID for "Jernbanetorget" Stop
var uri = "http://reisapi.ruter.no/StopVisit/GetDepartures/3010011";
HttpResponseMessage response = client.GetAsync(uri).Result;
var responseString = response.Content.ReadAsStringAsync().Result;
var timeStamp = System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
log.Info($"Time Stamp: {timeStamp}");
var fileName = "JERN_" + timeStamp + ".json";
string srcFolderPath = @"D:\home\site\";
string srcFilePath = Path.Combine(srcFolderPath, fileName);
string destFolderPath = "/Ruter/";
string destFilePath = Path.Combine(destFolderPath, fileName);
//Save to the file system of app service temporarily
System.IO.File.WriteAllText(@"D:\home\site\" + fileName, responseString);
//Delay to allow file to be written to disk before uploading
System.Threading.Thread.Sleep(2000);
log.Info($"src: {srcFilePath}, dest: {destFilePath}");
try
{
_adlsFileSystemClient.FileSystem.UploadFile(_adlsAccountName, srcFilePath, destFilePath, overwrite:true);
log.Info($"Uploaded file: {fileName}");
}
catch(Exception ex)
{
log.Info($"Upload went foo: {ex.Message}");
}
var departures = JsonConvert.DeserializeObject<List<RootObject>>(responseString);
foreach (var departure in departures)
{
var depJson = new Departure{DestinationName= departure.MonitoredVehicleJourney.DestinationName,
AimedArrivalTime = departure.MonitoredVehicleJourney.MonitoredCall.AimedArrivalTime,
ExpectedArrivalTime = departure.MonitoredVehicleJourney.MonitoredCall.ExpectedArrivalTime,
Delay = departure.MonitoredVehicleJourney.Delay,
VehicleJourneyName = departure.MonitoredVehicleJourney.VehicleJourneyName,
InCongestion = departure.MonitoredVehicleJourney.InCongestion,
DataFrameRef = departure.MonitoredVehicleJourney.FramedVehicleJourneyRef.DataFrameRef,
DatedVehicleJourneyRef = departure.MonitoredVehicleJourney.FramedVehicleJourneyRef.DatedVehicleJourneyRef};
DateTime aimed = Convert.ToDateTime(departure.MonitoredVehicleJourney.MonitoredCall.AimedArrivalTime);
DateTime expected = Convert.ToDateTime(departure.MonitoredVehicleJourney.MonitoredCall.ExpectedArrivalTime);
TimeSpan delay = expected - aimed;
depJson.Delay = delay.ToString();
log.Info($"Delay (Ruter API): {departure.MonitoredVehicleJourney.Delay}, Delay (Calculated): {depJson.Delay}");
var dep = JsonConvert.SerializeObject(depJson);
fileName = $"{depJson.DatedVehicleJourneyRef}_{depJson.DataFrameRef}.json";
//Save to the file system of app service temporarily
System.IO.File.WriteAllText(@"D:\home\site\" + fileName, dep);
//Delay to allow file to be written to disk before uploading
System.Threading.Thread.Sleep(2000);
destFolderPath = "/Ruter/departures/";
srcFilePath = Path.Combine(srcFolderPath, fileName);
destFilePath = Path.Combine(destFolderPath, fileName);
try
{
_adlsFileSystemClient.FileSystem.UploadFile(_adlsAccountName, srcFilePath, destFilePath, overwrite:true);
log.Info($"Uploaded file: {fileName}");
}
catch(Exception ex)
{
log.Info($"Upload went foo: {ex.Message}");
}
}
}
}
public class Departure
{
public string DataFrameRef { get; set; }
public string DatedVehicleJourneyRef { get; set; }
public string DestinationName { get; set; }
public string AimedArrivalTime { get; set; }
public string ExpectedArrivalTime { get; set; }
public bool InCongestion { get; set; }
public string Delay { get; set; }
public string VehicleJourneyName { get; set; }
}
public class FramedVehicleJourneyRef
{
public string DataFrameRef { get; set; }
public string DatedVehicleJourneyRef { get; set; }
}
public class TrainBlockPart
{
public int NumberOfBlockParts { get; set; }
}
public class MonitoredCall
{
public int VisitNumber { get; set; }
public bool VehicleAtStop { get; set; }
public string DestinationDisplay { get; set; }
public string AimedArrivalTime { get; set; }
public string ExpectedArrivalTime { get; set; }
public string AimedDepartureTime { get; set; }
public string ExpectedDepartureTime { get; set; }
public string DeparturePlatformName { get; set; }
}
public class MonitoredVehicleJourney
{
public string LineRef { get; set; }
public string DirectionRef { get; set; }
public FramedVehicleJourneyRef FramedVehicleJourneyRef { get; set; }
public string PublishedLineName { get; set; }
public string DirectionName { get; set; }
public string OperatorRef { get; set; }
public string OriginName { get; set; }
public string OriginRef { get; set; }
public string DestinationRef { get; set; }
public string DestinationName { get; set; }
public string OriginAimedDepartureTime { get; set; }
public string DestinationAimedArrivalTime { get; set; }
public bool Monitored { get; set; }
public bool InCongestion { get; set; }
public string Delay { get; set; }
public TrainBlockPart TrainBlockPart { get; set; }
public string BlockRef { get; set; }
public string VehicleRef { get; set; }
public int VehicleMode { get; set; }
public string VehicleJourneyName { get; set; }
public MonitoredCall MonitoredCall { get; set; }
public string VehicleFeatureRef { get; set; }
}
public class OccupancyData
{
public bool OccupancyAvailable { get; set; }
public int OccupancyPercentage { get; set; }
}
public class Deviation
{
public int ID { get; set; }
public string Header { get; set; }
}
public class Extensions
{
public bool IsHub { get; set; }
public OccupancyData OccupancyData { get; set; }
public List<Deviation> Deviations { get; set; }
public string LineColour { get; set; }
}
public class RootObject
{
public string RecordedAtTime { get; set; }
public string MonitoringRef { get; set; }
public MonitoredVehicleJourney MonitoredVehicleJourney { get; set; }
public Extensions Extensions { get; set; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment