-
-
Save jfbueno/4674a5c859f5fef8202624d8432f8d91 to your computer and use it in GitHub Desktop.
This file contains 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
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
public void BaixarImagens(List<Galeria> galerias) | |
{ | |
const string PastaRaiz = "D:\\robotica"; | |
foreach (var galeria in galerias) | |
{ | |
var nomeGaleria = Regex.Replace(galeria.Nome, @"[^\w\d\s]+", ""); | |
var caminhoGaleria = Path.Combine(PastaRaiz, nomeGaleria); | |
Directory.CreateDirectory(caminhoGaleria); | |
var i = 0; | |
foreach (var foto in galeria.Fotos) | |
{ | |
i++; | |
var nomeImagem = foto.Caminho.Substring(foto.Caminho.LastIndexOf('/') + 1); | |
nomeImagem = nomeImagem.Substring(0, nomeImagem.LastIndexOf('.')); | |
using (var webClient = new WebClient()) | |
{ | |
byte[] data = webClient.DownloadData(foto.Caminho); | |
using (var mem = new MemoryStream(data)) | |
{ | |
using (var imagem = Image.FromStream(mem)) | |
{ | |
imagem.Save(Path.Combine(caminhoGaleria, $"{i}__{nomeImagem}" + ".jpg"), ImageFormat.Jpeg); | |
} | |
} | |
} | |
} | |
} | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
var galerias = new List<Galeria>(); | |
var connection = MySqlHelper.Connection; | |
var command = connection.CreateCommand(); | |
try | |
{ | |
connection.Open(); | |
// Selecionar todas as galerias | |
command.CommandText = "Select * From wp_posts Where post_type='dt_galleries' And post_status = 'publish' And Post_Title not like 'loren%'"; | |
command.ExecuteNonQuery(); | |
var reader = command.ExecuteReader(); | |
while (reader.Read()) | |
{ | |
galerias.Add(new Galeria | |
{ | |
Id = reader["ID"].ToString(), | |
Nome = reader["post_title"].ToString() | |
}); | |
} | |
reader.Close(); | |
// Passar por todas as galerias | |
foreach (var galeria in galerias) | |
{ | |
command = connection.CreateCommand(); | |
// Selecionar todos os posts (fotos) relacionados com esta galeria | |
command.CommandText = $"Select * From wp_posts Where post_parent = { galeria.Id }"; | |
command.ExecuteNonQuery(); | |
reader = command.ExecuteReader(); | |
while (reader.Read()) | |
{ | |
galeria.Fotos.Add(new FotoGaleria {Id = reader["ID"].ToString(), Caminho = reader["guid"].ToString()}); | |
} | |
reader.Close(); | |
} | |
} | |
finally | |
{ | |
if (connection.State == ConnectionState.Open) | |
connection.Close(); | |
} | |
BaixarImagens(galerias); | |
} | |
} | |
public class Galeria | |
{ | |
public string Id { get; set; } | |
public string Nome { get; set; } | |
public List<FotoGaleria> Fotos { get; set; } | |
public Galeria() | |
{ | |
Fotos = new List<FotoGaleria>(); | |
} | |
} | |
public class FotoGaleria | |
{ | |
public string Id { get; set; } | |
public string Caminho { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment