Skip to content

Instantly share code, notes, and snippets.

@martinleblanc
Last active August 18, 2021 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinleblanc/d1c3ac30317425c611925e27815e436c to your computer and use it in GitHub Desktop.
Save martinleblanc/d1c3ac30317425c611925e27815e436c to your computer and use it in GitHub Desktop.
icon-download.php
<?php
function prettyPrint( $json )
{
$result = '';
$level = 0;
$in_quotes = false;
$in_escape = false;
$ends_line_level = NULL;
$json_length = strlen( $json );
for( $i = 0; $i < $json_length; $i++ ) {
$char = $json[$i];
$new_line_level = NULL;
$post = "";
if( $ends_line_level !== NULL ) {
$new_line_level = $ends_line_level;
$ends_line_level = NULL;
}
if ( $in_escape ) {
$in_escape = false;
} else if( $char === '"' ) {
$in_quotes = !$in_quotes;
} else if( ! $in_quotes ) {
switch( $char ) {
case '}': case ']':
$level--;
$ends_line_level = NULL;
$new_line_level = $level;
break;
case '{': case '[':
$level++;
case ',':
$ends_line_level = $level;
break;
case ':':
$post = " ";
break;
case " ": case "\t": case "\n": case "\r":
$char = "";
$ends_line_level = $new_line_level;
$new_line_level = NULL;
break;
}
} else if ( $char === '\\' ) {
$in_escape = true;
}
if( $new_line_level !== NULL ) {
$result .= "\n".str_repeat( "\t", $new_line_level );
}
$result .= $char.$post;
}
return $result;
}
?>
<?php
error_reporting(E_ALL & ~E_NOTICE);
$DEVELOPMENT = false;
if($DEVELOPMENT) {
// For iconfinder.dev
$URL_LIST_FILE = "/Users/martinleblanceigtved//Dropbox (Iconfinder)/Iconfinder (Dropbox Business)/Extended library/Concept Draw/Business and Finance/urls_dev.txt";
$TARGET_FOLDER = "/Users/martinleblanceigtved//Dropbox (Iconfinder)/Iconfinder (Dropbox Business)/Extended library/Concept Draw/Business and Finance/";
$API_DOMAIN = "http://iconfinder.dev";
$AUTH_URL = "http://iconfinder.dev/api/v2/oauth2/token";
$CLIENT_ID = "llNzOTC3jmC1vj9AQwo2jGMn7uLZSDMlnLFUt4a4c7b5L4EqGuHH1xlZ8tm2nDDM";
$CLIENT_SECRET = "DK2vqwfrYrcKXZprF3VSbVlqXBcWJa5QMwklEuIJVLFoJw8rrq70FRemUz5aWF2m";
}
else {
// For iconfinder.com
// Edit these for the different 'groups' of icons the client wants.
$URL_LIST_FILE = "/Users/martinleblanceigtved//Dropbox (Iconfinder)/Iconfinder (Dropbox Business)/Extended library/Concept Draw/Business and Finance/urls.txt";
$TARGET_FOLDER = "/Users/martinleblanceigtved//Dropbox (Iconfinder)/Iconfinder (Dropbox Business)/Extended library/Concept Draw/Business and Finance/";
$API_DOMAIN = "https://api.iconfinder.com";
$AUTH_URL = "https://www.iconfinder.com/api/v2/oauth2/token";
$CLIENT_ID = "123";
$CLIENT_SECRET = "456";
}
// TODO:
// If the icon does not have an SVG version, it still downloads a file without extension
// If the icon is removed from the website, it still downloads a file without extension
// If the "icons" folder is missing, the script does not exit or create it
// If duplicate is found, the script exits
// If an icon is not found, the script exits
?>
<?php
/**
* Send a POST requst using cURL
* @param string $CLIENT_ID the client ID
* @param string $CLIENT_SECRET the client secret
* @return string
*/
function get_token($CLIENT_ID, $CLIENT_SECRET, $AUTH_URL) {
// Request token
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $AUTH_URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=jwt_bearer&client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET");
$result = curl_exec($ch);
curl_close($ch);
// Prepare response
header('Content-type: application/json');
// return JWT to requesting app
if($result == FALSE) {
// Something went wrong.
echo json_encode(array("error" => "CURL request failed."));
return false;
}
else {
$result_decoded = json_decode($result);
if(array_key_exists("error", $result_decoded) or !array_key_exists("access_token", $result_decoded)) {
// Use this for additional error handling.
return $result;
}
else {
// Print successful response.
return $result;
}
}
}
?>
<?php
/**
* Send a GET requst using cURL
* @param string $url to request
* @param array $get values to send
* @param array $options for cURL
* @return string
*/
function curl_get($url,
array $get = NULL,
array $headers = array()) {
$ch = curl_init();
// Set URL.
$url = $url . (strpos($url, '?') === FALSE ? '?' : '') . http_build_query($get);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(!$result = curl_exec($ch)) {
trigger_error(curl_error($ch));
}
curl_close($ch);
return $result;
}
?>
<?php
include("settings.php");
include("token.php");
include("helpers/curl.php");
// Get token.
$token = json_decode(get_token($CLIENT_ID, $CLIENT_SECRET, $AUTH_URL));
if(sizeof($token->access_token) == 0) {
echo "Error: Token not generated correctly.";
exit();
}
// Set headers.
$headers = array();
$headers[] = 'Authorization: JWT ' . $token->access_token;
$headers[] = 'Origin: ' . 'http://www.example.com';
// Get URLs from file.
foreach (file($URL_LIST_FILE) as $url) {
// Ignore lines beginning with #.
if($url[0] == "#") continue;
$urls[] = $url;
}
// Set get parameters.
$get_parameters = array();
// Create array to store information about designers.
$designers = array();
$downloaded_icons = array();
$icons = 0;
// Loop through all our URLs.
foreach ($urls as $url) {
// Print process output.
system('clear');
echo $output;
echo round(($icons / sizeof($urls)) * 100) . "% complete. \n";
$output = "";
$url = trim($url);
// Create the URL for the icon details endpoint by using the icon id.
preg_match('/\/.\d*\//', $url, $icon_id_with_slashes);
$icon_id = str_replace("/", "", $icon_id_with_slashes[0]);
$url = $API_DOMAIN . "/v2/icons/" . $icon_id;
if(in_array($icon_id, $downloaded_icons)) {
echo "Error: Duplicate icon id found: " . $icon_id;
exit();
}
// Imcrement counter of icons.
$icons++;
// Check if the URL is valid.
if(filter_var($url, FILTER_VALIDATE_URL) !== false) {
$result = curl_get($url, $get_parameters, $headers);
$json_result = json_decode($result);
}
else {
echo "URL not valid: " . $url;
exit();
}
// Handle possible error messages.
if(property_exists($json_result, 'message')) {
echo $json_result->message . "\n";
exit();
}
// Keep track of who the designers of the icons are
// add the prices.
if($json_result->iconset->author->user_id != "") {
if($json_result->is_premium) {
$designers[$json_result->iconset->author->user_id] += $json_result->prices[0]->price;
}
else {
// Manually append the price of zero if icon is free
// in order to have the designer in the array.
$designers[$json_result->iconset->author->user_id] += 0;
}
}
else {
// Handle cases where the icons are coming from a designer
// who is not registered.
$designers['Not a registered designer'] += 0;
}
// Save the SVG version
$download_url = $API_DOMAIN .'/v2'. $json_result->vector_sizes[0]->formats[0]->download_url;
$download_format = $json_result->vector_sizes[0]->formats[0]->format;
$file_name = $TARGET_FOLDER . "icons/" . $icon_id . "." . $download_format;
$downloaded_icons[] = $icon_id;
// Skip the last part if the file has already been downloaded.
if(file_exists($file_name)) {
$output = "File already exists: " . $file_name . "\n";
continue;
}
// Check to see if the download URL is correct.
if(filter_var($download_url, FILTER_VALIDATE_URL) === false) {
echo "Error: Invalid URL.";
exit();
}
// Read the icon data from the API.
try{
$icon_data = curl_get($download_url, $get_parameters, $headers);
$output .= "Downloading: " . $download_url . "\n";
try {
// Write icon data to file.
$file = fopen($file_name, "w");
fwrite($file, $icon_data);
$output .= "Writing to file: " . $file_name . "\n";
}
catch(Exception $e) {
echo 'Caught exception while writing to file: ', $e->getMessage(), "\n";
exit();
}
}
catch(Exception $e) {
echo 'Caught exception while reading file: ', $e->getMessage(), "\n";
exit();
}
}
// Perform a check to see if we have looped through all icons.
if($icons != sizeof($urls)) {
echo "Error: Icons count and URLs does not match.\nIcons: " . $icons . " and size of URLs list: " . sizeof($urls);
}
else {
$file_content = "Summary:\n\n";
$file_content = $icons . " icons were downloaded.\n\n";
foreach ($designers as $key => $value) {
$file_content .= $key . ": " . $value . "\n";
}
$file = fopen($TARGET_FOLDER . "summary.txt", "w");
if(fwrite($file, $file_content))
echo "\n\nWriten to summary.txt:\n" . $file_content;
else
echo "Error: Failed writing summary to " . $TARGET_FOLDER . "summary.txt";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment