Skip to content

Commit

Permalink
add project
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrovasconcellos committed Apr 12, 2023
1 parent e2852ed commit b4d3ec5
Show file tree
Hide file tree
Showing 15 changed files with 18,408 additions and 2 deletions.
39 changes: 39 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc

# Visual Studio Code
.vscode

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
msbuild.log
msbuild.err
msbuild.wrn

# Visual Studio 2015
.vs/
ZipeCodeConsole/bin/Debug/ConsoleCEP.exe
ZipeCodeConsole/bin/Debug/ConsoleCEP.pdb
*.exe
*.pdb
ZipeCodeConsoleCore/ZipeCodeConsoleCore.csproj.user
ZipeCodeConsoleCore/cop
ZipeCodeConsoleCore/ZipeCodeConsoleCore.csproj.user
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,50 @@
# zip-code-download-robot
esigned to retrieve Brazilian Zipe Code data and store it in a MongoDB database. This powerful tool has been created using C# .NET. Info: {..., StreetName, Altitude, Latitude, Longitude}
# ZipeCode Robot [net7.0]
Designed to retrieve Brazilian ZipeCode data and store it in a MongoDB database. This powerful tool has been created using C# .NET.


### Load the ceps that are in the [zipe-code-numbers.csv] for the ZipeCodeNumber Collection MongoDB
##### Data format when loading into the collection
```json
{
"_id" : ObjectId("643411c9fe31a5566cf1a8e8"),
"cep" : "87043350"
},
{
"_id" : ObjectId("643411bcfe31a5566cf17df0"),
"cep" : "85803330"
},
{
"_id" : ObjectId("643411bafe31a5566cf17620"),
"cep" : "85070560"
},
```

### Set Environment Variable

##### MongoDB ConnectionString:
```text
Environment.GetEnvironmentVariable("Vasconcellos_ZipeCode_ConsoleApp_MongoDB")
```

##### www.cepaberto.com Token (Example):
```text
- Environment.GetEnvironmentVariable("Vasconcellos_ZipeCode_ConsoleApp_Token") = 09cadce3330c25fc7de26238042a9df9
```


##### Example of Data generated by the robot
```json
{
"_id" : "01001000",
"bairro" : "",
"logradouro" : "Praça da Sé",
"cidade" : "São Paulo",
"estado" : "SP",
"ddd" : 11,
"ibge" : 3550308,
"altitude" : "760.0",
"latitude" : "-23.5479099981",
"longitude" : "-46.636",
"datetime" : ISODate("2023-04-08T08:23:08.980-03:00")
}
```
42 changes: 42 additions & 0 deletions ZipeCode.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.421
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZipeCodeConsoleCore", "ZipeCodeConsoleCore\ZipeCodeConsoleCore.csproj", "{921C8AFE-6071-48C1-9536-D9B7C45D3141}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{608C7E2A-972E-45FD-8040-5ED93AF0601E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{608C7E2A-972E-45FD-8040-5ED93AF0601E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{608C7E2A-972E-45FD-8040-5ED93AF0601E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{608C7E2A-972E-45FD-8040-5ED93AF0601E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{608C7E2A-972E-45FD-8040-5ED93AF0601E}.Release|Any CPU.Build.0 = Release|Any CPU
{608C7E2A-972E-45FD-8040-5ED93AF0601E}.Release|Any CPU.Deploy.0 = Release|Any CPU
{921C8AFE-6071-48C1-9536-D9B7C45D3141}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{921C8AFE-6071-48C1-9536-D9B7C45D3141}.Debug|Any CPU.Build.0 = Debug|Any CPU
{921C8AFE-6071-48C1-9536-D9B7C45D3141}.Release|Any CPU.ActiveCfg = Release|Any CPU
{921C8AFE-6071-48C1-9536-D9B7C45D3141}.Release|Any CPU.Build.0 = Release|Any CPU
{44572707-CA58-4C4C-B880-CDA03A26122F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44572707-CA58-4C4C-B880-CDA03A26122F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44572707-CA58-4C4C-B880-CDA03A26122F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44572707-CA58-4C4C-B880-CDA03A26122F}.Release|Any CPU.Build.0 = Release|Any CPU
{5CB52F56-6B08-41CB-84C3-C6DA5E090CB4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5CB52F56-6B08-41CB-84C3-C6DA5E090CB4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5CB52F56-6B08-41CB-84C3-C6DA5E090CB4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5CB52F56-6B08-41CB-84C3-C6DA5E090CB4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1446DE5E-7CF2-4D9F-960A-09074EB747BA}
EndGlobalSection
GlobalSection(MonoDevelopProperties) = preSolution
version = 1.1.0
EndGlobalSection
EndGlobal
29 changes: 29 additions & 0 deletions ZipeCodeConsoleCore/Models/ZipeCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace ZipeCodeConsoleCore.Models
{
public class ZipeCode : ZipeCodeBase
{
public string bairro { get; set; }
public string logradouro { get; set; }
public Cidade cidade { get; set; }
public Estado estado { get; set; }
public decimal? altitude { get; set; }
public decimal? latitude { get; set; }
public decimal? longitude { get; set; }
public DateTime datetime { get; set; } = DateTime.UtcNow;
}

public class Cidade
{
public short? ddd { get; set; }
public int? ibge { get; set; }
public string nome { get; set; }
}

public class Estado
{
public string sigla { get; set; }
}
}
10 changes: 10 additions & 0 deletions ZipeCodeConsoleCore/Models/ZipeCodeBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
namespace ZipeCodeConsoleCore.Models
{
public abstract class ZipeCodeBase
{
[MongoDB.Bson.Serialization.Attributes.BsonId]
public virtual string cep { get; set; }
}
}

38 changes: 38 additions & 0 deletions ZipeCodeConsoleCore/Models/ZipeCodeDesnormalized.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.ComponentModel.DataAnnotations;

namespace ZipeCodeConsoleCore.Models
{
public class ZipeCodeDesnormalized : ZipeCodeBase
{
public ZipeCodeDesnormalized()
{

}
public ZipeCodeDesnormalized(ZipeCode model)
{
this.cep = model.cep;
this.bairro = model.bairro;
this.logradouro = model.logradouro;
this.cidade = model.cidade?.nome;
this.estado = model.estado?.sigla;
this.ddd = model.cidade?.ddd;
this.ibge = model.cidade?.ibge;
this.altitude = model.altitude;
this.latitude = model.latitude;
this.longitude = model.longitude;
this.datetime = model.datetime;
}

public string bairro { get; set; }
public string logradouro { get; set; }
public string cidade { get; set; }
public string estado { get; set; }
public short? ddd { get; set; }
public int? ibge { get; set; }
public decimal? altitude { get; set; }
public decimal? latitude { get; set; }
public decimal? longitude { get; set; }
public DateTime datetime { get; set; } = DateTime.UtcNow;
}
}
14 changes: 14 additions & 0 deletions ZipeCodeConsoleCore/Models/ZipeCodeNumber.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace ZipeCodeConsoleCore.Models
{
public class ZipeCodeNumber
{
[BsonId]
public ObjectId _id { get; set; }
public string cep { get; set; }
}
}

138 changes: 138 additions & 0 deletions ZipeCodeConsoleCore/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using ZipeCodeConsoleCore.Models;
using ZipeCodeConsoleCore.Repository;

namespace ZipeCodeConsoleCore
{
class Program
{
private static MongoDBRepository _repository;
private static ZipeCodeHttpRequest _http;

static void Main(string[] args)
{
try
{
Init();
Process().Wait();
Console.ReadKey();
}
catch (Exception ex)
{
PrintColor(ex.Message, ConsoleColor.Red);
Console.ReadKey();
}
}

private static async Task Process()
{
PrintColor($"Start Process. DateTime: {DateTime.Now}", ConsoleColor.Green);

var allZipeCodes = (await _repository.GetAllAsyncNumber())
?.Select(x => x.cep).ToList()
?? new List<string>();

foreach (var zipeCodeItem in allZipeCodes)
{
var zipeCodeFormatItem = zipeCodeItem.ToString().PadLeft(8, '0');
await ProcessCep(zipeCodeFormatItem);
}

PrintColor("Finished Process.", ConsoleColor.Green);
await Task.CompletedTask;
}

private static async Task ProcessCep(string zipeCodeNumber)
{
if (_repository.ZipeCodeExist<ZipeCodeDesnormalized>(zipeCodeNumber))
return;

PrintColor((String.Concat("Get CEP: ", zipeCodeNumber.Insert(5, "-"))), (ConsoleColor)new Random().Next(7, 14));

var zipeCodeSelected = default(ZipeCode);
var zipeCodeDesnormalized = default(ZipeCodeDesnormalized);
while (zipeCodeDesnormalized == null)
{
zipeCodeSelected = _http.GetZipeCodeInfo(zipeCodeNumber);
if (zipeCodeSelected == null)
PrintColor($"Error on the server, performing a new request! ZipeCode = {zipeCodeNumber}", ConsoleColor.Magenta);
else
zipeCodeDesnormalized = new ZipeCodeDesnormalized(zipeCodeSelected);
}

if (string.IsNullOrEmpty(zipeCodeDesnormalized.cep)
&& string.IsNullOrEmpty(zipeCodeDesnormalized.estado)
&& string.IsNullOrEmpty(zipeCodeDesnormalized.cidade))
{
PrintColor($"ZipeCode {zipeCodeNumber.Insert(5, "-")} not found!", ConsoleColor.Yellow);
return;
}

PrintObject(zipeCodeDesnormalized);
await SaveZipeCode(zipeCodeDesnormalized);
await SaveZipeCode(zipeCodeSelected);
}

private static async Task SaveZipeCode<T>(
T zipeCodeDesnormalized) where T : ZipeCodeBase
{
await _repository.InsertOneAsync(zipeCodeDesnormalized);
}

private static void PrintObject(object obj)
{
string response = JsonConvert.SerializeObject(obj);
PrintColor(response, ConsoleColor.Green);
}

private static void PrintColor(string text, ConsoleColor fontColor)
{
Console.ForegroundColor = fontColor;
Console.WriteLine(String.Concat(text, Environment.NewLine));
}

private static void Init()
{
using var serviceProvider = new ServiceCollection()
.AddLogging(config =>
config
.ClearProviders()
.AddConsole()
.SetMinimumLevel(LogLevel.Information)
)
.BuildServiceProvider();


var connectionString = Environment.GetEnvironmentVariable("Vasconcellos_ZipeCode_ConsoleApp_MongoDB");
if (string.IsNullOrEmpty(connectionString))
throw new ArgumentException("Connection string invalid!");

var token = Environment.GetEnvironmentVariable("Vasconcellos_ZipeCode_ConsoleApp_Token");
if (string.IsNullOrEmpty(token))
throw new ArgumentException("token http-request invalid!");

var loggerM = serviceProvider
.GetService<ILoggerFactory>()
.CreateLogger<MongoDBRepository>();

if (loggerM is null)
throw new ArgumentNullException(nameof(loggerM));

_repository = new MongoDBRepository(loggerM, connectionString);

var loggerZ = serviceProvider
.GetService<ILoggerFactory>()
.CreateLogger<ZipeCodeHttpRequest>();

if (loggerZ is null)
throw new ArgumentNullException(nameof(loggerZ));
_http = new ZipeCodeHttpRequest(loggerZ, token);
}
}
}
Loading

0 comments on commit b4d3ec5

Please sign in to comment.