Skip to content

Instantly share code, notes, and snippets.

@spicydog
Last active December 1, 2015 08:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spicydog/1cbb48a7583ab3bcdd88 to your computer and use it in GitHub Desktop.
Save spicydog/1cbb48a7583ab3bcdd88 to your computer and use it in GitHub Desktop.
Auto anime download with aria2c and php from horriblesubs.info
<?php
$keyword = 'one piece';
$keyword = 'fairy tail';
function subBetweenStrings($string, $begin, $end) {
if($begin==='')
return substr( $string, 0, strpos($string,$end) );
if($end==='')
return substr($string, strpos($string, $begin)+strlen($begin));
$string = ' ' . $string;
$ini = strpos($string,$begin);
if ($ini == 0) return '';
$ini += strlen($begin);
$len = strpos($string,$end,$ini) - $ini;
return substr($string,$ini,$len);
}
if (!is_dir('log')) {
mkdir('log');
}
$time = time()*1000;
$keyword = str_replace(' ', '%20', $keyword);
$url = "http://horriblesubs.info/lib/search.php?value=$keyword&_=$time";
$html = file_get_contents($url);
$lines = explode("<td", $html);
$results = [];
$currentEpisode = '';
foreach ($lines as $line) {
if (strpos($line, 'class="dl-label"')) {
$currentEpisode = str_replace(' class="dl-label"><i>', '', str_replace('</i></td>', '', $line));
}
if (strpos($line, 'class="dl-type hs-magnet-link"')) {
$episode = subBetweenStrings($currentEpisode, ' - ', ' [');
if (is_numeric($episode) && intval($episode)>0) { // filter only weekly ones
$link = explode('"', $line)[7];
$hash = explode('&', $link)[0];
$hash = explode(':', $hash);
$hash = end($hash);
$results[$currentEpisode]['name'] = $currentEpisode;
$results[$currentEpisode]['episode'] = $episode;
$results[$currentEpisode]['magnetHash'] = $hash;
$results[$currentEpisode]['magnetLink'] = $link;
}
}
}
foreach ($results as $name => $value) {
if (strpos($name, '[720p]') === false) { // only 720p that we want
unset($results[$name]);
}
}
// get the lastest one
$meta = array_values($results)[0];
if (!is_file('log/'.$meta['magnetHash'].'.json')) {
echo "found new download " . $meta['name'] . " \n";
file_put_contents('log/'.$meta['magnetHash'].'.json', json_encode($meta, JSON_PRETTY_PRINT));
$cmd = sprintf('aria2c --seed-time=30 --seed-ratio=1.5 --bt-require-crypto=true --bt-min-crypto-level=arc4 "%s"', $meta['magnetLink']);
// exec($cmd);
echo "download complete!\n";
} else {
echo "no new download\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment