Skip to content

Instantly share code, notes, and snippets.

@unapersona
Created February 21, 2020 10:40
Show Gist options
  • Save unapersona/c71021de281855d83de3ca8bb967e02c to your computer and use it in GitHub Desktop.
Save unapersona/c71021de281855d83de3ca8bb967e02c to your computer and use it in GitHub Desktop.
Generación de csv para consolidar acreditaciones
<?php
/*
* Generate a CSV for InDesign with attendee info and Gravatars
*
* See http://plan.wordcamp.org/helpful-documents-and-templates/create-wordcamp-badges-with-gravatars/ for instructions
*
* input is a CSV export of CampTix attendees. Format is: "First Name","Last Name","E-mail Address","Twitter Username"
* the script downloads the attendee's Gravatars, and adds a column to the CSV with the filename of the image
* the CSV can then be used by InDesign to generate wordcamp badges with the attendee's gravatar
*
* make sure you update the input/output file paths before running
*/
// Configuration
$gravatar_dir = __DIR__ . '/gravatar/';
$original_csv = fopen( 'badges-input.csv', 'r' );
$final_csv = fopen( 'badges-output.csv', 'w' );
$destination_directory = 'C:\Users\User\gravatar\\';
$replace_empty_twitter = false;
$missing_gravatars = array();
$output_headers = array( 'First Name', 'Last Name', 'Twitter', '@Gravatar' ); // The Gravatar column is prefixed with a @ to let InDesign know that it contains images
function remove_accents($str){
return strtr($str, array(
// Decompositions for Latin-1 Supplement
'ª' => 'a', 'º' => 'o',
'À' => 'A', 'Á' => 'A',
'Â' => 'A', 'Ã' => 'A',
'Ä' => 'A', 'Å' => 'A',
'Æ' => 'AE','Ç' => 'C',
'È' => 'E', 'É' => 'E',
'Ê' => 'E', 'Ë' => 'E',
'Ì' => 'I', 'Í' => 'I',
'Î' => 'I', 'Ï' => 'I',
'Ð' => 'D', 'Ñ' => 'N',
'Ò' => 'O', 'Ó' => 'O',
'Ô' => 'O', 'Õ' => 'O',
'Ö' => 'O', 'Ù' => 'U',
'Ú' => 'U', 'Û' => 'U',
'Ü' => 'U', 'Ý' => 'Y',
'Þ' => 'TH','ß' => 's',
'à' => 'a', 'á' => 'a',
'â' => 'a', 'ã' => 'a',
'ä' => 'a', 'å' => 'a',
'æ' => 'ae','ç' => 'c',
'è' => 'e', 'é' => 'e',
'ê' => 'e', 'ë' => 'e',
'ì' => 'i', 'í' => 'i',
'î' => 'i', 'ï' => 'i',
'ð' => 'd', 'ñ' => 'n',
'ò' => 'o', 'ó' => 'o',
'ô' => 'o', 'õ' => 'o',
'ö' => 'o', 'ø' => 'o',
'ù' => 'u', 'ú' => 'u',
'û' => 'u', 'ü' => 'u',
'ý' => 'y', 'þ' => 'th',
'ÿ' => 'y', 'Ø' => 'O',
// Decompositions for Latin Extended-A
'Ā' => 'A', 'ā' => 'a',
'Ă' => 'A', 'ă' => 'a',
'Ą' => 'A', 'ą' => 'a',
'Ć' => 'C', 'ć' => 'c',
'Ĉ' => 'C', 'ĉ' => 'c',
'Ċ' => 'C', 'ċ' => 'c',
'Č' => 'C', 'č' => 'c',
'Ď' => 'D', 'ď' => 'd',
'Đ' => 'D', 'đ' => 'd',
'Ē' => 'E', 'ē' => 'e',
'Ĕ' => 'E', 'ĕ' => 'e',
'Ė' => 'E', 'ė' => 'e',
'Ę' => 'E', 'ę' => 'e',
'Ě' => 'E', 'ě' => 'e',
'Ĝ' => 'G', 'ĝ' => 'g',
'Ğ' => 'G', 'ğ' => 'g',
'Ġ' => 'G', 'ġ' => 'g',
'Ģ' => 'G', 'ģ' => 'g',
'Ĥ' => 'H', 'ĥ' => 'h',
'Ħ' => 'H', 'ħ' => 'h',
'Ĩ' => 'I', 'ĩ' => 'i',
'Ī' => 'I', 'ī' => 'i',
'Ĭ' => 'I', 'ĭ' => 'i',
'Į' => 'I', 'į' => 'i',
'İ' => 'I', 'ı' => 'i',
'IJ' => 'IJ','ij' => 'ij',
'Ĵ' => 'J', 'ĵ' => 'j',
'Ķ' => 'K', 'ķ' => 'k',
'ĸ' => 'k', 'Ĺ' => 'L',
'ĺ' => 'l', 'Ļ' => 'L',
'ļ' => 'l', 'Ľ' => 'L',
'ľ' => 'l', 'Ŀ' => 'L',
'ŀ' => 'l', 'Ł' => 'L',
'ł' => 'l', 'Ń' => 'N',
'ń' => 'n', 'Ņ' => 'N',
'ņ' => 'n', 'Ň' => 'N',
'ň' => 'n', 'ʼn' => 'n',
'Ŋ' => 'N', 'ŋ' => 'n',
'Ō' => 'O', 'ō' => 'o',
'Ŏ' => 'O', 'ŏ' => 'o',
'Ő' => 'O', 'ő' => 'o',
'Œ' => 'OE','œ' => 'oe',
'Ŕ' => 'R','ŕ' => 'r',
'Ŗ' => 'R','ŗ' => 'r',
'Ř' => 'R','ř' => 'r',
'Ś' => 'S','ś' => 's',
'Ŝ' => 'S','ŝ' => 's',
'Ş' => 'S','ş' => 's',
'Š' => 'S', 'š' => 's',
'Ţ' => 'T', 'ţ' => 't',
'Ť' => 'T', 'ť' => 't',
'Ŧ' => 'T', 'ŧ' => 't',
'Ũ' => 'U', 'ũ' => 'u',
'Ū' => 'U', 'ū' => 'u',
'Ŭ' => 'U', 'ŭ' => 'u',
'Ů' => 'U', 'ů' => 'u',
'Ű' => 'U', 'ű' => 'u',
'Ų' => 'U', 'ų' => 'u',
'Ŵ' => 'W', 'ŵ' => 'w',
'Ŷ' => 'Y', 'ŷ' => 'y',
'Ÿ' => 'Y', 'Ź' => 'Z',
'ź' => 'z', 'Ż' => 'Z',
'ż' => 'z', 'Ž' => 'Z',
'ž' => 'z', 'ſ' => 's',
// Decompositions for Latin Extended-B
'Ș' => 'S', 'ș' => 's',
'Ț' => 'T', 'ț' => 't',
// Euro Sign
'€' => 'E',
// GBP (Pound) Sign
'£' => '',
// Vowels with diacritic (Vietnamese)
// unmarked
'Ơ' => 'O', 'ơ' => 'o',
'Ư' => 'U', 'ư' => 'u',
// grave accent
'Ầ' => 'A', 'ầ' => 'a',
'Ằ' => 'A', 'ằ' => 'a',
'Ề' => 'E', 'ề' => 'e',
'Ồ' => 'O', 'ồ' => 'o',
'Ờ' => 'O', 'ờ' => 'o',
'Ừ' => 'U', 'ừ' => 'u',
'Ỳ' => 'Y', 'ỳ' => 'y',
// hook
'Ả' => 'A', 'ả' => 'a',
'Ẩ' => 'A', 'ẩ' => 'a',
'Ẳ' => 'A', 'ẳ' => 'a',
'Ẻ' => 'E', 'ẻ' => 'e',
'Ể' => 'E', 'ể' => 'e',
'Ỉ' => 'I', 'ỉ' => 'i',
'Ỏ' => 'O', 'ỏ' => 'o',
'Ổ' => 'O', 'ổ' => 'o',
'Ở' => 'O', 'ở' => 'o',
'Ủ' => 'U', 'ủ' => 'u',
'Ử' => 'U', 'ử' => 'u',
'Ỷ' => 'Y', 'ỷ' => 'y',
// tilde
'Ẫ' => 'A', 'ẫ' => 'a',
'Ẵ' => 'A', 'ẵ' => 'a',
'Ẽ' => 'E', 'ẽ' => 'e',
'Ễ' => 'E', 'ễ' => 'e',
'Ỗ' => 'O', 'ỗ' => 'o',
'Ỡ' => 'O', 'ỡ' => 'o',
'Ữ' => 'U', 'ữ' => 'u',
'Ỹ' => 'Y', 'ỹ' => 'y',
// acute accent
'Ấ' => 'A', 'ấ' => 'a',
'Ắ' => 'A', 'ắ' => 'a',
'Ế' => 'E', 'ế' => 'e',
'Ố' => 'O', 'ố' => 'o',
'Ớ' => 'O', 'ớ' => 'o',
'Ứ' => 'U', 'ứ' => 'u',
// dot below
'Ạ' => 'A', 'ạ' => 'a',
'Ậ' => 'A', 'ậ' => 'a',
'Ặ' => 'A', 'ặ' => 'a',
'Ẹ' => 'E', 'ẹ' => 'e',
'Ệ' => 'E', 'ệ' => 'e',
'Ị' => 'I', 'ị' => 'i',
'Ọ' => 'O', 'ọ' => 'o',
'Ộ' => 'O', 'ộ' => 'o',
'Ợ' => 'O', 'ợ' => 'o',
'Ụ' => 'U', 'ụ' => 'u',
'Ự' => 'U', 'ự' => 'u',
'Ỵ' => 'Y', 'ỵ' => 'y',
// Vowels with diacritic (Chinese, Hanyu Pinyin)
'ɑ' => 'a',
// macron
'Ǖ' => 'U', 'ǖ' => 'u',
// acute accent
'Ǘ' => 'U', 'ǘ' => 'u',
// caron
'Ǎ' => 'A', 'ǎ' => 'a',
'Ǐ' => 'I', 'ǐ' => 'i',
'Ǒ' => 'O', 'ǒ' => 'o',
'Ǔ' => 'U', 'ǔ' => 'u',
'Ǚ' => 'U', 'ǚ' => 'u',
// grave accent
'Ǜ' => 'U', 'ǜ' => 'u',
));
}
function sanitize_file_name($str){
return strtr( remove_accents($str) , array(
'"' => '',
' ' => '_'
));
}
echo "\nRunning...";
if ( $original_csv && $final_csv ) {
$row = -1;
$gravatar_file = null;
fputcsv( $final_csv, $output_headers );
while ( ( $attendee = fgetcsv( $original_csv, 1000, "," ) ) !== FALSE ) {
$row++;
if ( $row == 0 ) {
continue; // skip header
}
foreach ( $attendee as $key => $value ) {
$attendee[ $key ] = trim( $value );
}
// Create empty badges for unknown attendees
if ( '[email protected]' == $attendee[2] ) {
$attendee[0] = $attendee[1] = $attendee[2] = $attendee[3] = '';
}
// Capitalize the first and last names so they look better
// todo if there's more than one word in either field, don't do anything b/c could be something like "Ines van Essen" and they want the "van" lowercase
$attendee[0] = ucwords( mb_strtolower( $attendee[0] ) );
$attendee[1] = ucwords( mb_strtolower( $attendee[1] ) );
// If they don't have a Twitter handle, add their first name instead. Add a @ to Twitter handles to avoid confusing them with first names.
if ( empty ( $attendee[3] ) ) {
if ( $replace_empty_twitter ) {
$attendee[3] = $attendee[0];
}
} else {
// Strip out everything but the username, and prefix a @
$attendee[3] = '@' . preg_replace(
'/
(https?:\/\/)?
(twitter\.com\/)?
(@)?
/ix',
'',
trim($attendee[3], "'")
);
}
// Download the Gravatar
$filename = 'default.png';
if ( $attendee[2] ) {
$md5 = md5( strtolower( trim( $attendee[2] ) ) );
$avatar = @file_get_contents( "http://www.gravatar.com/avatar/{$md5}.jpg?s=512&default=404" );
if(!$avatar && $attendee[3]){
echo "\n Buscamos el twitter de {$attendee[0]}... ";
$avatar = @file_get_contents("https://avatars.io/twitter/{$attendee[3]}/large");
echo ($avatar ? 'ok' : 'ko');
}
if ( $avatar !== false ) {
$prefix = str_pad($row, 3, '0', STR_PAD_LEFT);
$filename = strtolower( $attendee[0] . ' ' . $attendee[1] );
$filename = sanitize_file_name( $prefix . '-' . $filename . '.jpg' );
$gravatar_filename = $gravatar_dir . $filename;
$gravatar_file = fopen( $gravatar_filename, 'w' );
fwrite( $gravatar_file, $avatar);
fclose( $gravatar_file );
//le cambiamos la extensión si no es jpg
$new_filename = false;
switch(exif_imagetype($gravatar_filename)){
case IMAGETYPE_PNG:
$new_filename = str_replace('.jpg', '.png', $filename);
break;
case IMAGETYPE_GIF:
$new_filename = str_replace('.jpg', '.gif', $filename);
break;
}
if($new_filename){
rename($gravatar_filename, str_replace($filename, $new_filename, $gravatar_filename));
$filename = $new_filename;
}
}
}
if ( 'default.png' === $filename ) {
$missing_gravatars[] = $attendee[0] .' '. $attendee[1];
}
// Remove the e-mail address from the output file, because it's not used in the badge and InDesign complains about unused fields
unset( $attendee[2] );
// Update the final CSV
$attendee['filename'] = $destination_directory . $filename;
fputcsv( $final_csv, $attendee );
// Output progress
if ( 0 == $row % 50 ) {
echo "\nProcessed " . $row . " attendees.";
}
// Limit to best case of ~10 iterations per second, to avoid flooding gravatar.com or draining the host server
usleep( 100000 );
}
fclose( $original_csv );
fclose( $final_csv );
$status = "\nFinished processing " . $row . " attendees.\n";
if ( $missing_gravatars ) {
$status .= "\nThe following ".count($missing_gravatars)." people did not have a Gravatar:\n\n";
$status .= implode( "\n", $missing_gravatars );
}
} else {
$status = "Error: Couldn't open files.";
}
echo "\n". $status ."\n\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment