Wikiproyecto:La Rioja/datos municipales/obtener plantilla
Apariencia
#!/usr/bin/perl # infobox.pl: Crea infoboxes para los municipios de la comunidad de La Rioja en la Wikipedia # versión: 0.1 # autor: Jaime Crespo "jynus" # fecha: 21 may 2006 # Este script viene sin ningún tipo de garantías -si borra sus ficheros, quema su ordenador, # o se lía con su novi@ es _SU RESPONSABILIDAD_ # # Copyright (C) 2006 Jaime Crespo # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA my $DOCUMENT = 'http://es.wikipedia.org/wiki/Wikiproyecto:La_Rioja/datos_municipales'; my $TMPFILE = `tempfile`; my $USAGE = "./infobox.pl municipio"; my $INFOBOX = "{{Infobox_Municipio_La_Rioja_España |\n". "\tnombre = NOMBRE |\n". "\tescudo = Sin escudo.svg |\n". "\tbandera = Sin bandera.svg |\n". "\timagen_localizacion = Falta.png |\n". "\timagen_representativa = Falta.png |\n". "\tregion = [[REGION]] |\n". "\tcomarca = [[COMARCA]] |\n". "\tcp = CODIGOPOSTAL |\n". "\tlat = LATITUD |\n". "\tlon = LONGITUD |\n". "\tsuperficie = SUPERFICIE [[kilómetro cuadrado|km²]] |\n". "\taltitud = ALTITUD [[metro|m]] |\n". "\tdistancia = [[Logroño]]: DISTANCIALOGRONO [[kilómetro|km]]". "<br />[[CABECERA]]: DISTANCIACABECERA [[kilómetro|km]] |\n". "\tfundacion = Sobre el [[Siglo SIGLO]]|\n". "\tpoblacion = HABITANTES hab. ([[Instituto Nacional de Estadística de España|INE]] [[2005]]) |\n". "\tdensidad = DENSIDAD hab./km² |\n". "\tgentilicio = GENTILICIO |\n". "\talcalde = ALCALDE<br />([[PARTIDO]], [[2003]]-[[2007]])|\n". "\thermanado = HERMANADO|\n". "\tsitio_web = WEB |\n". "}}\n"; my $field = "([^;]*);"; my $stringfield = "(?:\"([^\"]*)\")?;"; my $PATTERN = "^".$field.($stringfield x 6).($field x 7).($stringfield x 4).($field x 28)."([^;]*)\n\$"; my $municipio; my @data; # procesando argumentos die "Uso: $USAGE\n" if $#ARGV < 0; $municipio = "@ARGV"; # procesando datos print "\n*Descargando la última versión de datos municipales...\n\n"; `wget $DOCUMENT -O $TMPFILE`; open(TEXT, $TMPFILE) or die("ERROR: No se ha podido abrir el fichero ".$TMPFILE." para lectura."); my $search = "^".$field.$field."\"".$municipio."\";"; my @fields; # búsqueda de patrones while (<TEXT>) { if (/$search/i) { print "Se ha encontrado $municipio en la línea $.:\n\n"; @fields = m/$PATTERN/; $_ = $INFOBOX; s/NOMBRE/$fields[2]/; s/REGION/$fields[5]/; s/COMARCA/$fields[4]/; s/CODIGOPOSTAL/$fields[11]/; # transformamos coordenadas a hexadecimal $fields[8] =~ m/-?([0-9]*),([0-9]*)/; $lat = (int($1))."º ".int($2 * 0.60 + 0.5)."' N"; s/LATITUD/$lat/; $fields[9] =~ m/-?([0-9]*),([0-9]*)/; $lon = (int($1))."º ".int($2 * 0.60 + 0.5)."' O"; s/LONGITUD/$lon/; s/ALTITUD/$fields[10]/; s/SUPERFICIE/$fields[7]/; # No poner la disntancia Logroño-Logroño if ($fields[2] eq "Logroño") { s/\[\[Logroño\]\]: DISTANCIALOGRONO/[[Madrid]]: 333/; } else { s/DISTANCIALOGRONO/$fields[12]/; } # calcular capitales de comarca $fields[4] =~ s/Cameros/Torrecilla en Cameros/; $fields[4] =~ s/Alhama/Cervera del Río Alhama/; # Si está en la comarca de logroño o es una cabecera de comarca, # omitir distancia a cabecera if (($fields[4] eq "Logroño") or ($fields[2] eq $fields[4])) { s/<br \/>\[\[CABECERA\]\]: DISTANCIACABECERA \[\[kilómetro\|km\]\]//; } else { s/CABECERA/$fields[4]/; s/DISTANCIACABECERA/$fields[13]/; } # no hay todavía un campo siglo s/SIGLO//; # punto de los millares if (length($fields[45]) > 3) { $fields[45] =~ s/([0-9]*)([0-9]{3})/\1.\2/; } s/HABITANTES/$fields[45]/; s/DENSIDAD/$fields[46]/; s/GENTILICIO/$fields[6]/; s/ALCALDE/$fields[14]/; s/PARTIDO/$fields[15]/; $fields[16] =~ s/,/<br \/>/g; s/HERMANADO/$fields[16]/; s/WEB/$fields[17]/; # imprimir resultados print $_; $encontrado = 1; } } print "No se encontró el municipio: revise la ortografía o puede que el ". "fichero de datos haya cambiado de formato\n" if (!$encontrado); # limpieza close(DATA); print "\n*Borrando archivos temporales...\n\n"; `rm -f $TMPFILE`;