﻿#!/share/CACHEDEV1_DATA/.qpkg/Optware/bin/perl -w

# ATENCION: Este es el script para ejecutarlo QNAP!!!
# Estas son las categorias (solo en Ingles) reconocidas por Tvheadend (ver epg.c) 
# Actualizado el 14/09/2016

open(LOG, ">>", "/share/CACHEDEV1_DATA/homes/nico/wg++/categorias/cat_log.txt")  or die "Can't open LOG file: $!";

my $BAD	;
my $MOVIE             =    "Movie / Drama";
my $THRILLER          =    "Detective / Thriller";
my $ADVENTURE         =    "Adventure / Western / War";
my $SF                =    "Science fiction / Fantasy / Horror";
my $COMEDY            =    "Comedy";
my $SOAP              =    "Soap / Melodrama / Folkloric";
my $ROMANCE           =    "Romance";
my $HISTORICAL        =    "Serious / Classical / Religious / Historical movie / Drama";
my $XXX               =    "Adult movie / Drama";

my $NEWS              =    "News / Current affairs";
my $WEATHER           =    "News / Weather report";
my $NEWS_MAGAZINE     =    "News magazine";
my $DOCUMENTARY       =    "Documentary";
my $DEBATE            =    "Discussion / Interview / Debate";
my $INTERVIEW         =    $DEBATE ;

my $SHOW              =    "Show / Game show";
my $GAME              =    "Game show / Quiz / Contest";
my $VARIETY           =    "Variety show";
my $TALKSHOW          =    "Talk show";

my $SPORT             =    "Sports";
my $SPORT_SPECIAL     =    "Special events (Olympic Games; World Cup; etc.)";
my $SPORT_MAGAZINE    =    "Sports magazines";
my $FOOTBALL          =    "Football / Soccer";
my $TENNIS            =    "Tennis / Squash";
my $SPORT_TEAM        =    "Team sports (excluding football)";
my $ATHLETICS         =    "Athletics";
my $SPORT_MOTOR       =    "Motor sport";
my $SPORT_WATER       =    "Water sport";

my $KIDS              =    "Children's / Youth programmes";
my $KIDS_0_5          =    "Pre-school children's programmes";
my $KIDS_6_14         =    "Entertainment programmes for 6 to 14";
my $KIDS_10_16        =    "Entertainment programmes for 10 to 16";
my $EDUCATIONAL       =    "Informational / Educational / School programmes";
my $CARTOON           =    "Cartoons / Puppets";

my $MUSIC             =    "Music / Ballet / Dance";
my $ROCK_POP          =    "Rock / Pop";
my $CLASSICAL         =    "Serious music / Classical music";
my $FOLK              =    "Folk / Traditional music";
my $JAZZ              =    "Jazz";
my $OPERA             =    "Musical / Opera";

my $CULTURE           =    "Arts / Culture (without music)";
my $PERFORMING        =    "Performing arts";
my $FINE_ARTS         =    "Fine arts";
my $RELIGION          =    "Religion";
my $POPULAR_ART       =    "Popular culture / Traditional arts";
my $LITERATURE        =    "Literature";
my $FILM              =    "Film / Cinema";
my $EXPERIMENTAL_FILM =    "Experimental film / Video";
my $BROADCASTING      =    "Broadcasting / Press";

my $SOCIAL            =    "Social / Political issues / Economics";
my $MAGAZINE          =    "Magazines / Reports / Documentary";
my $ECONOMIC          =    "Economics / Social advisory";
my $VIP               =    "Remarkable people";

my $SCIENCE           =    "Education / Science / Factual topics";
my $NATURE            =    "Nature / Animals / Environment";
my $TECHNOLOGY        =    "Technology / Natural sciences";
my $DIOLOGY           =    $TECHNOLOGY;
my $MEDECINE          =    "Medicine / Physiology / Psychology";
my $FOREIGN           =    "Foreign countries / Expeditions";
my $SPIRITUAL         =    "Social / Spiritual sciences";
my $FURTHER_EDUCATION =    "Further education";
my $LANGUAGES         =    "Languages";

my $HOBBIES           =    "Leisure hobbies";
my $TRAVEL            =    "Tourism / Travel";
my $HANDICRAF         =    "Handicraft";
my $MOTORING          =    "Motoring";
my $FITNESS           =    "Fitness and health";
my $COOKING           =    "Cooking";
my $SHOPPING          =    "Advertisement / Shopping";
my $GARDENING         =    "Gardening";

#
# A continuacion las equivalencias de las categorias de la EPG que obtengo con las de Tvheadend 
#
#
#

my %REPLACE=(
    "El Tiempo"        					=> $WEATHER ,
    "Acción y Aventuras"        		=> $ADVENTURE ,
    "Acción"							=> $THRILLER ,
    "Aventuras"							=> $ADVENTURE ,
    "Animación Adultos" 				=> $XXX ,
    "Animación"             			=> $CARTOON,
    "Automovilismo"           			=> $SPORT_MOTOR ,
    "Actualidad"     					=> $VARIETY ,
	"Artes Marciales"					=> $SPORT_SPECIAL ,
    "Atletismo"           				=> $ATHLETICS ,
    "Baloncesto"           				=> $SPORT_TEAM ,
    "Balonmano"							=> $SPORT_TEAM ,
    "Bélica"           					=> $ADVENTURE  ,
    "Bricolaje"							=> $HANDICRAF ,
    "Boxeo"								=> $SPORT ,
    "Biográficos"						=> $DOCUMENTARY  ,
    "Caza"           					=> $NATURE ,
    "Champions League"					=> $FOOTBALL ,
    "Ciencia y tecnología"				=> $TECHNOLOGY ,
    "Curso educativo"					=> $SCIENCE ,
    "Concursos"                			=> $GAME ,
    "Cultura"            				=> $CULTURE,
	"Cultural/Educativo"				=> $CULTURE ,
    "Cocina"          					=> $COOKING  ,
    "Cine"           					=> $MOVIE ,
	"Cine, Acción"						=> $THRILLER  ,
	"Cine, Acción y Aventuras"			=> $THRILLER  ,
	"Cine, Animación"					=> $MOVIE ,
	"Cine, Artes Marciales"				=> $MOVIE ,
	"Cine, Aventuras"					=> $THRILLER  ,
	"Cine, Bélica"						=> $MOVIE ,
    "Cine, Ciencia ficción"				=> $SF ,
	"Cine, Cine Negro"					=> $MOVIE ,
	"Cine, Comedia"						=> $COMEDY ,
	"Cine, Documental"					=> $DOCUMENTARY ,
	"Cine, Documentales"				=> $DOCUMENTARY ,
	"Cine, Drama"						=> $MOVIE ,
	"Cine, Erótica"						=> $XXX ,
	"Cine, Fantástico"					=> $SF ,
	"Cine, Histórico"					=> $HISTORICAL ,
	"Cine, Infantil / Familiar"			=> $ADVENTURE ,
	"Cine, Musical"						=> $SOAP ,
	"Cine, Romántica"					=> $ROMANCE ,
	"Cine, Suspense / Thriller"			=> $THRILLER ,
	"Cine, Terror"						=> $SF ,
	"Cine, Wéstern"						=> $ADVENTURE ,
	"Cine Negro"        				=> $MOVIE ,
    "Corazón"       					=> $VARIETY ,
    "Concierto"             			=> $MUSIC ,
	"Concurso"							=> $GAME ,
    "Comedia"           				=> $COMEDY,
    "Ciencia ficción"           		=> $SF  ,
    "Ciclismo"           				=> $SPORT ,
    "Cortometraje"						=> $EXPERIMENTAL_FILM ,
    "Divulgativo"						=> $NEWS_MAGAZINE ,
	"Deportes, Automovilismo"			=> $SPORT_MOTOR ,
	"Deportes, Balonmano"				=> $SPORT_TEAM ,
	"Deportes, Boxeo"					=> $SPORT ,
	"Deportes, Champions League"		=> $FOOTBALL ,
	"Deportes, Ciclismo"				=> $SPORT_SPECIAL ,
	"Deportes, Deportes náuticos"		=> $SPORT_WATER ,
	"Deportes, Documentales"			=> $SPORT_MAGAZINE ,
	"Deportes, Fútbol"					=> $FOOTBALL ,
	"Deportes, Golf"					=> $SPORT_SPECIAL ,
	"Deportes, Hípica"					=> $SPORT_SPECIAL ,
	"Deportes, Juegos Olímpicos"		=> $SPORT_SPECIAL ,
	"Deportes, Motociclismo"			=> $SPORT_MOTOR ,
	"Deportes, Otros deportes"			=> $SPORT ,
	"Deportes, Pesca"					=> $HOBBIES ,
	"Deportes, Póquer"					=> $GAME ,
	"Deportes, Voleibol"				=> $SPORT_TEAM ,
    "Drama"               				=> $MOVIE ,
    "Documental Viaje"          		=> $TRAVEL,
    "Documentaire"       				=> $DOCUMENTARY ,
    "Documental Actualidad"     		=> $DOCUMENTARY,
	"Documental Cine"					=> $DOCUMENTARY ,
    "Documental Historia"       		=> $DOCUMENTARY,
    "Documental Naturaleza y Animales"  => $NATURE ,
	"Docu-reality"						=> $MAGAZINE ,
    "Deportes"              			=> $SPORT ,
    "Dibujos Animados"           		=> $CARTOON ,
    "Documental Antropológico"          => $DOCUMENTARY  ,
    "Documental Biográfico"           	=> $DOCUMENTARY  ,
    "Documentales"           			=> $DOCUMENTARY  ,
    "Documental Arte y Espectáculos"    => $CULTURE  ,
    "Documental Ciencia y tecnología"   => $TECHNOLOGY ,
    "Documental"           				=> $DOCUMENTARY ,
    "Deportiva"           				=> $SPORT ,
    "Docu-Drama"           				=> $HISTORICAL ,
    "Deportes náuticos"					=> $SPORT_WATER ,
    "Deportes de invierno"				=> $SPORT_SPECIAL ,
    "Decoración"						=> $CULTURE ,
    "Debates"							=> $DEBATE , 
    "Economía"							=> $ECONOMIC ,
    "Entrevistas"						=> $INTERVIEW ,
	"Entretenimiento"					=> $VARIETY ,
    "Eróticas"							=> $XXX ,
    "Erótica"							=> $XXX ,
	"Especial"							=> $SPORT_SPECIAL ,
    "Fantástico"            			=> $SF  ,
    "Fantástica"           				=> $SF ,
    "Fútbol"         					=> $FOOTBALL ,
    "Fórmula 1"							=> $SPORT_MOTOR ,
	"Gastronómico"						=> $COOKING ,
    "Gimnasia"							=> $SPORT ,
	"Golf"								=> $SPORT_SPECIAL ,
    "Humor"           					=> $COMEDY ,
    "Histórico"							=> $HISTORICAL ,
    "Histórica"							=> $HISTORICAL ,
    "Hípica"							=> $SPORT ,
    "Informativos"						=> $NEWS ,
	"Informativos, Otros"				=> $NEWS ,
	"Información"						=> $NEWS ,
    "Infantil"           				=> $KIDS ,
	"Infantil, Dibujos Animados"		=> $KIDS ,
	"Infantil, Series"					=> $KIDS ,
    "Infantil / Familiar"           	=> $KIDS , 
    "Juvenil"							=> $KIDS ,
	"Juveniles"							=> $KIDS ,
    "Juegos Olímpicos"					=> $SPORT_SPECIAL ,
    "Judicial"							=> $SOCIAL ,
    "Literatura"						=> $LITERATURE ,
    "Lucha"								=> $SPORT ,
    "Magacín"           				=> $MAGAZINE ,
	"Magacín, Biográficos"				=> $MAGAZINE ,
	"Magacín, Magazines"				=> $MAGAZINE ,
	"Magacín, Ciencia y tecnología"		=> $TECHNOLOGY ,
	"Magacín, Musicales"				=> $MUSIC ,
	"Magacín, Otros"					=> $MAGAZINE ,
	"Magacín, Reality Show"				=> $VARIETY ,
	"Magacín, Religiosos"				=> $RELIGION ,
	"Magacín, Videoclips"				=> $MUSIC ,	
    "Magazines"							=> $MAGAZINE ,
	"Manga"								=> $KIDS_10_16 ,
    "Medio Ambiente"             		=> $DOCUMENTARY ,
    "Musicales"           				=> $MUSIC ,
    "Musical"							=> $OPERA ,
    "Moda"								=> $CULTURE ,
    "Motociclismo"           			=> $SPORT_MOTOR ,
    "Misterio"           				=> $THRILLER ,
    "Música popular"					=> $FOLK ,
	"Natación"							=> $SPORT_WATER ,
    "Naturaleza y animales"           	=> $NATURE ,
    "Noticias"           				=> $NEWS ,
	"Ocio y Aficiones"					=> $HOBBIES ,
    "Ópera"								=> $OPERA ,
    "Otros deportes"           			=> $SPORT,
    "Otros"           					=> 0 ,
    "Política"							=> $SOCIAL ,         
    "Parapsicología"					=> $MEDECINE ,
    "Póquer"							=> $GAME ,
    "Policíacas"                		=> $THRILLER  ,
    "Pesca"           					=> $NATURE ,
    "Programas"							=> $VARIETY ,
    "Profesiones"           			=> $THRILLER , 
    "Reportajes"						=> $NEWS_MAGAZINE ,
    "Romántica"							=> $ROMANCE ,
    "Reportaje Actualidad"				=> $NEWS_MAGAZINE ,
	"Reportaje Economía"				=> $ECONOMIC ,
    "Reportaje Sociológico"				=> $SOCIAL ,
    "Religion"           				=> $RELIGION ,
    "Reality Show"           			=> $VARIETY ,
    "Religiosos"						=> $RELIGION ,
	"Rugby"								=> $SPORT_TEAM ,
    "Sorteos"							=> $GAME ,
	"Serie"								=> $SHOW ,
	"Serie documental"					=> $DOCUMENTARY ,
    "Series"              				=> $SHOW ,
	"Series, Acción y aventuras"		=> $SHOW ,
	"Series, Animación Adultos"			=> $KIDS_10_16 ,
	"Series, Comedia"					=> $SHOW ,
	"Series, Ciencia ficción"			=> $SF  ,
	"Series, Docu-Drama"				=> $SHOW ,
	"Series, Drama"						=> $SHOW ,
	"Series, Eróticas"					=> $XXX ,
	"Series, Fantástica"				=> $SHOW ,
	"Series, Juvenil"					=> $KIDS_10_16 ,
	"Series, Manga"						=> $KIDS_10_16 ,
	"Series, Misterio"					=> $SHOW ,
	"Series, Policíacas"				=> $SHOW ,
	"Series, Profesiones"				=> $SHOW ,
	"Series, Telenovela"				=> $SHOW ,
	"Series, Todas"						=> $SHOW ,	
    "Salud y belleza"					=> $FITNESS ,
	"Sin clasificar"					=> 0 ,
    "Sucesos"							=> $NEWS ,
    "Suspense / Thriller"           	=> $THRILLER  ,
	"Talent Show"						=> $GAME ,
	"Talk Shows"						=> $TALKSHOW ,
    "Telenovela"						=> $ROMANCE ,
    "Televisión"						=> $VARIETY ,
    "Terror"           					=> $SF , 
    "Teatro"							=> $MOVIE , 
    "Tenis"           					=> $TENNIS ,
    "Teletienda"           				=> $SHOPPING ,
    "Todas"								=> 0 ,
    "Toros"								=> $SPORT_SPECIAL ,	
    "Viajes"             				=> $TRAVEL ,
    "Videoclips"           				=> $MUSIC ,
	"Voleibol"							=> $SPORT_TEAM ,
    "Variedades"           				=> $VARIETY ,
    "Wéstern"           				=> $ADVENTURE ,
    "Waterpolo"							=> $SPORT_WATER ,
	"Zapping"							=> $HOBBIES ,
	
	

    
 ) ; 

my $PRE  = '<category lang=\"es\">' ;
my $POST = '</category>'  ;

sub myfilter {
  my ($a) = @_;
  if ( exists $REPLACE{$a} ) {     
      return $REPLACE{$a} ;
  } else {
open(FH, '>', '/share/CACHEDEV1_DATA/homes/nico/wg++/categorias/temporal.txt') or die "cannot open file";
select FH;
  if ( ! exists $BAD{$a} ) {
      print STDERR "ATENCION: Agregar categoria al script: '$a'\n" ;
	  print LOG    "ATENCION: Agregar categoria al script: '$a'\n" ;
  # Record in BAD map so next error won´t produce a message
      $BAD{$a} = 1 ;
      return $a ;
  }
}

while (<>) {
    my $line = $_ ;
    $line =~ s/($PRE)(.*)($POST)/"$1".myfilter("$2")."$3"/ge ;
    print $line;
}