Skip to content

Instantly share code, notes, and snippets.

@ha1t
Created January 21, 2012 09:03
Show Gist options
  • Save ha1t/1652073 to your computer and use it in GitHub Desktop.
Save ha1t/1652073 to your computer and use it in GitHub Desktop.
しょぼいカレンダーを使ってアニメタイトルをファイル名にする
#!/usr/bin/env php
<?php
/**
* しょぼいカレンダーを使ってアニメタイトルをファイル名にする
* @url https://sites.google.com/site/syobocal/spec/rss2-php
*/
//$word = '戦姫絶唱シンフォギア_20120113_225900.ts';
$user_id = 'anime_rename';
if (!isset($argv[1])) {
echo "arg notfound.";
exit;
}
if (!file_exists($argv[1])) {
echo "file notfound.";
exit;
}
$dir = dirname($argv[1]);
if ($dir == '.') {
$dir = '';
} else {
$dir .= '/';
}
$word = basename($argv[1]);
$parts = explode('.', $word);
$ext = end($parts);
$parts = explode('_', $word);
$title = $parts[0];
$start = $parts[1] . '0000';
$url = "http://cal.syoboi.jp/rss2.php?start={$start}&days=1&usr={$user_id}&titlefmt=%24(Title)%7C%24(Count)%7C%24(EdTime)%7C%24(SubTitleA)";
$xml = file_get_contents($url);
if ($xml === false) {
echo "http error";
exit;
}
function getProgramList($xml)
{
$matches = array();
preg_match_all("_<title>(.*?)</title>_", $xml, $matches);
$list = array();
foreach ($matches[1] as $title) {
$parts = explode('|', $title);
if (count($parts) > 2) {
$row = array(
'title' => $parts[0],
'count' => $parts[1],
'edtime' => $parts[2],
'subtitle' => $parts[3],
);
$list[] = $row;
}
}
return $list;
}
function get_title($word, $items)
{
$result = '';
$shortest = -1;
foreach ($items as $item) {
$lev = levenshtein($item['title'], $word);
if ($lev == 0) {
return $item;
}
if ($lev <= $shortest || $shortest < 0) {
$result = $item;
$shortest = $lev;
}
}
var_dump("lev:" . $lev);
return $result;
}
function get_title2($word, $items)
{
$result = '';
$before_percent = 0;
foreach ($items as $item) {
similar_text($word, $item['title'], $percent);
if ($percent == 100) {
echo "percent:{$percent}" . PHP_EOL;
return $item;
}
if ($before_percent < $percent) {
$result = $item;
$before_percent = $percent;
}
}
echo "percent:{$percent}" . PHP_EOL;
return $result;
}
$items = getProgramList($xml);
$item = get_title2($title, $items);
$item['title'] = htmlspecialchars_decode($item['title'], ENT_QUOTES);
$item['title'] = mb_convert_kana($item['title'], 'a');
$item['title'] = str_replace(' ', '_', $item['title']);
$item['subtitle'] = htmlspecialchars_decode($item['subtitle'], ENT_QUOTES);
$item['subtitle'] = mb_convert_kana($item['subtitle'], 'a');
$item['subtitle'] = str_replace(' ', '_', $item['subtitle']);
if ((int)$item['count'] < 10) {
$item['count'] = '0' . $item['count'];
}
$rename_title = "{$item['title']}_#{$item['count']}_{$item['subtitle']}.{$ext}";
$src = $dir . $word;
$dst = $dir . $rename_title;
echo "SRC:{$src}" . PHP_EOL;
echo "DST:{$dst}" . PHP_EOL;
echo "rename? (y/n)" . PHP_EOL;
$stdin = fopen("php://stdin", "r");
$line = trim(fgets($stdin, 64));
fclose($stdin);
if ($line === 'y') {
rename($src, $dst);
echo "renamed." . PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment