Advertisement
kwasinski

video_scraper

Aug 21st, 2015
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.88 KB | None | 0 0
  1. <?php //author Vinicius Kwasinski:: [email protected]
  2. ///////////////////////////////
  3. // Check all directories recursively
  4. // and search for videos files
  5. // with extensions defined in the data structure
  6. ///////////////////////////////
  7.  
  8. if(isset($_GET['filename']) && isset($_GET['ext'])) {
  9.     header("Content-disposition: attachment; filename=" . $_GET['filename']);
  10.     header("Content-type: application/{$_GET['ext']}");
  11.     readfile($_GET['filename']);
  12. }
  13.  
  14. $scrap_structure = array(
  15.     'ext' => array('mov', 'avi', 'mpeg', 'divx'),           //availabe video extensions
  16.     'basefolders_to_ignore' => array('video_scraper_img'),  //add here folders that contains files used by video_scraper
  17.     'exclude_paths' => '/^[^\.]/',              //regExp to exclude original folder's path
  18.     'get_extension' => '/\.(\w\w\w\w?)$/',      //regExp to catch file extension
  19. //var_dump($scrap_structurevideo_scraper_imgvideofolder.png
  20. );
  21.  
  22. list_dirs();
  23. //var_dump($scrap_structurevideo_scraper_img/folder.png
  24.  
  25. ?>
  26. <html>
  27.     <head>
  28.         <title>Video Scraper 0.1</title>
  29.         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  30.         <style type="text/css">
  31.             .content h2 { width: 540px; margin: 0 auto;}
  32.             li { cursor: pointer; list-style-type: none; }
  33.             .tree li span, .tree li a { position: relative; bottom: 10px;}
  34.             .tree { display: none; position: relative; left: 30px; top: 10px; }
  35.             .content { width: 1024px; margin: 0 auto; border: 1px solid black; padding: 20px; }
  36.             .indicator img { width: 17px; position: relative; top: 3px; }
  37.         </style>
  38.     </head>
  39.     <body>
  40.         <div class="content">
  41.             <h2>Lista de todos os vídeos dentro dessas pastas</h2>
  42.             <p>Clique na(s) pasta(s) abaixo para mostrar o(s) video(s) que cada uma contém ou realize uma busca.</p>
  43.  
  44.             <ul>
  45.             <?php
  46.                 $i = 0; if($scrap_structure['paths'] & $paths = $scrap_structure['paths'])
  47.                 foreach ($paths as $path_name => $keys): $i++; ?>
  48.                 <li <?= "class=\"clicklabe_$i\""?>> <span class="indicator"><img src="video_scraper_img/close.png" alt=""></span> <?= $path_name  == '.'? 'Raiz': $path_name; ?> -- Arquivos:<?= array_pop($paths[$path_name]) ?>
  49.                 <?php foreach ($paths[$path_name] as $filename): ?>
  50.                     <ul class="tree">
  51.                         <?php if(file_check($filename, true)): ?>
  52.                         <li style="list-style-image: url('video_scraper_img/video.png')">
  53.                             <a href="index.php?filename=<?= "$path_name/$filename"?>&ext=<?= file_check($filename, true) ?>"><?= $filename ?></a>
  54.                         </li>
  55.                         <?php else: ?>
  56.                         <li style="list-style-image: url('video_scraper_img/folder.png')">
  57.                             <span><?= $filename ?></span>
  58.                         </li>
  59.                         <?php endif; ?>
  60.                     </ul>
  61.                 <?php endforeach; ?>
  62.                 </li>
  63.                 <br/>
  64.             <?php endforeach; ?>
  65.             </ul>
  66.  
  67.         </div>
  68.     </body>
  69. </html>
  70. <script type="text/javascript">
  71.     jQuery(document).ready(function($) {
  72.         var image = {
  73.             open: 'open.png',
  74.             close: 'close.png',
  75.         }
  76.  
  77.         $("li[class^='clicklabe_']").click(function(){
  78.             var $this = $(this);
  79.             var file_tree = $this.find('ul.tree');
  80.  
  81.             file_tree.toggle('fast');
  82.  
  83.             //Swap the open <-> close img;
  84.             var img_src = $this.find('.indicator > img'),
  85.             img_src_attr = img_src.attr('src').match(/(\w+)\.\w{3,3}$/)[0]? RegExp.$1: null;
  86.             var replace = img_src.attr('src').replace(/(\w+)\.\w{3,3}$/, img_src_attr == 'close'? 'open.png': 'close.png');
  87.             img_src.attr('src', replace);
  88.  
  89.         });
  90.     });
  91. </script>
  92.  
  93. <?php
  94. function list_dirs($path = false) {
  95.     $base_path = $path? $path: '.';
  96.  
  97.     global $scrap_structure;
  98.  
  99.     $directories = sanitize_dir(scandir($base_path));
  100.  
  101.     foreach ($directories as $path) {
  102.         $full_filename = "$base_path/$path";
  103.  
  104.         if (in_array($path, $scrap_structure['basefolders_to_ignore']))  continue;
  105.         if (is_dir($full_filename))
  106.           list_dirs($full_filename)  & $scrap_structure['paths'][$base_path][] = $full_filename;
  107.         if (!file_check($path))  continue;
  108.  
  109.         $scrap_structure['paths'][$base_path][] = $path;
  110.  
  111.     }
  112.     $scrap_structure['paths'][$base_path]['n_files'] = count($directories);
  113.     ksort($scrap_structure['paths']);
  114. }
  115.  
  116. // exclude [.] && [..] and hidden linux files [.filename]
  117. function sanitize_dir($arr_dir) {
  118.     if(!$arr_dir)  return;
  119.  
  120.     global $scrap_structure;
  121.  
  122.     $to_exclude = preg_grep($scrap_structure['exclude_paths'], $arr_dir, PREG_GREP_INVERT);
  123.     foreach ($to_exclude as $key => $value)
  124.         if ($to_exclude[$key] == $arr_dir[$key] || !file_check($arr_dir[$key], true)) unset($arr_dir[$key]);
  125.  
  126.     return $arr_dir;
  127. }
  128.  
  129. // Check if the filename has a video extension if not add the path with a value [vazio]
  130. // @returns $get_ext? bool: extension
  131. function file_check($filename, $get_ext = false) {
  132.     if(!$filename)  return false;
  133.  
  134.     global $scrap_structure;
  135.  
  136.     $ext = preg_match($scrap_structure['get_extension'], $filename , $_)? $_[1]: '';
  137.     if (!empty($ext)) $is_a_video = in_array($ext, $scrap_structure['ext'])? true: false;
  138.     if (isset($is_a_video)) return !$get_ext? $is_a_video: $_[1];
  139.  
  140.     return false;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement