public static void DownloadFotosSenadores(string dirRaiz)
        {
            using (var banco = new Banco())
            {
                DataTable table = banco.GetTable("SELECT id, foto FROM sf_senador where foto is not null", 0);

                foreach (DataRow row in table.Rows)
                {
                    string id  = row["id"].ToString();
                    string src = dirRaiz + id + ".jpg";
                    if (File.Exists(src))
                    {
                        continue;
                    }

                    try
                    {
                        using (WebClient client = new WebClient())
                        {
                            client.Headers.Add("User-Agent: Other");
                            client.DownloadFile(row["foto"].ToString(), src);

                            ImportacaoUtils.CreateImageThumbnail(src);

                            Console.WriteLine("Atualizado imagem do senador " + id);
                        }
                    }
                    catch (Exception)
                    {
                        //ignore
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static string DownloadFotosSenadores(string dirRaiz)
        {
            var db = new StringBuilder();

            using (var banco = new Banco())
            {
                DataTable table = banco.GetTable("SELECT id FROM sf_senador where valor_total_ceaps > 0 or ativo = 'S'");

                foreach (DataRow row in table.Rows)
                {
                    string id  = row["id"].ToString();
                    string url = "https://www.senado.leg.br/senadores/img/fotos-oficiais/senador" + id + ".jpg";
                    string src = dirRaiz + id + ".jpg";
                    if (File.Exists(src))
                    {
                        continue;
                    }

                    try
                    {
                        using (WebClient client = new WebClient())
                        {
                            client.Headers.Add("User-Agent: Other");
                            client.DownloadFile(url, src);

                            ImportacaoUtils.CreateImageThumbnail(src, 120, 160);
                            ImportacaoUtils.CreateImageThumbnail(src, 240, 300);

                            db.AppendLine("Atualizado imagem do senador " + id);
                            Console.WriteLine("Atualizado imagem do senador " + id);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (!ex.Message.Contains("404"))
                        {
                            db.AppendLine("Imagem do senador " + id + " inexistente! Motivo: " + ex.ToFullDescriptionString());
                            Console.WriteLine("Imagem do senador " + id + " inexistente! Motivo: " + ex.ToFullDescriptionString());
                            //ignore
                        }
                    }
                }
            }

            return(db.ToString());
        }