public function gather() { if (!empty($this->source)) { $podcast = new Podcast($this->podcast); $file = $podcast->get_root_path(); if (!empty($file)) { $pinfo = pathinfo($this->source); $file .= DIRECTORY_SEPARATOR . $this->id . '-' . $pinfo['basename']; debug_event('podcast_episode', 'Downloading ' . $this->source . ' to ' . $file . ' ...', 5); if (file_put_contents($file, fopen($this->source, 'r')) !== false) { debug_event('podcast_episode', 'Download completed.', 5); $this->file = $file; $vainfo = new vainfo($this->file); $vainfo->get_info(); $key = vainfo::get_tag_type($vainfo->tags); $infos = vainfo::clean_tag_info($vainfo->tags, $key, $file); // No time information, get it from file if ($this->time <= 0) { $this->time = $infos['time']; } $this->size = $infos['size']; $sql = "UPDATE `podcast_episode` SET `file` = ?, `size` = ?, `time` = ?, `state` = 'completed' WHERE `id` = ?"; Dba::write($sql, array($this->file, $this->size, $this->time, $this->id)); } else { debug_event('podcast_episode', 'Error when downloading podcast episode.', 1); } } } else { debug_event('podcast_episode', 'Cannot download podcast episode ' . $this->id . ', empty source.', 3); } }
public static function create(array $data) { $feed = $data['feed']; // Feed must be http/https if (strpos($feed, "http://") !== 0 && strpos($feed, "https://") !== 0) { AmpError::add('feed', T_('Wrong feed url')); } $catalog_id = intval($data['catalog']); if ($catalog_id <= 0) { AmpError::add('catalog', T_('Target catalog required')); } else { $catalog = Catalog::create_from_id($catalog_id); if ($catalog->gather_types !== "podcast") { AmpError::add('catalog', T_('Wrong target catalog type')); } } if (AmpError::occurred()) { return false; } $title = T_('Unknown'); $website = null; $description = null; $language = null; $copyright = null; $generator = null; $lastbuilddate = 0; $episodes = array(); $arturl = ''; $xmlstr = file_get_contents($feed); if ($xmlstr === false) { AmpError::add('feed', T_('Cannot access the feed.')); } else { $xml = simplexml_load_string($xmlstr); if ($xml === false) { AmpError::add('feed', T_('Cannot read the feed.')); } else { $title = html_entity_decode($xml->channel->title); $website = $xml->channel->link; $description = html_entity_decode($xml->channel->description); $language = $xml->channel->language; $copyright = html_entity_decode($xml->channel->copyright); $generator = html_entity_decode($xml->channel->generator); $lastbuilddatestr = $xml->channel->lastBuildDate; if ($lastbuilddatestr) { $lastbuilddate = strtotime($lastbuilddatestr); } if ($xml->channel->image) { $arturl = $xml->channel->image->url; } $episodes = $xml->channel->item; } } if (AmpError::occurred()) { return false; } $sql = "INSERT INTO `podcast` (`feed`, `catalog`, `title`, `website`, `description`, `language`, `copyright`, `generator`, `lastbuilddate`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; $db_results = Dba::write($sql, array($feed, $catalog_id, $title, $website, $description, $language, $copyright, $generator, $lastbuilddate)); if ($db_results) { $id = Dba::insert_id(); $podcast = new Podcast($id); $dirpath = $podcast->get_root_path(); if (!is_dir($dirpath)) { if (mkdir($dirpath) === false) { debug_event('podcast', 'Cannot create directory ' . $dirpath, 1); } } if (!empty($arturl)) { $art = new Art($id, 'podcast'); $art->insert_url($arturl); } if (count($episodes) > 0) { $podcast->add_episodes($episodes); } } return $db_results; }