Esempio n. 1
0
        public static async void GetLocationName(string street, string municipality)
        {
            string url = "https://api.vasttrafik.se/bin/rest.exe/v2/location.name?input=" + street + "%20" + municipality + "&format=json";

            //string url = "https://api.vasttrafik.se/bin/rest.exe/v2/location.name?input=sanneg%C3%A5rdshamnen&format=json";
            using (HttpResponseMessage response = await ApiHelperVasttrafik.ApiClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    string fromVastTrafik = await response.Content.ReadAsStringAsync();

                    JObject jsonObject = JObject.Parse(fromVastTrafik);

                    var locationName = jsonObject.SelectToken("LocationList").SelectToken("StopLocation")[0];

                    MongoCRUD db = new MongoCRUD("admin");

                    VastTrafikModelLocation model = new VastTrafikModelLocation();

                    model.Name = locationName[key : "name"].ToString();
                    model.Lon  = locationName[key : "lon"].ToObject <float>();
                    model.Lat  = locationName[key : "lat"].ToObject <float>();



                    db.InsertRecord(Vasttrafik.locations, model);
                }
                else
                {
                    Console.Write("Fel vid kontakt med API");
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            MongoCRUD db = new MongoCRUD("AddressBook");

            // startar en webclient
            ApiHelper.InitializeClient();
            string apikey = "'6bb34852b5b140e69f3eec606ea04220'";

            // sätter query i en string
            string quearyString = "<REQUEST>" +
                                  // Use your valid authenticationkey
                                  "<LOGIN authenticationkey=" + apikey + "/>" +
                                  "<QUERY objecttype='TrainStation' schemaversion='1'>" +
                                  "<FILTER>" +
                                  "<IN name='CountyNo' value='14'/>" +
                                  "</FILTER>" +
                                  "<INCLUDE>AdvertisedLocationName</INCLUDE>" +
                                  "<INCLUDE>LocationSignature</INCLUDE>" +
                                  "<INCLUDE>LocationInformationText</INCLUDE>" +
                                  "</QUERY>" +
                                  "</REQUEST>";

            // sätter query i formatet för http body request
            StringContent query = new StringContent(quearyString);


            //Console.WriteLine(TrafikverketProcessor.PostTrafikVerket(query));
            db.InsertRecord("test", TrafikverketProcessor.PostTrafikVerket(query));

            Console.ReadLine();
        }
        public static async void PostTrainStation(HttpContent data)

        {
            string url = "https://api.trafikinfo.trafikverket.se/v2/data.json";

            int errorCountyNo = 0;

            using (HttpResponseMessage response = await ApiHelper.ApiClient.PostAsync(url, data))

            {
                if (response.IsSuccessStatusCode)
                {
                    string fromTrafikverket = await response.Content.ReadAsStringAsync();

                    JObject jsonObject = JObject.Parse(fromTrafikverket);


                    // json objectet från trafikverket har strukturen ("RESPONSE") ("RESULT") [ ("TrainStation") ]

                    var trainStations = jsonObject.SelectToken("RESPONSE").SelectToken("RESULT")[0].SelectToken("TrainStation");

                    MongoCRUD db = new MongoCRUD("admin");
                    foreach (var message in trainStations)
                    {
                        TrafikverketTrainStationModel model = new TrafikverketTrainStationModel();


                        model.AdvertisedLocationName = message[key : "AdvertisedLocationName"].ToString();
                        model.LocationSignature      = message[key : "LocationSignature"].ToString();



                        try
                        {
                            model.CountyNo = message[key : "CountyNo"][0].ToString();
                        }
                        catch
                        {
                            errorCountyNo += 1;
                        }
                        JToken geo         = message[key : "Geometry"];
                        JToken wgs84       = geo[key : "WGS84"];
                        string pointstring = wgs84.ToString();


                        // Regex för att ta bort "POINT" från fältet och sedan "( )"
                        pointstring = Regex.Replace(pointstring, "POINT", "");
                        pointstring = Regex.Replace(pointstring, @"[()]", "");

                        // delar stringen på alla mellanrum. !OBS fältet startar med mellanrum vilket skapar 3! delar
                        string[] substring = Regex.Split(pointstring, " ");

                        // plockar upp och delar ut cordinaterna, mest för läsbarhet och att inte råka skicka med mellanrummet
                        string[] coords = { substring[1], substring[2] };
                        model.Longitude = float.Parse(coords[0], CultureInfo.InvariantCulture);
                        model.Latitude  = float.Parse(coords[1], CultureInfo.InvariantCulture);



                        // Fortsättning på kod, läs upp från databasen för att kolla efter dubbleter i DB



                        db.InsertRecord(Trafikverket.station, model);
                    }

                    Console.WriteLine("Failed to get " + errorCountyNo + " number of county numbers from API");
                }
                else
                {
                    Console.WriteLine("!!FEL VID KONTAKT MED API!!");
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }