Skip to content

Instantly share code, notes, and snippets.

@thinsoldier
Created December 6, 2016 09:18
Show Gist options
  • Save thinsoldier/0be3df74267d22f0c8c4177d57673da8 to your computer and use it in GitHub Desktop.
Save thinsoldier/0be3df74267d22f0c8c4177d57673da8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
// 2016-12-06 github.com/thinsoldier
error_reporting(E_ALL);
//-----------------------------
if( !isset($argv[1]) ) {
echo "CODEPEN PEN URL MUST BE PROVIDED!";
EXIT;
}
else { action_pen(); }
//-----------------------------
function action_pen()
{
global $argv;
$parsed = util_parsePenURL( $argv[1] );
// setup .gitignore file based on pen id
util_git_init();
// setup .gitignore file based on pen id
util_create_gitignore( $parsed['id'] );
$downloaded = util_download_trinity( $parsed['url'] );
if( $downloaded ){ util_commit_all(); }
echo "\n";
}
function util_git_init()
{
if( !file_exists('./.git/') )
{
echo 'Initialized git repository.';
exec('git init');
}
}
function util_parsePenURL( $url )
{
$output = array();
$format = "http://codepen.io/%s/pen/%s";
//codepen.io/gebrutommy/pen/LbQGBQ
$parts = explode('/',$url);
#print_r($parts);
// Find the key of the "pen" entry...
$literal_word_pen_slot = array_search('pen', $parts);
// ...the following entry will be the pen id...
$output['id'] = $parts[ $literal_word_pen_slot + 1 ];
// ...the preceding entry will be the username...
$output['user'] = $parts[ $literal_word_pen_slot - 1 ];
// Combine the values with $format to make a proper url.
$output['url'] = sprintf( $format, $output['user'], $output['id']);
#print_r($output);
return $output;
}
function util_create_gitignore($penID)
{
$ignFile = './.gitignore';
if( file_exists( $ignFile ) )
{ echo ".gitignore file already exists\n"; return; }
$contents = "*
!.gitignore
!$penID.html
!$penID.css
!$penID.js
";
file_put_contents($ignFile, $contents);
echo "created .gitignore file for pen $penID\n";
}
function util_download_trinity( $penURL )
{
//$penURL = $penURL.'.css';
echo "\n\n";
echo "Attempting to download html, css, and js of $penURL\n\n";
$command = <<<xterminal
unset DYLD_LIBRARY_PATH
curl -Ok $penURL.html
curl -Ok $penURL.css
curl -Ok $penURL.js
2>&1
xterminal;
exec( $command );
return true;
}
function util_commit_all()
{
$now = date("Y-m-d H:i:s");
$cmdCOMMIT = <<<xterminal
unset DYLD_LIBRARY_PATH
git add .
git commit -m "Codepen Auto Commit $now" \
2>&1
xterminal;
$output = array();
exec( $cmdCOMMIT, $output );
echo implode("\n",$output);
}
exit;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment