Skip to content

Instantly share code, notes, and snippets.

@dirceu
Created September 5, 2011 22:57
Show Gist options
  • Save dirceu/1196113 to your computer and use it in GitHub Desktop.
Save dirceu/1196113 to your computer and use it in GitHub Desktop.
Instagram JPlatform example
<?php
define('_JEXEC', 1);
//
// Author: Dirceu Pereira Tiegs <[email protected]> and other folks from #jdbr11
// Jetworks Desenvolvimento Web
//
// run this inside joomla-platform/examples/
require_once ('../libraries/import.php');
jimport('joomla.application.cli');
jimport('joomla.database.database');
jimport('joomla.database.table');
class Instagram extends JCli
{
public function getInstagramData($tag)
{
return simplexml_load_file("http://instagr.am/tags/$tag/feed/recent.rss");
}
public function execute()
{
// Change the path to use your Joomla! installation
require_once '/var/www/htdocs/joomla17/configuration.php';
$media_path = '/var/www/htdocs/joomla17/media/';
// Get a database driver
$config = new JConfig;
$db = JDatabase::getInstance(array(
'driver' => $config->dbtype,
'host' => $config->host,
'user' => $config->user,
'password' => $config->password,
'database' => $config->db,
'prefix' => $config->dbprefix
));
JFactory::$config = new JRegistry($config);
// Get input from the user
$this->out("Tag to search");
$tag = $this->in();
$this->out("Number of items to download");
$count = $this->in();
$this->out("Publish the articles immediately (Y/N)?");
$published = $this->in();
$publish = strtoupper($published) == 'Y';
$pictures = $this->getInstagramData($tag);
for($i=0; $i<$count; $i++) {
// Because of some weird behavior of SimpleXML objects, we need to make some casts
$pic = $pictures->channel->item[$i];
$title = (string)$pic->title;
$img = (string)$pic->description;
$article = JTable::getInstance('content', 'JTable', array('dbo' => $db));
$alias = trim(strtolower(preg_replace(array('/\s+/', '/[^A-Za-z0-9\-]/'), array('-', ''), $title)));
$catid = 9;
// Save the image locally
$filename = $alias.'.jpg';
$destination = fopen($media_path.$filename, "w+");
$source = fopen((string)$pic->link, "r");
while ($a = fread($source,1024)) fwrite($destination, $a);
fclose($source);
fclose($destination);
// Fill the required attributes on the article
$article->load(array('alias' => $alias, 'catid' => $catid));
$article->introtext = "$title <br /><img src='media/$filename' />";
$article->title = $title;
$article->alias = $alias;
$article->catid = 9;
$article->state = ($publish ? 1 : 0);
$article->created_by = 42;
$article->access = 1;
if ($article->store()) {
$this->out('Added the picture: '.$title.' to the Joomla site database.');
} else {
$this->out($article->getError());
}
}
}
}
JCli::getInstance('Instagram')->execute();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment