From 207c085b30955761beb0e76837fc1838148a648c Mon Sep 17 00:00:00 2001 From: Nick Frey Date: Wed, 28 Nov 2012 16:57:07 -0600 Subject: Play audio podcasts from feeds in the News app --- appinfo/database.xml | 10 ++++++++++ appinfo/version | 2 +- lib/item.php | 32 +++++++++++++++++++++++++++++++- lib/itemmapper.php | 26 +++++++++++++++++++++----- lib/utils.php | 15 ++++++++++++++- templates/part.items.php | 15 ++++++++++++++- 6 files changed, 91 insertions(+), 9 deletions(-) diff --git a/appinfo/database.xml b/appinfo/database.xml index c5b06bcac..fbc5a4d6f 100644 --- a/appinfo/database.xml +++ b/appinfo/database.xml @@ -178,6 +178,16 @@ clob false + + enclosure_mime + clob + false + + + enclosure_link + clob + false + feed_id integer diff --git a/appinfo/version b/appinfo/version index f1d68e6b5..a9212c01c 100644 --- a/appinfo/version +++ b/appinfo/version @@ -1,2 +1,2 @@ -7.5 +7.6 diff --git a/lib/item.php b/lib/item.php index 95a64859c..3d63e24f6 100644 --- a/lib/item.php +++ b/lib/item.php @@ -35,7 +35,8 @@ class Item { private $author; private $date; //date is stored in the Unix format private $feedTitle; - + private $enclosure; // Item_Enclosure object containing media attachment information + public function __construct($url, $title, $guid, $body, $id = null) { $this->title = $title; $this->url = $url; @@ -161,4 +162,33 @@ class Item { public function setDate($date) { $this->date = $date; } + + public function getEnclosure() { + return $this->enclosure; + } + + public function setEnclosure(Item_Enclosure $enclosure) { + $this->enclosure = $enclosure; + } +} + +class Item_Enclosure { + private $mimetype; + private $link; + + public function getMimeType() { + return $this->mimetype; + } + + public function setMimeType($mimetype) { + $this->mimetype = $mimetype; + } + + public function getLink() { + return $this->link; + } + + public function setLink($link) { + $this->link = $link; + } } diff --git a/lib/itemmapper.php b/lib/itemmapper.php index 9b6c613ff..9aac95a37 100644 --- a/lib/itemmapper.php +++ b/lib/itemmapper.php @@ -36,18 +36,25 @@ class ItemMapper { * @returns an object of the class OC_News_Item */ public function fromRow($row) { - $url = $row['url']; $title = $row['title']; $guid = $row['guid']; $body = $row['body']; $id = $row['id']; + $item = new Item($url, $title, $guid, $body, $id); $item->setStatus($row['status']); $item->setAuthor($row['author']); $item->setFeedId($row['feed_id']); $item->setDate(Utils::dbtimestampToUnixtime($row['pub_date'])); + if($row['enclosure_mime'] !== null && $row['enclosure_link'] !== null) { + $enclosure = new Item_Enclosure(); + $enclosure->setMimeType($row['enclosure_mime']); + $enclosure->setLink($row['enclosure_link']); + $item->setEnclosure($enclosure); + } + return $item; } @@ -195,11 +202,18 @@ class ItemMapper { $title = $item->getTitle(); $body = $item->getBody(); $author = $item->getAuthor(); - + $enclosure_mime = null; + $enclosure_link = null; + + if($enclosure = $item->getEnclosure()) { + $enclosure_mime = $enclosure->getMimeType(); + $enclosure_link = $enclosure->getLink(); + } + $stmt = \OCP\DB::prepare(' INSERT INTO ' . self::tableName . - '(url, title, body, author, guid, guid_hash, pub_date, feed_id, status) - VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) + '(url, title, body, author, guid, guid_hash, pub_date, enclosure_mime, enclosure_link, feed_id, status) + VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) '); if(empty($title)) { @@ -222,6 +236,8 @@ class ItemMapper { $guid, $guid_hash, $pub_date, + $enclosure_mime, + $enclosure_link, $feedid, $status ); @@ -244,7 +260,7 @@ class ItemMapper { public function findById($id) { $stmt = \OCP\DB::prepare('SELECT ' . self::tableName . '.id AS id, ' . self::tableName . - '.url AS url, ' . self::tableName . '.title AS title, guid, body, status, author, feed_id, pub_date' . + '.url AS url, ' . self::tableName . '.title AS title, guid, body, status, author, feed_id, pub_date, enclosure_mime, enclosure_link' . ' FROM ' . self::tableName . ' JOIN ' . FeedMapper::tableName . ' ON ' . self::tableName . '.feed_id = ' . FeedMapper::tableName . '.id WHERE (' . self::tableName . '.id = ? AND ' . FeedMapper::tableName . '.user_id = ? )'); diff --git a/lib/utils.php b/lib/utils.php index aa1111dec..52e71b092 100644 --- a/lib/utils.php +++ b/lib/utils.php @@ -77,7 +77,7 @@ class Utils { $itemGUID = $spitem->get_id(); $itemBody = $spitem->get_content(); $item = new Item($itemUrl, $itemTitle, $itemGUID, $itemBody); - + $spAuthor = $spitem->get_author(); if ($spAuthor !== null) { $item->setAuthor($spAuthor->get_name()); @@ -87,6 +87,19 @@ class Utils { $itemDate = $spitem->get_date('U'); $item->setDate($itemDate); + // associated media file, for podcasts + $itemEnclosure = $spitem->get_enclosure(); + if($itemEnclosure !== null) { + $enclosureType = $itemEnclosure->get_type(); + $enclosureLink = $itemEnclosure->get_link(); + if(stripos($enclosureType, "audio/") !== FALSE) { + $enclosure = new Item_Enclosure(); + $enclosure->setMimeType($enclosureType); + $enclosure->setLink($enclosureLink); + $item->setEnclosure($enclosure); + } + } + $items[] = $item; } } diff --git a/templates/part.items.php b/templates/part.items.php index efad98830..3d043dfd0 100644 --- a/templates/part.items.php +++ b/templates/part.items.php @@ -50,7 +50,20 @@ foreach($items as $item) { echo '

'. $feedTitle . $author . '

'; } - echo '
' . $item->getBody() . '
'; + echo '
'; + echo $item->getBody(); + + if($item->getEnclosure() !== null) { + $enclosure = $item->getEnclosure(); + $enclosureType = htmlspecialchars($enclosure->getMimeType(), ENT_QUOTES, 'UTF-8'); + $enclosureLink = htmlspecialchars($enclosure->getLink(), ENT_QUOTES, 'UTF-8'); + $enclosureFilename = htmlspecialchars(basename($enclosureLink), ENT_QUOTES, 'UTF-8'); + + echo '


'; + echo 'Original audio source (' . $enclosureFilename . ')'; + } + + echo '
'; echo '
'; echo '
    '; -- cgit v1.2.3