[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/station/archive/scripts/ -> ripper.php (source)

   1  #!/usr/local/bin/php -q
   2  <?php
   3  
   4  // $Id: ripper.php,v 1.12 2010/09/22 03:21:38 timplunkett Exp $
   5  
   6  /**
   7   * This script is designed to be run by a cron job or windows scheduled task.
   8   * It connects to a webstream, downloads one hours worth and saves it as an
   9   * MP3 for the station archive module to import.
  10   *
  11   *     Y O U   D O N ' T   N E E D   T O   E D I T   T H I S   F I L E !
  12   *       Configuration options are now set in the ripper.inc file.
  13   *
  14   * This script requires that Streamripper (http://streamripper.sourceforge.net/)
  15   * version 1.61.17 or higher be installed (we use the --quite option)
  16   */
  17  
  18  // Make sure we're being run from the command line.
  19  if (isset($_SERVER['REMOTE_ADDR'])) {
  20    exit("This script must be called from the command line.\n");
  21  }
  22  
  23  // Load our settings from the ripper.inc file.
  24  $incfile = realpath(dirname(__FILE__) .'/ripper.inc');
  25  if (!file_exists($incfile)) {
  26    exit("Cannot read the ripper.inc file from {$incfile}.\n");
  27  }
  28  $settings = parse_ini_file($incfile);
  29  
  30  // Check that the import directory is writable.
  31  $import_dir = realpath($settings['import_path']);
  32  if (!is_dir($import_dir) || !is_writable($import_dir)) {
  33    exit("Cannot write to the import directory '{$import_dir}'.\n");
  34  }
  35  
  36  // Make sure we can find stream ripper. The is_executable() test might not work
  37  // with PHP4 and Windows. Upgrade! PHP 5.1 is great.
  38  $streamripper = realpath($settings['streamripper_path']);
  39  if (!file_exists($streamripper) || !is_executable($streamripper)) {
  40    exit("Couldn't find the stream ripper executable at '{$streamripper}'.\n");
  41  }
  42  
  43  // Determine when we're starting, when we should end and convert that to a
  44  // length of time in seconds.
  45  $start_time = round_to_nearest_hour(time());
  46  $end_time = $start_time + 3600;
  47  $length = ($end_time - time()) + (int) $settings['overlap_seconds'];
  48  
  49  // Download the stream
  50  $stream_url = $settings['stream_url'];
  51  $file_format = strtolower($settings['file_format']);
  52  exec("{$streamripper} {$stream_url} -s -d {$import_dir} -A -l {$length} -a {$start_time}.{$file_format} --quiet");
  53  
  54  // stream ripper creates the .cue file. we'll use its absence as a signal
  55  // to the module that it's safe to import a file.
  56  $cuefile = "{$import_dir}/{$start_time}.cue";
  57  if (file_exists($cuefile)) {
  58    unlink($cuefile);
  59  }
  60  
  61  exit(0);
  62  
  63  
  64  /**
  65   * Round a timestamp to the nearest hour.
  66   *
  67   * @param $time
  68   *   A UNIX timestamp
  69   */
  70  function round_to_nearest_hour($time) {
  71    $parts = getdate($time);
  72    if ($parts['minutes'] > 50) {
  73      // advance it to the next hour
  74      return mktime($parts['hours'] + 1, 0, 0, $parts['mon'], $parts['mday'], $parts['year']);
  75    }
  76    else {
  77      // we're late for this hour
  78      return mktime($parts['hours'], 0, 0, $parts['mon'], $parts['mday'], $parts['year']);
  79    }
  80  }
  81  


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7