#!/usr/bin/perl # # gabriel 2010/06/03 # gabriel 2011/04/01 update to biblehal # # apt-get install libwww-mechanize-perl libwww-perl use strict; use WWW::Mechanize; use Data::Dumper; use HTML::Form; my $laboratory = shift @ARGV; my $year = shift @ARGV; die "Usage /usr/lib/biblehal/biblehal-pop laboratory year\n" if $year < 1990 or $year > 2020; mkdir '/var/cache/biblehal/download', 0750; my $F = < END_HTML # On intègre le second formulaire qui se remplit normalement via un javascript my $parser = HTML::Form->parse("$F", "http://hal.archives-ouvertes.fr/$laboratory"); print($@) if $@; #print Dumper $parser; # Création du bot my $url = "http://hal.archives-ouvertes.fr/$laboratory"; my $bot = WWW::Mechanize->new; $bot->env_proxy(); # Load proxy settings from *_proxy environment variables # Hop, c'est parti, premiere page $bot->get($url); #my ( $firstoccur ) = grep /service_export/, split /\n/, $bot->content; # &halsid=tgtdq7gpp47p64fd67u2imrfv1&label #my ( $halsid ) = $firstoccur =~ m{ \& halsid = ([\w\d]+) \& label }xm; #print "SESSIONID : $halsid\n"; #my $url2 = 'http://hal.archives-ouvertes.fr/view_by_stamp.php?&halsid='.$halsid.'&label=LEGI&langue=fr&action_todo=service_export'; #$bot->get($url2); # On suit le lien pour exporter les publications du LEGI # Export d'une liste de publications $bot->follow_link( text_regex => qr/Export\sd.une\sliste\sde\spublications/, url_regex => qr/action_todo=service_export/ ); # Hop, seconde page, on choisis le premier (et seul) formulaire $bot->form_number(1); # Choix de l'année $bot->select( 'f_0' => 'YEAR' ); $bot->select( 'p_0' => 'is_exactly' ); $bot->field( 'v_0' => "$year" ); # Format simplifié de la structure "current_form" #inputs #'name' => 'search_in_typdoc[]', #'menu' => [ # { # 'seen' => 1, # 'value' => undef, # 'name' => 'off' # }, # { # 'value' => 'ART_ACL', # 'name' => 'Articles dans des revues avec comité de lecture' # } # ], #'name' => 'search_in_typdoc[]', # On récupère dans le formulaire la liste des types de documents my @typdoc = (); foreach my $item ( @{$bot->current_form->{inputs}} ) { next if not $item->{'name'} eq 'search_in_typdoc[]'; #print "TYPDOCi : ".$item->{'menu'}[1]{'value'}."\n"; push @typdoc, $item->{'menu'}[1]{'value'}; } #print "TYPDOC : @typdoc\n"; $bot->select( 'search_in_typdoc[]' => \@typdoc ); #$bot->select( 'search_in_typdoc[]' => [ 'THESE' ]); $bot->click( 'submit' ); #print $bot->content; #print Dumper $bot->current_form; # Hop, troisième page, on prends le second formulaire $bot->form_number(2); #print Dumper $bot->current_form; # On intègre à la main les valeurs dans le formulaire # qui se fait en javascript via firefox foreach my $input ( @{$parser->{inputs}} ) { push @{$bot->current_form->{inputs}}, $input; } # On récupère dans le formulaire la liste des items à exporter my @wanted_meta = (); foreach my $item ( @{$bot->current_form->{inputs}} ) { next if not $item->{'name'} eq 'wanted_meta[]'; #print "WANTED_METAi : ".$item->{'menu'}[1]{'value'}."\n"; push @wanted_meta, $item->{'menu'}[1]{'value'}; } #print "WANTED_META : @wanted_meta\n"; #print Dumper $bot->current_form; $bot->select( 'wanted_meta[]' => \@wanted_meta); #$bot->select( 'wanted_meta[]' => [ 'id_article', 'title', 'auteur_nomprenom','yearstamp' ]); $bot->select( 'export_format' => [ 'BIBTEX' ]); $bot->click( 'submit' ); print $bot->content; # Hop, quatrième page. On récupère l'URL du fichier temporaire sur le serveur my ( $biburlline ) = grep /http\:\/\/hal\.archives-ouvertes\.fr\/tmp\/hal/, split /\n/, $bot->content; my ( $biburl ) = $biburlline =~ m{ ( http://hal\.archives-ouvertes\.fr/ tmp/ hal[\w\d]+\.bib ) }xm; $bot->get("$biburl", ':content_file' => "/var/cache/biblehal/download/hal-extract-auto-$year.bib" ); print "URLLINE : $biburlline\n"; print "URL : $biburl\n"; #Télécharger le fichier créé ici #Télécharger le fichier créé ici #my $biburl = $bot->find_link(text_regex => qr/ici/, url_regex => qr/http:\/\/hal\.archives-ouvertes\.fr\/tmp\/hal[\d\w]+\.bib/); ##my $biburl = $bot->find_link(url_regex => qr/http:\/\/hal\.archives-ouvertes\.fr\/tmp\/hal[\d\w]+\.bib/); ##sleep 5; ##$bot->get( $biburl->url(), ':content_file' => '/tmp/hal-extract-auto.bib' ); # Hop, le fichier .bib est sauvé sous /tmp/hal-extract-auto.bib # Mission accomplie