Introduction: Importing content using RSS-Bridge Library
I bring news of a new solution 7x is working on for developers with an interest to syndicate information without an rss / atom / xml / etc feed available.
Today if you want to include your companies YouTube Playlist videos (say commercials or educational materials) you can embed this content manually within the administrator content editing features available by default.
What if you want to sync content from YouTube like your companies playlist video content (the meta data not the actual videos, playback is through YouTube's embed video URL APIs) into the eZ Content Tree Automatically?
Currently this is possible today without a feed via old fashioned web page scraping of specific account playlists video title and video URL URI v element data (video's id) using RSS-Bridge.
Normally RSS-Bridge (doc) is a standalone application well written in OOP PHP. Because documentation on using the library standalone is limited we created a solution to bootup the software layers and call the provided YouTube Playlist Data Collection APIs from RSS-Bridge within an eZ Publish Request Scope within a eZ Publish Cronjob Part Script.
Filename: extension/sevenx_rss_bridge/cronjobs/7xRssBridgeYoutubeFeedImport.php
<?php if ( !$isQuiet ) { $cli->output( "Creating youtube video object(s) ..." ); } $count = SevenxRssBridgeFeedImport::importFeedContentObjects(); if ( !$isQuiet ) { $cli->output( "Number of objects created: $count" ); $cli->output( "Done." ); } ?>
Got your attention now with a stub for calling the features we need within eZ (Remember to enable via cronjob settings and clear all caches).
Filename: extension/sevenx_rss_bridge/settings/cronjobs.ini.append.php
<?php /* #?ini charset="utf8"? [CronjobSettings] ExtensionDirectories[]=sevenx_rss_bridge [CronjobPart-youtube-feed-import] Scripts[]=7xRssBridgeYoutubeFeedImport.php */ ?>
Next we have the worker class that does the work to fetch the YouTube information, create the YouTube videos (Meta Data Based Embeds in a Playlist) underneath Youtube Playlist Nodes Containing the Playlist Meta Data.
With this we first create the playlist nodes (locations) with the required playlist YouTube embed URL + Parameters (like list which helps provide long form content within a playlist). With the following code in place (remember to regenerate eZ's Autoloads) we can call the cronjob and sync / import the data from YouTube into eZ Publish Content Tree.
Filename: extension/sevenx_rss_bridge/classes/SevenxRssBridgeFeedImport.php
<?php class SevenxRssBridgeFeedImport { public static function importFeedContentObjects() { // Settings $storageNodeID = eZINI::instance('sevenx_videos.ini')->variable('PlaylistSettings','PlaylistNodeID'); // Fetch Playlists Objects to store the videos underneath. $playlists = self::fetchNodeContent($storageNodeID ); // Initialize the count of imported items $importedCount = 0; // Include RSS-Bridge autoloader require_once('vendor/rss-bridge/rss-bridge/lib/bootstrap.php'); foreach ( $playlists as $playlist ) { if ( $playlist->attribute('children_count') <= 0 ) { $playlistsNodeID = $playlist->attribute('node_id'); $dm = $playlist->dataMap(); $playlistID = $dm[ 'youtube_url' ]->content(); if ( !str_contains($playlistID, 'videoseries' ) ) continue; $playlistID = explode( 'list=', $playlistID )[1]; $bridgeParams = [ 'p' => $playlistID ]; try { $main = new RssBridge(); $bridgeFactory = new BridgeFactory(); // Directly instantiate the YouTubeBridge class $bridge = $bridgeFactory->create( YoutubeBridge::class ); // Set parameters $bridge->setInput( $bridgeParams ); // Fetch data $data = $bridge->collectData(); $items = $bridge->getItems(); //var_dump( $items ); // Process each video entry foreach ($items as $item) { $importedCount += self::importYouTubeVideo($item, $playlistsNodeID, $playlistID); } } catch (Exception $e) { echo "Error: " . $e->getMessage() . "\n"; return 0; // Return 0 in case of error } } } echo "Successfully imported YouTube videos.\n"; return $importedCount; // Return the count of imported items } private static function importYouTubeVideo($item, $storageNodeID, $playlistID) { $adminUser = 'admin'; // no password used so safe and available by default. change if needed. $classID = 48; // Note: Please change to your video class. // Extract video data $title = $item['title']; $description = $item['content']; $url = $item['uri']; $video = explode( '?v=', $url )[1]; $url = "https://www.youtube.com/embed/$video?si=" . $video . '&list=' . $playlistID; // var_dump($item['title']); // var_dump($url); // die('fin'); // return 1; // Fetch admin user $user = eZUser::fetchByName( $adminUser ); $userCreatorID = $user->attribute( 'contentobject_id' ); // Create object $defaultSectionID = 1; $class = eZContentClass::fetch( $classID ); $contentObject = $class->instantiate( $userCreatorID, $defaultSectionID ); // Set remote_id content //$remoteID = "contentserver:incomingnode"; //$contentObject->setAttribute( 'remote_id', $remoteID ); $contentObject->store(); // Fetch related IDs $contentObjectID = $contentObject->attribute( 'id' ); //$userID = $contentObjectID; // Create node assignment $nodeAssignment = eZNodeAssignment::create( array( 'contentobject_id' => $contentObjectID, 'contentobject_version' => 1, 'parent_node' => $storageNodeID, 'is_main' => 1 ) ); $nodeAssignment->store(); // Set version modified and status content $version = $contentObject->version( 1 ); $version->setAttribute( 'modified', time() ); $version->setAttribute( 'status', eZContentObject::STATUS_DRAFT ); $version->store(); // Fetch contentObject IDs $contentObjectID = $contentObject->attribute( 'id' ); $contentObjectAttributes = $version->contentObjectAttributes(); // Set Name $contentObjectAttributes[0]->setAttribute( 'data_text', $title ); $contentObjectAttributes[0]->store(); // Set Name $contentObjectAttributes[2]->fromString( "$url|$title" ); $contentObjectAttributes[2]->store(); // Publish content object to top level root node $operationResult = eZOperationHandler::execute( 'content', 'publish', array( 'object_id' => $contentObjectID,'version' => 1 ) ); echo "Imported video: $title\n $url\n"; return 1; // Return 1 to indicate successful import } public static function fetchNodeContent( $nodeID = 2, $limit = false, $classIdentifierArray = array( 'playlist' ) ) { $node = eZContentObjectTreeNode::fetch($nodeID); if (!$node) { echo "Node with ID $nodeID not found.\n"; return []; } // Fetch only objects of 'playlist' class $params = [ 'ClassFilterType' => array( 'include' ), 'ClassFilterArray' => $classIdentifierArray, // Replace 'playlist' with your desired class identifier 'Limit' => $limit // Adjust the limit as needed ]; $result = eZContentObjectTreeNode::subTreeByNodeID($params, $nodeID); return $result; } } ?>
Here is an example of calling the cronjob script. We do this after the above is properly installed and configured.
cd /path/to/ezpublish/; clear; date;./runcronjobs.php youtube-feed-import; date
This solution works well by simulation of the RSS-Bridge Application which effortlessly (once you learn how to call it correctly) fetches Youtube / Instagram / Telegram / Other content for you to store and use within your own website powered by eZ Publish.
We are working to release a package of this software ready to install and customize to meet your own needs. Until then use this blog post as a project origin story / working example of a solution for content stored in web applications which do not provide feeds.
Comments