Difference between revisions of "Anri-chan/Source/df.php"

From SDA Knowledge Base

Jump to: navigation, search
(grenola's and my changes)
 
Line 1: Line 1:
<pre><nowiki><?php
+
<pre><nowiki>
 +
<?php
  
  
 
// Settings
 
// Settings
 
$df_path = 'http://speeddemosarchive.com/kb/index.php/DF'; // Path to sda/kb/DB
 
$df_path = 'http://speeddemosarchive.com/kb/index.php/DF'; // Path to sda/kb/DB
$titles_path = 'dfnd_titles.bat'; // Path where the titles file will be written
+
$titles_path = 'dfnd_titles_temp.bat'; // Path where the titles file will be written
$data_path = 'dfnd_data.bat'; // Path where the data file will be written
+
$data_path = 'dfnd_data_temp.bat'; // Path where the data file will be written
  
  
Line 23: Line 24:
  
 
if ( ($pos1 === false) or ($pos2 === false) or ($pos1 > $pos2) )
 
if ( ($pos1 === false) or ($pos2 === false) or ($pos1 > $pos2) )
die('DF data does not exist in this page.');
+
    die('DF data does not exist in this page.');
+
   
 
$raw = substr($raw, $pos1, $pos2 - $pos1 - 1);
 
$raw = substr($raw, $pos1, $pos2 - $pos1 - 1);
  
Line 30: Line 31:
  
 
$k = 0;
 
$k = 0;
for ($i = 1; $t[$i]; $i++)  
+
$ct = count($t);
 +
for ($i = 1; $i < $ct; $i++)  
 
{
 
{
$t2 = substr( $t[$i], strpos($t[$i], '</td>') );
+
    $t2 = substr( $t[$i], strpos($t[$i], '</td>') );
if ( preg_match('/D([14])/', $t2, $m) )
+
    if ( preg_match('/D([14])/', $t2, $m) )
$data[$k]['d'] = $m[1];
+
        $data[$k]['d'] = $m[1];
if ( preg_match('/F([123])/', $t2, $m) )
+
    if ( preg_match('/F([123])/', $t2, $m) )
$data[$k]['f'] = $m[1];
+
        $data[$k]['f'] = $m[1];
if ( preg_match('/([23])D/', $t2, $m) )
+
    if ( preg_match('/([23])D/', $t2, $m) )
$data[$k]['twod'] = ($m[1] == 2) ? 'y' : 'n';
+
        $data[$k]['twod'] = ($m[1] == 2) ? 'y' : 'n';
if ( is_array($data[$k]) )
+
    if ( is_array($data[$k]) )
{
+
    {
$title[$k] = str_replace(
+
        $title[$k] = str_replace(
array( ':', '&', '<', '>', '(', ')', 'é' ),
+
            array( ':', '&', '<', '>', '(', ')', 'é' ),
array( ' -', 'and', '[', ']', '[', ']', 'e' ),
+
            array( ' -', 'and', '[', ']', '[', ']', 'e' ),
html_entity_decode( substr( $t[$i], 0, strpos($t[$i], '</td>') ) )
+
            html_entity_decode( substr( $t[$i], 0, strpos($t[$i], '</td>') ) )
);
+
        );
$initial[$k] = strtolower($title[$k]{$j});
+
        $initial[$k] = strtolower($title[$k]{0});
if ($initial[$k] == '.') $initial[$k] = 'd'; // because .hack disagrees with Spock's philosophy.
+
        if ($initial[$k] == '.') $initial[$k] = 'd'; // because .hack disagrees with Spock's philosophy.
$k++;
+
        $k++;
}
+
    }
 
}
 
}
  
 
if (!is_array($title))
 
if (!is_array($title))
die('DF data does not exist in this page.');
+
    die('DF data does not exist in this page.');
  
 
$out1 = $out2 = "@ECHO OFF\r\n";
 
$out1 = $out2 = "@ECHO OFF\r\n";
  
for ($i = 0; $title[$i]; $i++)
+
$cur_initial = '';
 +
$ct = count($title);
 +
for ($i = 0; $i < $ct; $i++)
 
{
 
{
if ($initial[$i] == $cur_initial)  
+
    if ($initial[$i] == $cur_initial)  
$j++;
+
        $j++;
else
+
    else
{
+
    {
$j = 1;
+
        $j = 1;
$cur_initial = $initial[$i];
+
        $cur_initial = $initial[$i];
if ($out1) $out1 .= ")\r\n";
+
        if ($out1) $out1 .= ")\r\n";
$out1 .= "IF [%dfnd_letter%]==[$cur_initial] (\r\n";
+
        $out1 .= "IF [%dfnd_letter%]==[$cur_initial] (\r\n";
}
+
    }
$number[$i] = sprintf('%03d', $j);
+
    $number[$i] = sprintf('%03d', $j);
$title[$i] = preg_replace('/[:&|><\(\)]/', '^\\0', $title[$i]);
+
    $title[$i] = preg_replace('/[:&|><\(\)]/', '^\\0', $title[$i]);
$out1 .= "ECHO {$initial[$i]}{$number[$i]}: {$title[$i]}\r\n";
+
    $out1 .= "ECHO {$initial[$i]}{$number[$i]}: {$title[$i]}\r\n";
$out2 .= "IF [%s%]==[{$initial[$i]}{$number[$i]}] (\r\nCALL :out_info Selected \"{$title[$i]}\"\r\n";
+
    $out2 .= "IF [%s%]==[{$initial[$i]}{$number[$i]}] (\r\nCALL :out_info Selected \"{$title[$i]}\"\r\n";
foreach ($data[$i] as $k => $v)
+
    foreach ($data[$i] as $k => $v)
$out2 .= "SET $k=$v\r\n";
+
        $out2 .= "SET $k=$v\r\n";
$out2 .= "SET dfnd_set=y\r\n)\r\n";
+
    $out2 .= "SET dfnd_set=y\r\n)\r\n";
 
}
 
}
 
$out1 .= ')';
 
$out1 .= ')';
 +
$out_info = '
 +
GOTO :EOF
 +
 +
:out_info
 +
ECHO %*
 +
ECHO.
 +
GOTO :EOF
 +
';
 +
 +
$out_info=str_replace("\n","\r\n",$out_info);
 +
$out2 .= $out_info;
  
 
if ( $f = fopen($titles_path, 'w' ) )
 
if ( $f = fopen($titles_path, 'w' ) )
if ( fwrite( $f, $out1 ) )
+
    if ( fwrite( $f, $out1 ) )
print 'DF title file created successfully.<br/>';
+
        print 'DF title file created successfully.<br/>';
+
 
 
if ( $f = fopen($data_path, 'w' ) )
 
if ( $f = fopen($data_path, 'w' ) )
if ( fwrite( $f, $out2 ) )
+
    if ( fwrite( $f, $out2 ) )
print 'DF data file created successfully.<br/>';</nowiki></pre>
+
        print 'DF data file created successfully.<br/>';
 +
</nowiki></pre>

Latest revision as of 22:33, 2 August 2007

<?php


// Settings
$df_path = 'http://speeddemosarchive.com/kb/index.php/DF'; // Path to sda/kb/DB
$titles_path = 'dfnd_titles_temp.bat'; // Path where the titles file will be written
$data_path = 'dfnd_data_temp.bat'; // Path where the data file will be written


// Usually non-editable stuff below.

// If this doesn't work, comment it out and use the commented code below it.
$raw = file_get_contents($df_path); 
/*
ob_start();
include $df_path;
$raw = ob_get_contents();
ob_clean();
*/

$pos1 = strpos($raw, '<table>');
$pos2 = strpos($raw, '</table>');

if ( ($pos1 === false) or ($pos2 === false) or ($pos1 > $pos2) )
    die('DF data does not exist in this page.');
    
$raw = substr($raw, $pos1, $pos2 - $pos1 - 1);

$t = explode('<tr><td>', $raw);

$k = 0;
$ct = count($t);
for ($i = 1; $i < $ct; $i++) 
{
    $t2 = substr( $t[$i], strpos($t[$i], '</td>') );
    if ( preg_match('/D([14])/', $t2, $m) )
        $data[$k]['d'] = $m[1];
    if ( preg_match('/F([123])/', $t2, $m) )
        $data[$k]['f'] = $m[1];
    if ( preg_match('/([23])D/', $t2, $m) )
        $data[$k]['twod'] = ($m[1] == 2) ? 'y' : 'n';
    if ( is_array($data[$k]) )
    {
        $title[$k] = str_replace(
            array( ':', '&', '<', '>', '(', ')', 'é' ),
            array( ' -', 'and', '[', ']', '[', ']', 'e' ),
            html_entity_decode( substr( $t[$i], 0, strpos($t[$i], '</td>') ) )
        );
        $initial[$k] = strtolower($title[$k]{0});
        if ($initial[$k] == '.') $initial[$k] = 'd'; // because .hack disagrees with Spock's philosophy.
        $k++;
    }
}

if (!is_array($title))
    die('DF data does not exist in this page.');

$out1 = $out2 = "@ECHO OFF\r\n";

$cur_initial = '';
$ct = count($title);
for ($i = 0; $i < $ct; $i++)
{
    if ($initial[$i] == $cur_initial) 
        $j++;
    else
    {
        $j = 1;
        $cur_initial = $initial[$i];
        if ($out1) $out1 .= ")\r\n";
        $out1 .= "IF [%dfnd_letter%]==[$cur_initial] (\r\n";
    }
    $number[$i] = sprintf('%03d', $j);
    $title[$i] = preg_replace('/[:&|><\(\)]/', '^\\0', $title[$i]);
    $out1 .= "ECHO {$initial[$i]}{$number[$i]}: {$title[$i]}\r\n";
    $out2 .= "IF [%s%]==[{$initial[$i]}{$number[$i]}] (\r\nCALL :out_info Selected \"{$title[$i]}\"\r\n";
    foreach ($data[$i] as $k => $v)
        $out2 .= "SET $k=$v\r\n";
    $out2 .= "SET dfnd_set=y\r\n)\r\n";
}
$out1 .= ')';
$out_info = '
GOTO :EOF

:out_info
ECHO %*
ECHO.
GOTO :EOF
';

$out_info=str_replace("\n","\r\n",$out_info);
$out2 .= $out_info;

if ( $f = fopen($titles_path, 'w' ) )
    if ( fwrite( $f, $out1 ) )
        print 'DF title file created successfully.<br/>';

if ( $f = fopen($data_path, 'w' ) )
    if ( fwrite( $f, $out2 ) )
        print 'DF data file created successfully.<br/>';
Personal tools