diff --git a/.gitmodules b/.gitmodules index 7690e43..a441373 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,30 +1,33 @@ [submodule "includes/boz-mw"] path = includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw.git [submodule "2018-06-wikidata-volleyball-players/includes/boz-mw"] path = 2018-06-wikidata-volleyball-players/includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw [submodule "2018-06-wikidata-soccer-serie-a/includes/boz-mw"] path = 2018-06-wikidata-soccer-serie-a/includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw.git [submodule "2018-02-italian-parks/includes/boz-mw"] path = 2018-02-italian-parks/includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw.git [submodule "2018-07-wikipedia-citation-language-migration/includes/boz-mw"] path = 2018-07-wikipedia-citation-language-migration/includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw [submodule "2017-11-wikidata-volleyball-players/includes/boz-mw"] path = 2017-11-wikidata-volleyball-players/includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw [submodule "2018-08-wiki-loves-monuments-switzerland/boz-mw"] path = 2018-08-wiki-loves-monuments-switzerland/includes/boz-mw url = git@github.com:valerio-bozzolan/boz-mw.git [submodule "2018-09-mibact-fixer/boz-mw"] path = 2018-09-mibact-fixer/boz-mw url = git@github.com:valerio-bozzolan/boz-mw.git [submodule "2018-09-23-itwiki-gnu-fdl/boz-mw"] path = 2018-09-23-itwiki-gnu-fdl/boz-mw url = https://github.com/valerio-bozzolan/boz-mw [submodule "2019-05-commons-volleyball-players-upload/boz-mw"] path = 2019-05-commons-volleyball-players-upload/include/boz-mw url = https://github.com/valerio-bozzolan/boz-mw +[submodule "2019-05-commons-volleyball-players-upload/include/boz-mw"] + path = 2019-05-commons-volleyball-players-upload/include/boz-mw + url = https://github.com/valerio-bozzolan/boz-mw diff --git a/2019-05-commons-volleyball-players-upload/bot.php b/2019-05-commons-volleyball-players-upload/bot.php index cb3d4e8..a828a15 100755 --- a/2019-05-commons-volleyball-players-upload/bot.php +++ b/2019-05-commons-volleyball-players-upload/bot.php @@ -1,135 +1,122 @@ #!/usr/bin/php . +// autoload framework require 'include/boz-mw/autoload.php'; + +// load credentials require '../config.php'; use \web\MediaWikis; use \cli\Log; +use \network\ContentDisposition; +// login in MediaWiki Commons $wiki = MediaWikis::findFromUID( 'commonswiki' ); -//$wiki->login(); +$wiki->login(); // fingerprint of the default pic $default_pic_md5 = md5( file_get_contents( 'data/default-pic.jpg' ) ); $header = true; $handle = fopen( 'data/2019-volleyball-players.csv', 'r' ); while( ( $data = fgetcsv( $handle ) ) !== false ) { + + // skip header if( $header ) { $header = false; continue; } - $n = count( $data ); - - /* - array(11) { - [0]=> - string(2) "id" - [1]=> - string(7) "cognome" - [2]=> - string(4) "nome" - [3]=> - string(7) "Altezza" - [4]=> - string(11) "DataNascita" - [5]=> - string(14) "NazioneNascita" - [6]=> - string(12) "LuogoNascita" - [7]=> - string(19) "NazionalitaSportiva" - [8]=> - string(13) "UltimaSquadra" - [9]=> - string(14) "ultimastagione" - [10]=> - string(7) "urlFoto" - } - */ - - list( $id, $surname, $name, $h, $birth, $birth_nation, $birth_where, $nation, $last_team, $last_season, $url_photo ) = $data; - - if( empty( $url_photo ) ) { - Log::warn( "skip $id $surname $name without URL" ); - continue; - } + // populate arguments + list( + $id, + $surname, + $name, + $h, + $birth, + $birth_nation, + $birth_where, + $nation, + $last_team, + $last_season, + $url_photo + ) = $data; + + // avoid nonsense files $pic = file_get_contents( $url_photo ); if( strlen( $pic ) < 300 ) { Log::warn( "skip $id $surname $name with no pic $url_photo" ); continue; } + // avoid default pic if( md5( $pic ) === $default_pic_md5 ) { Log::warn( "skip $id $surname $name with default pic $url_photo" ); continue; } if( empty( $name ) || empty( $surname ) ) { Log::warn( "skip $id without name or surname" ); continue; } + // check if the page already exists $filename = "$name $surname (Legavolley 2019).jpg"; - -$date = date('Y-m-d H:i:s'); -$text = <<fetch( [ 'action' => 'query', 'prop' => 'info', 'titles' => "File:$filename", ] ); $exists = false; foreach( $response->query as $pages ) { foreach( $pages as $page ) { $exists = empty( $page->missing ); } } - if( !$exists ) { Log::warn( "skip File:$filename already existing" ); continue; } + $date = date('Y-m-d H:i:s'); + $text = require( 'data/content.wiki.php' ); $comment = "Bot: test [[Commons:Bots/Requests/Valerio Bozzolan bot (5)|upload 2019 volleyball players from Legavolley]]"; - $token = $commons->getToken( \mw\Tokens::CSRF ); - $commons->post( [ - 'filename' => $filename, - 'comment' => $comment, - 'text' => $text, - 'url' > $url_photo, - 'token' => $token, - ] ); - exit; + + try { + // send a POST with multipart + $response = $wiki->upload( [ + 'comment' => $comment, + 'text' => $text, + 'filename' => $filename, + ContentDisposition::createFromNameURLType( 'file', $url_photo, 'image/jpg' ), + ] ); + + } catch( \mw\API\PermissionDeniedException $e ) { + Log::error( "permission denied (wtf?): " . $e->getMessage() ); + continue; + } + + // look at the response + $msg = $response->upload->result; + Log::info( "response: $msg" ); + if( isset( $response->upload->warnings ) ) { + print_r( $response->upload->warnings ); + } + + file_put_contents( 'done.log', "$id;$filename;$msg\n", FILE_APPEND ); } diff --git a/2019-05-commons-volleyball-players-upload/include/boz-mw b/2019-05-commons-volleyball-players-upload/include/boz-mw new file mode 160000 index 0000000..afdb79b --- /dev/null +++ b/2019-05-commons-volleyball-players-upload/include/boz-mw @@ -0,0 +1 @@ +Subproject commit afdb79bf2fa48d93d673e40f04e162ae4f4b155b