PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/rename.php

http://github.com/Vaporbook/BookGluttonEpub
PHP | 77 lines | 36 code | 30 blank | 11 comment | 2 complexity | a4b83eac0546bd05dd5ba8623fbc20df MD5 | raw file
  1. <?php
  2. require_once('./BookGluttonEpub.php');
  3. require_once('./BookGluttonZipEpub.php');
  4. /*
  5. Example and test script using the libraries to rename files
  6. according to metadata found in the epub file.
  7. */
  8. $dir = realpath(dirname(__FILE__)).'/epubs/';
  9. $dir = $argv[1];
  10. $ite=new RecursiveDirectoryIterator($dir);
  11. $bytestotal=0;
  12. $nbfiles=0;
  13. foreach (new RecursiveIteratorIterator($ite) as $file=>$cur) {
  14. if(!preg_match('/\.epub$/i',$file)) continue;
  15. $filesize=$cur->getSize();
  16. $bytestotal+=$filesize;
  17. $nbfiles++;
  18. echo "$file => $filesize\n";
  19. try {
  20. $epub = new BookGluttonZipEpub();
  21. $epub->enableLogging();
  22. $epub->loadZip($file);
  23. $title = $epub->getTitle();
  24. $author = $epub->getAuthor();
  25. //$epub->close();
  26. // how you do the actual rename is up to you -- our example
  27. // just echoes what the operation will do:
  28. $newtitle = preg_replace('/[\$\'\\\!\`\~\/\>\<\}\{\@\^\*]/',"","$author - $title".".epub");
  29. echo "rename to ".$newtitle."\n";
  30. if(!is_dir("$dir/$author")) {
  31. mkdir("$dir/$author");
  32. }
  33. rename($file,"$dir/$author/$newtitle");
  34. } catch (Exception $e) {
  35. // BAD FILES go to bad file GHETTO
  36. echo "Exception caught:".$e->getMessage()."\n----------\n";
  37. rename($file,$dir.'/_GHETTO.'.$newtitle);
  38. echo "Moved to ghetto.\n------------\n=============\n";
  39. }
  40. }
  41. $bytestotal=number_format($bytestotal);
  42. echo "Total: $nbfiles files, $bytestotal bytes\n";
  43. exit();
  44. ?>