SHARE
TWEET

Train Updater

a guest Feb 17th, 2017 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Diagnostics;
  8. using System.Net;
  9.  
  10. namespace Train_Updater
  11. {
  12.     class Program
  13.     {
  14.  
  15.         const string TrainCodesFilepath = "traincodes.txt";
  16.         const char Limiter = '-';
  17.         const string DepartureTimeFind = "\"departureTime\":\"";
  18.         const string ArrivalTimeFind = "\"arrivalTime\":\"";
  19.         const int TimeSize = 5;
  20.  
  21.         public static List<string> GetTimes(string SourceCode, string FindingString, int TimeSize)
  22.         {
  23.             List<string> Times = new List<string>();            //Creates a new list to add strings to
  24.             int Start = 0;                                      //Defines the start of the substring
  25.  
  26.             for (int x = 0; x < 5; x++)
  27.             {
  28.                 Start = SourceCode.IndexOf(FindingString) + FindingString.Length;         //Finds the index of the first instance
  29.                 Times.Add(SourceCode.Substring(Start, TimeSize));                         //Adds this to the list of times
  30.                 SourceCode = SourceCode.Substring(Start + TimeSize);                      //Redefines the current code string to remove the old instance
  31.             }
  32.  
  33.             return Times;
  34.         }
  35.  
  36.         public static List<string> GetDurations(List<string> DepartureTimes, List<string> ArrivalTimes)
  37.         {
  38.  
  39.             List<string> RList = new List<string>();        //Creates a new list to return at the end of the function
  40.  
  41.             for (int x = 0; x < DepartureTimes.Count; x++)
  42.             {
  43.                 int CurrentDepartureTime = Convert.ToInt32(DepartureTimes[x].Split(':')[0]) * 60 +
  44.                                           (Convert.ToInt32(DepartureTimes[x].Split(':')[1]));               //Converts the departure and arrival times into minutes from strings
  45.                 int CurrentArrivalTime = Convert.ToInt32(ArrivalTimes[x].Split(':')[0]) * 60 +
  46.                                         (Convert.ToInt32(ArrivalTimes[x].Split(':')[1]));
  47.  
  48.                 int TimeTaken = CurrentArrivalTime - CurrentDepartureTime;                  //Works out the difference in times between the two
  49.  
  50.                 if (TimeTaken < 60)
  51.                 {
  52.                     RList.Add(Convert.ToString(TimeTaken) + " min");
  53.                 }
  54.                 else
  55.                 {
  56.                     int Hours = TimeTaken / 60;
  57.                     int Remaining = TimeTaken - (Hours * 60);
  58.  
  59.                     RList.Add(Convert.ToString(Hours) + " h " + Convert.ToString(Remaining) + " min");
  60.                 }
  61.             }
  62.  
  63.             return RList;
  64.  
  65.         }
  66.  
  67.         public static string CreateUrl(string StartCode, string EndCode, string Date, string Time, string ArrDep)
  68.         {
  69.             return "http://ojp.nationalrail.co.uk/service/timesandfares/" + StartCode + "/" + EndCode + "/" + Date + "/" + Time + "/" + ArrDep;
  70.         }
  71.  
  72.         public static Dictionary<string, string> GetCodes(string Filepath, char Limiter)
  73.         {
  74.             Dictionary<string, string> RDict = new Dictionary<string, string>();
  75.             StreamReader sr = new StreamReader(Filepath);
  76.             string[] SplitString;
  77.  
  78.             string CurrentLine = sr.ReadLine();
  79.  
  80.             while (CurrentLine != null)
  81.             {
  82.                 SplitString = CurrentLine.ToLower().Split(Limiter);
  83.  
  84.                 if (!(RDict.ContainsKey(SplitString[0])))
  85.                 {
  86.                     RDict.Add(SplitString[0], SplitString[1]);
  87.                 }
  88.  
  89.                 CurrentLine = sr.ReadLine();
  90.             }
  91.  
  92.             sr.Close();
  93.             return RDict;
  94.         }
  95.  
  96.         static void Main(string[] args)
  97.         {
  98.  
  99.             Dictionary<string, string> StationCodes = GetCodes(TrainCodesFilepath, Limiter);
  100.  
  101.             /*Console.Write("What station are you starting at?: ");
  102.             string StartCode = StationCodes[Console.ReadLine().ToLower()];
  103.  
  104.             Console.Write("What station are you ending at?: ");
  105.             string EndCode = StationCodes[Console.ReadLine().ToLower()];
  106.  
  107.             Console.Write("What date are you leaving? (Today, tomorrow or a date): ");
  108.             string Date = Console.ReadLine().ToLower().Replace("/", "");
  109.  
  110.             Console.Write("What time are you needing?: ");
  111.             string Time = Console.ReadLine().Replace(":", "");
  112.  
  113.             Console.Write("Do you want to depart or arrive at that time?: ");
  114.             string ArrDep = Console.ReadLine().ToLower().Substring(0, 3);*/
  115.  
  116.             Console.Clear();
  117.  
  118.             string StartCode = "eps", EndCode = "wdo", Date = "today", Time = "1500", ArrDep = "dep";
  119.             //string StartCode = "gld", EndCode = "wat", Date = "tomorrow", Time = "0930", ArrDep = "dep";
  120.  
  121.             string TrainTimeUrl = CreateUrl(StartCode, EndCode, Date, Time, ArrDep);
  122.             string SourceCode = new WebClient().DownloadString(TrainTimeUrl);
  123.  
  124.             List<string> DepartureTimes = GetTimes(SourceCode, DepartureTimeFind, TimeSize);
  125.             List<string> ArrivalTimes = GetTimes(SourceCode, ArrivalTimeFind, TimeSize);
  126.             List<string> JourneyMinutes = GetDurations(DepartureTimes, ArrivalTimes);
  127.  
  128.             Console.WriteLine("Departing\tArriving\tDuration");
  129.  
  130.             for (int x = 0; x < DepartureTimes.Count; x++)
  131.             {
  132.                 Console.WriteLine("{0}\t\t{1}\t\t{2}", DepartureTimes[x], ArrivalTimes[x], JourneyMinutes[x]);
  133.             }
  134.  
  135.             Console.ReadKey();
  136.  
  137.         }
  138.     }
  139. }
RAW Paste Data
Top