Tryng to get the list of tables to download.
This commit is contained in:
parent
a0f227438b
commit
81b1779c1d
BIN
ANEEL.accdb
BIN
ANEEL.accdb
Binary file not shown.
196
Program.cs
196
Program.cs
@ -12,27 +12,61 @@ using System.Data.SqlClient;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
using Windows.System;
|
||||
using System.Security.Cryptography;
|
||||
using System.Reflection.PortableExecutable;
|
||||
using System.Xml.Linq;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static async Task Main(string[] args)
|
||||
private static async Task Main(string[] args)
|
||||
{
|
||||
string AneelURL = "https://dadosabertos.aneel.gov.br/api/3/action/datastore_search?resource_id=fcf2906c-7c32-4b9b-a637-054e7a5234f4&limit=32000";
|
||||
//string AccessPath = @"C:\Users\contratos\Documents\Giuliano\Code\TarifasANEEL\ANEEL.accdb";
|
||||
|
||||
Rootobject? rootObject = await PullDataAsync(AneelURL);
|
||||
string AneelURL = "https://dadosabertos.aneel.gov.br/api/3/action/datastore_search";
|
||||
string AccessPath = @"C:\Users\contratos\Documents\Giuliano\Code\TarifasANEEL\ANEEL.accdb";
|
||||
int offset = 0;
|
||||
int lastID;
|
||||
|
||||
Dictionary<string, List<object>>? WebDatas = GetResources(AccessPath, "SELECT * FROM tblResourceComponentesTarifarias") !;
|
||||
|
||||
foreach(object resource in WebDatas["resource_id"].ToList())
|
||||
{
|
||||
if (resource != null)
|
||||
{
|
||||
Console.WriteLine(resource.ToString());
|
||||
};
|
||||
}
|
||||
using (OleDbConnection connection = new OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={AccessPath};Persist Security Info=False;"))
|
||||
{
|
||||
connection.Open();
|
||||
using (OleDbCommand command = new OleDbCommand($"SELECT Max([_id]) FROM TmpTable WHERE [resource_id] = \"{WebDatas["resource_id"]}\"", connection))
|
||||
{
|
||||
lastID = (int)command.ExecuteScalar()!;
|
||||
}
|
||||
}
|
||||
|
||||
string webString = $@"{AneelURL}?";
|
||||
|
||||
foreach (var webData in WebDatas)
|
||||
{
|
||||
webString = $@"{webString}{webData.Key}={webData.Value}&";
|
||||
}
|
||||
|
||||
|
||||
|
||||
Rootobject? rootObject = await PullDataAsync($@"{webString}offset={offset.ToString()}");
|
||||
|
||||
List<Record>? records = rootObject?.result?.records?.ToList();
|
||||
List<Record>? tempRecords = rootObject?.result?.records?.ToList();
|
||||
Console.Clear();
|
||||
Console.WriteLine(rootObject?.result?.records?.Last()._id);
|
||||
|
||||
if (records != null && (records.Last()._id != rootObject?.result.total))
|
||||
if (records != null && (records.Last()._id != rootObject?.result?.total))
|
||||
{
|
||||
while (rootObject?.result.total != records.Last()._id)
|
||||
while (rootObject?.result?.total != records.Last()._id)
|
||||
//while (records.Last()._id != 32000)
|
||||
{
|
||||
rootObject = await PullDataAsync(AneelURL + "&offset=" + records.Last()._id);
|
||||
offset = records.Last()._id;
|
||||
rootObject = await PullDataAsync($@"{webString}offset={offset.ToString()}");
|
||||
Console.WriteLine(rootObject?.result?.records?.Last()._id);
|
||||
|
||||
tempRecords = rootObject?.result?.records?.ToList();
|
||||
@ -44,11 +78,43 @@ public class Program
|
||||
}
|
||||
if (records != null)
|
||||
{
|
||||
UpdateData(records, "tblTarifas");
|
||||
//UpdateData(records, "tblTarifas");
|
||||
AdicionarDadosAoBanco(records,AccessPath);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static Dictionary<string, List<object>>? GetResources(string AccessPath, string sSQL)
|
||||
{
|
||||
Dictionary<string, List<object>>? Temp = new Dictionary<string, List<object>>();
|
||||
try
|
||||
{
|
||||
using (OleDbConnection connection = new OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={AccessPath};Persist Security Info=False;"))
|
||||
{
|
||||
using (OleDbCommand command = new OleDbCommand(sSQL, connection))
|
||||
{
|
||||
connection.Open();
|
||||
using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
|
||||
{
|
||||
DataTable dt = new DataTable();
|
||||
adapter.Fill(dt);
|
||||
foreach(DataColumn dc in dt.Columns)
|
||||
{
|
||||
var list = dt.Rows.OfType<DataRow>().Select(dr => dr.Field<object>(dc.ColumnName)).ToList();
|
||||
Temp.Add(dc.ColumnName, list !);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Temp;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task<Rootobject?> PullDataAsync(string sURL)
|
||||
{
|
||||
var client = new HttpClient();
|
||||
@ -94,14 +160,48 @@ public class Program
|
||||
conn.Open();
|
||||
|
||||
//Creating temp table on database
|
||||
command.CommandText = "CREATE TABLE TmpTable (_id INT, DatGeracaoConjuntoDados TEXT, DscREH TEXT, SigAgente TEXT, NumCNPJDistribuidora TEXT, DatInicioVigencia TEXT, DatFimVigencia TEXT, DscBaseTarifaria TEXT, DscSubGrupo TEXT, DscModalidadeTarifaria TEXT, DscClasse TEXT, DscSubClasse TEXT, DscDetalhe TEXT, NomPostoTarifario TEXT, DscUnidadeTerciaria TEXT, SigAgenteAcessante TEXT, VlrTUSD TEXT, VlrTE TEXT)";
|
||||
command.CommandText = @"CREATE TABLE tblTarifas (_id INT, DatGeracaoConjuntoDados TEXT,
|
||||
DscResolucaoHomologatoria TEXT,
|
||||
SigNomeAgente TEXT,
|
||||
NumCPFCNPJ TEXT,
|
||||
DatInicioVigencia TEXT,
|
||||
DatFimVigencia TEXT,
|
||||
DscBaseTarifaria TEXT,
|
||||
DscSubGrupoTarifario TEXT,
|
||||
DscModalidadeTarifaria TEXT,
|
||||
DscClasseConsumidor TEXT,
|
||||
DscSubClasseConsumidor TEXT,
|
||||
DscDetalheConsumidor TEXT,
|
||||
DscPostoTarifario TEXT,
|
||||
DscUnidade TEXT,
|
||||
SigNomeAgenteAcessante TEXT,
|
||||
DscComponenteTarifario TEXT,
|
||||
VlrComponenteTarifario TEXT";
|
||||
command.ExecuteNonQuery();
|
||||
//command.CommandText = @"CREATE TABLE TmpTable (_id INT, DatGeracaoConjuntoDados TEXT,
|
||||
// DscResolucaoHomologatoria TEXT,
|
||||
// SigNomeAgente TEXT,
|
||||
// NumCPFCNPJ TEXT,
|
||||
// DatInicioVigencia TEXT,
|
||||
// DatFimVigencia TEXT,
|
||||
// DscBaseTarifaria TEXT,
|
||||
// DscSubGrupoTarifario TEXT,
|
||||
// DscModalidadeTarifaria TEXT,
|
||||
// DscClasseConsumidor TEXT,
|
||||
// DscSubClasseConsumidor TEXT,
|
||||
// DscDetalheConsumidor TEXT,
|
||||
// DscPostoTarifario TEXT,
|
||||
// DscUnidade TEXT,
|
||||
// SigNomeAgenteAcessante TEXT,
|
||||
// DscComponenteTarifario TEXT,
|
||||
// VlrComponenteTarifario TEXT";
|
||||
//command.ExecuteNonQuery();
|
||||
|
||||
//Bulk insert into temp table
|
||||
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conn))
|
||||
{
|
||||
bulkcopy.BulkCopyTimeout = 660;
|
||||
bulkcopy.DestinationTableName = "TmpTable";
|
||||
bulkcopy.DestinationTableName = "tblTarifas";
|
||||
bulkcopy.WriteToServer(dt);
|
||||
bulkcopy.Close();
|
||||
}
|
||||
@ -110,22 +210,22 @@ public class Program
|
||||
command.CommandTimeout = 300;
|
||||
command.CommandText = @"UPDATE P SET
|
||||
P.[DatGeracaoConjuntoDados] = T.[DatGeracaoConjuntoDados],
|
||||
P.[DscREH] = T.[DscREH],
|
||||
P.[SigAgente] = T.[SigAgente],
|
||||
P.[NumCNPJDistribuidora] = T.[NumCNPJDistribuidora],
|
||||
P.[DscResolucaoHomologatoria] = T.[DscResolucaoHomologatoria],
|
||||
P.[SigNomeAgente] = T.[SigNomeAgente],
|
||||
P.[NumCPFCNPJ] = T.[NumCPFCNPJ],
|
||||
P.[DatInicioVigencia] = T.[DatInicioVigencia],
|
||||
P.[DatFimVigencia] = T.[DatFimVigencia],
|
||||
P.[DscBaseTarifaria] = T.[DscBaseTarifaria],
|
||||
P.[DscSubGrupo] = T.[DscSubGrupo],
|
||||
P.[DscSubGrupoTarifario] = T.[DscSubGrupoTarifario],
|
||||
P.[DscModalidadeTarifaria] = T.[DscModalidadeTarifaria],
|
||||
P.[DscClasse] = T.[DscClasse],
|
||||
P.[DscSubClasse] = T.[DscSubClasse],
|
||||
P.[DscDetalhe] = T.[DscDetalhe],
|
||||
P.[NomPostoTarifario] = T.[NomPostoTarifario],
|
||||
P.[DscUnidadeTerciaria] = T.[DscUnidadeTerciaria],
|
||||
P.[SigAgenteAcessante] = T.[SigAgenteAcessante],
|
||||
P.[VlrTUSD] = T.[VlrTUSD],
|
||||
P.[VlrTE] = T.[VlrTE]
|
||||
P.[DscClasseConsumidor] = T.[DscClasseConsumidor],
|
||||
P.[DscSubClasseConsumidor] = T.[DscSubClasseConsumidor],
|
||||
P.[DscDetalheConsumidor] = T.[DscDetalheConsumidor],
|
||||
P.[DscPostoTarifario] = T.[DscPostoTarifario],
|
||||
P.[DscUnidade] = T.[DscUnidade],
|
||||
P.[SigNomeAgenteAcessante] = T.[SigNomeAgenteAcessante],
|
||||
P.[DscComponenteTarifario] = T.[DscComponenteTarifario],
|
||||
P.[VlrComponenteTarifario] = T.[VlrComponenteTarifario]
|
||||
FROM[TmpTable] AS P INNER JOIN TmpTable AS T ON P.[_id] = T.[_id]; DROP TABLE TmpTable; ";
|
||||
command.ExecuteNonQuery();
|
||||
}
|
||||
@ -165,23 +265,22 @@ public class Program
|
||||
// Adicionar parâmetros
|
||||
command.Parameters.AddWithValue("@_id", record._id);
|
||||
command.Parameters.AddWithValue("@DatGeracaoConjuntoDados", record.DatGeracaoConjuntoDados);
|
||||
command.Parameters.AddWithValue("@DscREH", record.DscREH);
|
||||
command.Parameters.AddWithValue("@SigAgente", record.SigAgente);
|
||||
command.Parameters.AddWithValue("@NumCNPJDistribuidora", record.NumCNPJDistribuidora);
|
||||
command.Parameters.AddWithValue("@DscResolucaoHomologatoria", record.DscResolucaoHomologatoria);
|
||||
command.Parameters.AddWithValue("@SigNomeAgente", record.SigNomeAgente);
|
||||
command.Parameters.AddWithValue("@NumCPFCNPJ", record.NumCPFCNPJ);
|
||||
command.Parameters.AddWithValue("@DatInicioVigencia", record.DatInicioVigencia);
|
||||
command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia);
|
||||
command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia);
|
||||
command.Parameters.AddWithValue("@DscBaseTarifaria", record.DscBaseTarifaria);
|
||||
command.Parameters.AddWithValue("@DscSubGrupo", record.DscSubGrupo);
|
||||
command.Parameters.AddWithValue("@DscSubGrupoTarifario", record.DscSubGrupoTarifario);
|
||||
command.Parameters.AddWithValue("@DscModalidadeTarifaria", record.DscModalidadeTarifaria);
|
||||
command.Parameters.AddWithValue("@DscClasse", record.DscClasse);
|
||||
command.Parameters.AddWithValue("@DscSubClasse", record.DscSubClasse);
|
||||
command.Parameters.AddWithValue("@DscDetalhe", record.DscDetalhe);
|
||||
command.Parameters.AddWithValue("@NomPostoTarifario", record.NomPostoTarifario);
|
||||
command.Parameters.AddWithValue("@DscUnidadeTerciaria", record.DscUnidadeTerciaria);
|
||||
command.Parameters.AddWithValue("@SigAgenteAcessante", record.SigAgenteAcessante);
|
||||
command.Parameters.AddWithValue("@VlrTUSD", record.VlrTUSD);
|
||||
command.Parameters.AddWithValue("@VlrTE", record.VlrTE);
|
||||
command.Parameters.AddWithValue("@DscClasseConsumidor", record.DscClasseConsumidor);
|
||||
command.Parameters.AddWithValue("@DscSubClasseConsumidor", record.DscSubClasseConsumidor);
|
||||
command.Parameters.AddWithValue("@DscDetalheConsumidor", record.DscDetalheConsumidor);
|
||||
command.Parameters.AddWithValue("@DscPostoTarifario", record.DscPostoTarifario);
|
||||
command.Parameters.AddWithValue("@DscUnidade", record.DscUnidade);
|
||||
command.Parameters.AddWithValue("@SigNomeAgenteAcessante", record.SigNomeAgenteAcessante);
|
||||
command.Parameters.AddWithValue("@DscComponenteTarifario", record.DscComponenteTarifario);
|
||||
command.Parameters.AddWithValue("@VlrComponenteTarifario", record.VlrComponenteTarifario);
|
||||
|
||||
command.ExecuteNonQuery();
|
||||
Console.WriteLine(record._id);
|
||||
@ -241,23 +340,22 @@ public class Program
|
||||
// Adicionar parâmetros
|
||||
command.Parameters.AddWithValue("@_id", record._id);
|
||||
command.Parameters.AddWithValue("@DatGeracaoConjuntoDados", record.DatGeracaoConjuntoDados);
|
||||
command.Parameters.AddWithValue("@DscREH", record.DscREH);
|
||||
command.Parameters.AddWithValue("@SigAgente", record.SigAgente);
|
||||
command.Parameters.AddWithValue("@NumCNPJDistribuidora", record.NumCNPJDistribuidora);
|
||||
command.Parameters.AddWithValue("@DscResolucaoHomologatoria", record.DscResolucaoHomologatoria);
|
||||
command.Parameters.AddWithValue("@SigNomeAgente", record.SigNomeAgente);
|
||||
command.Parameters.AddWithValue("@NumCPFCNPJ", record.NumCPFCNPJ);
|
||||
command.Parameters.AddWithValue("@DatInicioVigencia", record.DatInicioVigencia);
|
||||
command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia);
|
||||
command.Parameters.AddWithValue("@DatFimVigencia", record.DatFimVigencia);
|
||||
command.Parameters.AddWithValue("@DscBaseTarifaria",record.DscBaseTarifaria);
|
||||
command.Parameters.AddWithValue("@DscSubGrupo", record.DscSubGrupo);
|
||||
command.Parameters.AddWithValue("@DscBaseTarifaria", record.DscBaseTarifaria);
|
||||
command.Parameters.AddWithValue("@DscSubGrupoTarifario", record.DscSubGrupoTarifario);
|
||||
command.Parameters.AddWithValue("@DscModalidadeTarifaria", record.DscModalidadeTarifaria);
|
||||
command.Parameters.AddWithValue("@DscClasse", record.DscClasse);
|
||||
command.Parameters.AddWithValue("@DscSubClasse", record.DscSubClasse);
|
||||
command.Parameters.AddWithValue("@DscDetalhe", record.DscDetalhe);
|
||||
command.Parameters.AddWithValue("@NomPostoTarifario", record.NomPostoTarifario);
|
||||
command.Parameters.AddWithValue("@DscUnidadeTerciaria", record.DscUnidadeTerciaria);
|
||||
command.Parameters.AddWithValue("@SigAgenteAcessante", record.SigAgenteAcessante);
|
||||
command.Parameters.AddWithValue("@VlrTUSD", record.VlrTUSD);
|
||||
command.Parameters.AddWithValue("@VlrTE", record.VlrTE);
|
||||
command.Parameters.AddWithValue("@DscClasseConsumidor", record.DscClasseConsumidor);
|
||||
command.Parameters.AddWithValue("@DscSubClasseConsumidor", record.DscSubClasseConsumidor);
|
||||
command.Parameters.AddWithValue("@DscDetalheConsumidor", record.DscDetalheConsumidor);
|
||||
command.Parameters.AddWithValue("@DscPostoTarifario", record.DscPostoTarifario);
|
||||
command.Parameters.AddWithValue("@DscUnidade", record.DscUnidade);
|
||||
command.Parameters.AddWithValue("@SigNomeAgenteAcessante", record.SigNomeAgenteAcessante);
|
||||
command.Parameters.AddWithValue("@DscComponenteTarifario", record.DscComponenteTarifario);
|
||||
command.Parameters.AddWithValue("@VlrComponenteTarifario", record.VlrComponenteTarifario);
|
||||
|
||||
// Adicionar outros parâmetros conforme necessário
|
||||
|
||||
|
||||
@ -8,11 +8,12 @@ namespace TarifasANEEL
|
||||
{
|
||||
|
||||
|
||||
|
||||
public class Rootobject
|
||||
{
|
||||
public string? help { get; set; }
|
||||
public bool success { get; set; }
|
||||
public required Result result { get; set; }
|
||||
public Result? result { get; set; }
|
||||
}
|
||||
|
||||
public class Result
|
||||
@ -25,8 +26,8 @@ namespace TarifasANEEL
|
||||
public Record[]? records { get; set; }
|
||||
public Field[]? fields { get; set; }
|
||||
public _Links? _links { get; set; }
|
||||
public int total { get; set; }
|
||||
public bool total_was_estimated { get; set; }
|
||||
public int? total { get; set; }
|
||||
public bool? total_was_estimated { get; set; }
|
||||
}
|
||||
|
||||
public class _Links
|
||||
@ -39,22 +40,22 @@ namespace TarifasANEEL
|
||||
{
|
||||
public int _id { get; set; }
|
||||
public string? DatGeracaoConjuntoDados { get; set; }
|
||||
public string? DscREH { get; set; }
|
||||
public string? SigAgente { get; set; }
|
||||
public string? NumCNPJDistribuidora { get; set; }
|
||||
public string? DscResolucaoHomologatoria { get; set; }
|
||||
public string? SigNomeAgente { get; set; }
|
||||
public string? NumCPFCNPJ { get; set; }
|
||||
public string? DatInicioVigencia { get; set; }
|
||||
public string? DatFimVigencia { get; set; }
|
||||
public string? DscBaseTarifaria { get; set; }
|
||||
public string? DscSubGrupo { get; set; }
|
||||
public string? DscSubGrupoTarifario { get; set; }
|
||||
public string? DscModalidadeTarifaria { get; set; }
|
||||
public string? DscClasse { get; set; }
|
||||
public string? DscSubClasse { get; set; }
|
||||
public string? DscDetalhe { get; set; }
|
||||
public string? NomPostoTarifario { get; set; }
|
||||
public string? DscUnidadeTerciaria { get; set; }
|
||||
public string? SigAgenteAcessante { get; set; }
|
||||
public string? VlrTUSD { get; set; }
|
||||
public string? VlrTE { get; set; }
|
||||
public string? DscClasseConsumidor { get; set; }
|
||||
public string? DscSubClasseConsumidor { get; set; }
|
||||
public string? DscDetalheConsumidor { get; set; }
|
||||
public string? DscPostoTarifario { get; set; }
|
||||
public string? DscUnidade { get; set; }
|
||||
public string? SigNomeAgenteAcessante { get; set; }
|
||||
public string? DscComponenteTarifario { get; set; }
|
||||
public string? VlrComponenteTarifario { get; set; }
|
||||
}
|
||||
|
||||
public class Field
|
||||
@ -64,4 +65,5 @@ namespace TarifasANEEL
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user