Created
September 10, 2014 23:00
-
-
Save khellang/3d9f6ab9131615d3dfc8 to your computer and use it in GitHub Desktop.
Parse all Vinmonopolet stores and save them to database using EF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open FSharp.Data | |
open System | |
open System.Diagnostics | |
open Vinmonopolet.Core | |
type Stores = | |
CsvProvider< | |
Sample = "stores.csv", | |
Separators = ";", | |
Encoding = "ISO8859-1", | |
Schema = "Gate_postnummer=string,Post_postnummer=string,Telefonnummer=string"> | |
let parseCategory (categoryString: string) = | |
Int32.Parse(categoryString.Replace("Kategori ", String.Empty)) | |
let mapEntity (row: Stores.Row) = | |
let streetAddress = | |
Address(Value = row.Gateadresse, | |
City = row.Gate_poststed.ToUpper(), | |
PostalCode = row.Gate_postnummer) | |
let postalAddress = | |
Address(Value = row.Postadresse, | |
City = row.Post_poststed.ToUpper(), | |
PostalCode = row.Post_postnummer) | |
let coordinate = | |
Coordinate(Latitude = row.GPS_breddegrad, | |
Longitude = row.GPS_lengdegrad) | |
let openingHours = | |
OpeningHours(WeekNumber = row.Ukenummer, | |
Monday = row.Apn_mandag, | |
Tuesday = row.Apn_tirsdag, | |
Wednesday = row.Apn_onsdag, | |
Thursday = row.Apn_torsdag, | |
Friday = row.Apn_fredag, | |
Saturday = row.Apn_lordag) | |
let altOpeningHours = | |
OpeningHours(WeekNumber = row.Ukenummer_neste, | |
Monday = row.Apn_neste_mandag, | |
Tuesday = row.Apn_neste_tirsdag, | |
Wednesday = row.Apn_neste_onsdag, | |
Thursday = row.Apn_neste_torsdag, | |
Friday = row.Apn_neste_fredag, | |
Saturday = row.Apn_neste_lordag) | |
Store(Name = row.Butikknavn, | |
StreetAddress = streetAddress, | |
PostalAddress = postalAddress, | |
PhoneNumber = row.Telefonnummer, | |
Category = parseCategory(row.Kategori), | |
Coordinate = coordinate, | |
OpeningHours = openingHours, | |
AlternativeOpeningHours = altOpeningHours) | |
[<EntryPoint>] | |
let main argv = | |
let stopwatch = Stopwatch.StartNew(); | |
let stores = Stores.Load("http://www.vinmonopolet.no/api/butikker") | |
use context = new VinmonopoletContext() | |
let entities = stores.Rows |> Seq.map mapEntity | |
for entity in entities do | |
context.Stores.Add(entity) |> ignore | |
let count = context.SaveChanges() | |
printfn "Saved %i stores in %dms" count stopwatch.ElapsedMilliseconds | |
0 // return an integer exit code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment