Skip to content

Instantly share code, notes, and snippets.

Created February 1, 2009 18:27
Show Gist options
  • Save anonymous/55940 to your computer and use it in GitHub Desktop.
Save anonymous/55940 to your computer and use it in GitHub Desktop.
<?php
/**
「res/min」や「(2)」、「.dat」が番組名に含まれると表示が崩れますがオリジナルどおりの仕様です。
**/
/**
* datの各行を処理し、配列にして返す。
* 引数 string $string 各行。
* 返り値 array 'datNumber', 'resPerMinutes', 'title' をキーとする連想配列。
**/
function parseLine($string){
$thread_utf8 = mb_convert_encoding($string, 'UTF-8', 'SJIS');
$thread_utf8 = str_replace('.dat<>', '<>', $thread_utf8);
$thread_utf8 = str_replace('res/min', '<>', $thread_utf8);
$thread_utf8 = str_replace('(2)', '', $thread_utf8);
$result= array();
list($datNumber, $resPerMinutes, $title) = explode('<>', $thread_utf8);
$result['datNumber'] = trim($datNumber);
$result['resPerMinutes'] = trim($resPerMinutes);
$result['title'] = trim($title);
return $result;
}
/**
* 各アイテムの連想配列を受け取り、<item>セクションをひとつ作って返す。
* 引数 array $item parseLine()の返す連想配列。
* 返り値 string <item>セクションひとつ。
**/
function buildItemXml($item){
$result= '';
$result.= " <item>\n";
$result.= " <title>".$item['title']."</title>\n";
$result.= " <description>".$item['resPerMinutes']."res/min" . "</description>\n";
$result.= " <link>".getUrl($item['datNumber'])."</link>\n";
$result.= " </item>\n";
return $result;
}
/**
* dat番号を受け取り、リンク先URLを返す。
* 引数 string $datnumber dat番号
* 返り値 string URL
**/
function getUrl($datNumber){
$u = "http://speedo.ula.cc/test/r.so/epg.2ch.net/tv2chwiki/";
return $u . $datNumber . "/";
}
$url = 'http://epg.2ch.net/tv2chwiki/subject.txt';
$header = '<?xml version="1.0" encoding="UTF-8"?>' . "\n"
. ' <rss version="2.0">' . "\n"
. ' <channel>' . "\n"
. ' <title>2chtv</title>' . "\n"
. ' <link>http://speedo.ula.cc/test/p.so/epg.2ch.net/tv2chwiki/?guid=ON</link>' . "\n"
. ' <description>2ちゃんねるTV番組欄</description>' . "\n"
. ' <language>ja</language>' . "\n";
$footer = " </channel>\n"
. " </rss>";
$lines = file($url);
$body = '';
foreach($lines as $line){
$parsedLine = parseLine($line);
$body .= buildItemXml($parsedLine);
}
file_put_contents('aha.xml', $header . $body . $footer);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment