Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit e77fc82fcd3f297dfc2719e0193d3f98ca258150 @gruzilla gruzilla committed Dec 4, 2010
@@ -0,0 +1,83 @@
+<?php
+/**
+ * @category Instagram
+ * @package Instagram
+ * @copyright Copyright (c) 2010-2011 Matthias Steinbšck <matthias@abendstille.at>
+ * @license New BSD License (enclosed file docs/LICENSE)
+ */
+class Instagram {
+
+ /*
+ * @var array
+ */
+ protected $_commands = array();
+
+ /*
+ * @var Instagram_Client_Communication
+ */
+ protected $_communication;
+
+ /*
+ * @var Instagram_Api_Feed
+ */
+ public $feed;
+
+ /*
+ * @var Instagram_Api_Auth
+ */
+ public $auth;
+
+ public function __construct() {
+ $this->feed = new Instagram_Api_Feed();
+ $this->feed->setInstagram($this);
+ $this->auth = new Instagram_Api_Auth();
+ $this->auth->setInstagram($this);
+ $this->_communication = new Instagram_Client_Communication();
+ }
+
+ public function addCommand(Instagram_Command_ICommand $cmd) {
+ $this->_commands[] = $cmd;
+ }
+
+ public function run() {
+ $executed = array();
+ //$cookieJar = null;
+ foreach ($this->_commands as $utcmd) {
+ if ($utcmd instanceof Instagram_Command_ICommand) {
+ /*
+ * @var Instagram_Command_ICommand
+ */
+ $cmd = $utcmd;
+
+ //if (!is_null($cookieJar))
+ // $this->_communication->getClient()->setCookieJar($cookieJar);
+
+ foreach ($cmd->dependsOn() as $dependency) {
+ foreach ($executed as $previous) {
+ if (get_class($previous) == 'Instagram_Command_'.$dependency) {
+ break 2;
+ }
+ }
+ throw new Instagram_Command_DependencyException(
+ 'Command '.get_class($cmd)." depends on $dependency but it never was executed!\n\n"
+ );
+ }
+ $cmd->setCommunication($this->_communication);
+
+ $cmd->validate();
+
+ //echo get_class($cmd)." is valid. executing...\n\n";
+ $cmd->exec();
+
+ $executed[] = $cmd;
+
+ // reset parameters
+ $this->_communication->getClient()->resetParameters();
+ //$cookieJar = $this->_communication->getClient()->getCookieJar();
+ }
+ }
+
+ // reset commands
+ $this->_commands = array();
+ }
+}
@@ -0,0 +1,18 @@
+<?php
+/**
+ * @category Instagram
+ * @package Instagram
+ * @copyright Copyright (c) 2010-2011 Matthias Steinbšck <matthias@abendstille.at>
+ * @license New BSD License (enclosed file docs/LICENSE)
+ */
+class Instagram_Api
+{
+ /*
+ * @var Instagram
+ */
+ protected $_instagram;
+
+ public function setInstagram(Instagram $instagram) {
+ $this->_instagram = $instagram;
+ }
+}
@@ -0,0 +1,28 @@
+<?php
+/**
+ * @category Instagram
+ * @package Instagram_Api
+ * @copyright Copyright (c) 2010-2011 Matthias Steinbšck <matthias@abendstille.at>
+ * @license New BSD License (enclosed file docs/LICENSE)
+ */
+class Instagram_Api_Auth extends Instagram_Api
+{
+ /**
+ * Logs you in
+ * @param String $username
+ * @param String $password
+ * @param String $deviceId
+ * @return Instagram_Command_Login
+ */
+ public function login($username, $password, $deviceId) {
+ $cmd = new Instagram_Command_Login();
+
+ $cmd->setParameter('username', $username);
+ $cmd->setParameter('password', $password);
+ $cmd->setParameter('device_id', $deviceId);
+
+ $this->_instagram->addCommand($cmd);
+
+ return $cmd;
+ }
+}
@@ -0,0 +1,40 @@
+<?php
+/**
+ * @category Instagram
+ * @package Instagram_Api
+ * @copyright Copyright (c) 2010-2011 Matthias Steinbšck <matthias@abendstille.at>
+ * @license New BSD License (enclosed file docs/LICENSE)
+ */
+class Instagram_Api_Feed extends Instagram_Api
+{
+ /**
+ * Reads the timeline from your feed. you can provide a point of time. (format unknown atm)
+ * @param String $time
+ * @return Instagram_Command_Feed_Timeline
+ */
+ public function timeline($time = '?') {
+ $cmd = new Instagram_Command_Feed_Timeline();
+
+ $cmd->setParameter('time', $time);
+
+ $this->_instagram->addCommand($cmd);
+ return $cmd;
+ }
+
+ /**
+ * Reads the timeline from a users feed. you have to know his PK
+ * you can provide a point of time. (format unknown atm)
+ * @param int $pk
+ * @param String $time
+ * @return Instagram_Command_Feed_Timeline
+ */
+ public function user($pk, $time = '?') {
+ $cmd = new Instagram_Command_Feed_User();
+
+ $cmd->setParameter('pk', $pk);
+ $cmd->setParameter('time', $time);
+
+ $this->_instagram->addCommand($cmd);
+ return $cmd;
+ }
+}
@@ -0,0 +1,31 @@
+<?php
+/**
+ * @category Instagram
+ * @package Instagram_Client
+ * @copyright Copyright (c) 2010-2011 Matthias Steinbšck <matthias@abendstille.at>
+ * @license New BSD License (enclosed file docs/LICENSE)
+ */
+class Instagram_Client_Communication
+{
+ /**
+ * @var Zend_Http_Client
+ */
+ protected $_client;
+
+ /**
+ * @var array
+ */
+ protected $_cookies;
+
+
+ public function __construct() {
+ $this->_client = new Zend_Http_Client('http://instagr.am/api', array(
+ 'keepalive' => true
+ ));
+ $this->_client->setCookieJar();
+ }
+
+ public function getClient() {
+ return $this->_client;
+ }
+}
@@ -0,0 +1,30 @@
+<?php
+/**
+ * @category Instagram
+ * @package Instagram_Client
+ * @copyright Copyright (c) 2010-2011 Matthias Steinbšck <matthias@abendstille.at>
+ * @license New BSD License (enclosed file docs/LICENSE)
+ */
+class Instagram_Client_Response {
+ /**
+ * @var Instagram_Command_ICommand
+ */
+ protected $_command;
+
+ /**
+ * @var mixed
+ */
+ protected $_response;
+
+ public function __construct(Instagram_Command_ICommand $command) {
+ $this->_command = $command;
+ }
+
+ public function setData($response) {
+ $this->_response = $response;
+ }
+
+ public function getData() {
+ return $this->_response;
+ }
+}
@@ -0,0 +1,71 @@
+<?php
+/**
+ * @category Instagram
+ * @package Instagram_Command
+ * @copyright Copyright (c) 2010-2011 Matthias Steinbšck <matthias@abendstille.at>
+ * @license New BSD License (enclosed file docs/LICENSE)
+ */
+abstract class Instagram_Command_AbstractCommand implements Instagram_Command_ICommand
+{
+ /*
+ * @var Instagram_Client_Communication
+ */
+ protected $_communication;
+
+ /*
+ * @var array
+ */
+ protected $_parameters = array();
+
+ /*
+ * @var array
+ */
+ protected $_requires = array();
+
+ /*
+ * @var array
+ */
+ protected $_dependsOn = array();
+
+ /*
+ * @var Instagram_Client_Response
+ */
+ protected $_response;
+
+ public function exec() {}
+
+ /**
+ * standard behaviour for setting the _communication attribute
+ * @see Instagram_Command_ICommand::setCommunication()
+ */
+ public function setCommunication(Instagram_Client_Communication $communication) {
+ $this->_communication = $communication;
+ $this->_response = new Instagram_Client_Response($this);
+ }
+
+ /**
+ * standard behaviour for setting the parameter in the _parameters attribute
+ * @see Instagram_Command_ICommand::setParameter()
+ */
+ public function setParameter($name, $value) {
+ $this->_parameters[$name] = $value;
+ }
+
+ /**
+ * @return Instagram_Client_Response
+ */
+ public function getResponse() {
+ return $this->_response;
+ }
+
+ public function validate() {
+ if (is_null($this->_communication)) throw new Instagram_Command_InvalidException('No communication instance!', 1);
+ if (isset($this->_requires)) foreach ($this->_requires as $required) {
+ if (!isset($this->_parameters[$required])) throw new Instagram_Command_InvalidException('You forgot to set required parameter '.$required.' for '.get_class($this), 2);
+ }
+ }
+
+ public function dependsOn() {
+ return $this->_dependsOn;
+ }
+}
@@ -0,0 +1,10 @@
+<?php
+/**
+ * @category Instagram
+ * @package Instagram_Command
+ * @copyright Copyright (c) 2010-2011 Matthias Steinbšck <matthias@abendstille.at>
+ * @license New BSD License (enclosed file docs/LICENSE)
+ */
+class Instagram_Command_DependencyException extends Instagram_Exception
+{
+}
@@ -0,0 +1,47 @@
+<?php
+/**
+ * @category Instagram
+ * @package Instagram_Command_Feed
+ * @copyright Copyright (c) 2010-2011 Matthias Steinbšck <matthias@abendstille.at>
+ * @license New BSD License (enclosed file docs/LICENSE)
+ */
+class Instagram_Command_Feed_Timeline extends Instagram_Command_AbstractCommand
+{
+ protected $_dependsOn = array(
+ 'Login'
+ );
+
+ protected $_requires = array(
+ 'time'
+ );
+
+ public function exec() {
+ $client = $this->_communication->getClient();
+
+ $client->setUri('http://instagr.am/api/v1/feed/timeline/'.$this->_parameters['time']);
+ $response = $client->request(Zend_Http_Client::GET);
+
+ $body = $response->getBody();
+ $this->getResponse()->setData(
+ self::parseResponse(
+ json_decode($body)
+ )
+ );
+ }
+
+ /**
+ * @param stdClass $response
+ * @return array<Instagram_Feed_Entry>
+ */
+ protected static function parseResponse($response) {
+ $entries = array();
+ // items, status, num_results
+ foreach ($response->items as $entryData) {
+ if (!is_object($entryData)) {
+ continue;
+ }
+ $entries[] = Instagram_Feed_Entry::factory($entryData);
+ }
+ return $entries;
+ }
+}
Oops, something went wrong.

0 comments on commit e77fc82

Please sign in to comment.