using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; using Finsel.AzureCommands; using System.Xml; namespace MaxMindIPGeoLocation { public partial class Form1 : Form { public Form1() { InitializeComponent(); txtAccount.Text = MaxMindIPGeoLocation.Properties.Settings.Default.Account; txtEndpoint.Text = MaxMindIPGeoLocation.Properties.Settings.Default.Endpoint; txtSharedKey.Text = MaxMindIPGeoLocation.Properties.Settings.Default.SharedKey; } private void btnFindBlocks_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.ShowDialog(); txtBlocksFile.Text = ofd.FileName; } private void btnFindCity_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.ShowDialog(); txtCityFile.Text = ofd.FileName; } private void btnLoad_Click(object sender, EventArgs e) { Int64 iCounter = 0; string regexCSVSplit = string.Empty; regexCSVSplit = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))"; regexCSVSplit = @"([^\\x22\\]*(:?:\\.[^\\x22\\]*)*)\\x22,?|([^,]+),?|,"; System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(regexCSVSplit); string tableName = "MaxMindData"; string cityPartition = "Cities"; string blockPartition = "IPBlocks"; this.Cursor = Cursors.WaitCursor; System.IO.StreamReader file = new System.IO.StreamReader(txtCityFile.Text); string inputString = string.Empty; AzureTableStorage ats = new AzureTableStorage(txtAccount.Text, txtEndpoint.Text, txtSharedKey.Text, "SharedKey"); StringBuilder sb = new StringBuilder(); sb.AppendFormat(@" "); int entityCount = 0; ats.Tables(cmdType.post, tableName); inputString = file.ReadLine(); // read copyright inputString = file.ReadLine(); // read column headers azureHelper ah = new azureHelper(ats.auth); while ((inputString = file.ReadLine()) != null) { iCounter++; string[] dataSet = r.Split(inputString); for (int i = 0; i < dataSet.GetUpperBound(0); i++) dataSet[i] = dataSet[i].ToString().Replace("\"", ""); if ((iCounter % 100) == 0) { Notify(string.Format("Processing location {0}: {1}", iCounter, DateTime.Now.ToString("O"))); } //locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode // string ds = string.Format(locationTemplate, tableName, cityPartition, dataSet[0], inputString); sb.AppendFormat(locationTemplate, dataSet); entityCount++; if (entityCount >= 99) { sb.Append(""); // Notify(string.Format("Transmitting Group of transactions, processed through {0}",iCounter)); ah.entityGroupTransaction(cmdType.post , tableName, sb.ToString()); sb = new StringBuilder(); sb.AppendFormat(@" "); entityCount = 0; } if ((iCounter % 100)==0) Application.DoEvents(); } if (entityCount > 0) { Notify("Transmitting Final Group of transactions"); sb.Append(""); ah.entityGroupTransaction(cmdType.post, tableName, sb.ToString()); sb = new StringBuilder(); entityCount = 0; } file.Close(); sb = new StringBuilder(); iCounter = 0; sb.AppendFormat(@" "); entityCount = 0; file = new System.IO.StreamReader(txtBlocksFile.Text); inputString = string.Empty; inputString = file.ReadLine(); // read copyright inputString = file.ReadLine(); // read column headers Int64 ipLoadRecord = 0; while ((inputString = file.ReadLine()) != null) { iCounter++; string[] dataSet = r.Split(inputString); for (int i = 0; i < dataSet.GetUpperBound(0); i++) dataSet[i] = dataSet[i].ToString().Replace("\"", ""); if ((iCounter % 100) == 0) { Notify(string.Format("Processing location {0}: {1}", iCounter, DateTime.Now.ToString("O"))); } sb.AppendFormat(ipTemplate, dataSet); entityCount++; if (entityCount >= 500) { sb.Append(""); Notify(string.Format("Transmitting Group of transactions, processed through {0}",iCounter)); ah.entityGroupTransaction(cmdType.post, tableName, sb.ToString()); sb = new StringBuilder(); sb.AppendFormat(@" "); entityCount = 0; } if ((iCounter % 100) == 0) { Notify(string.Format("Processed through {0}", iCounter)); Application.DoEvents(); } } if (entityCount > 0) { Notify("Transmitting Final Group of transactions"); sb.Append(""); ah.entityGroupTransaction(cmdType.post, tableName, sb.ToString()); sb = new StringBuilder(); entityCount = 0; } file.Close(); this.Cursor = Cursors.Default; } private void Notify(string message) { toolStripStatusLabel1.Text = message; Application.DoEvents(); } string ipTemplate = @" IPBlocks {0} {0} {1} "; //locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode string locationTemplate = @" Cities {0} {1} {2} {3} {4} {5} {6} {7} {8} "; private void Form1_FormClosing(object sender, FormClosingEventArgs e) { MaxMindIPGeoLocation.Properties.Settings.Default.Account = txtAccount.Text; MaxMindIPGeoLocation.Properties.Settings.Default.Endpoint = txtEndpoint.Text; MaxMindIPGeoLocation.Properties.Settings.Default.SharedKey = txtSharedKey.Text; MaxMindIPGeoLocation.Properties.Settings.Default.Save(); } } }