PageRenderTime 29ms CodeModel.GetById 15ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/inc/class.timeline.php

http://php-twitter.googlecode.com/
PHP | 250 lines | 142 code | 14 blank | 94 comment | 25 complexity | 3c0d2906ac21710876b5dbd31043ae1f MD5 | raw file
  1<?php
  2/**
  3 * Twitter Search
  4 *
  5 * @package php-twitter 2.0
  6 * @subpackage timeline
  7 * @author Aaron Brazell
  8 **/
  9
 10class Twitter_Timeline extends Twitter {
 11	/**
 12	 * Setup Twitter client connection details
 13	 *
 14	 * Ensure you set a user agent, whether via the constructor or by assigning a value to the property directly.
 15	 * Also, if you are not running this from the Eastern timezone, be sure to set your proper timezone.
 16	 *
 17	 * @access public
 18	 * @since 2.0
 19	 * @return Twitter_Timeline
 20	 */
 21	public function __construct( $username = null, $password = null, $user_agent = null, $headers = null, $timezone = 'America/New_York')
 22	{
 23		parent::__construct($username, $password, $user_agent, $headers, $timezone);
 24	}
 25
 26	/**
 27	 * Send a request for a timeline
 28	 *
 29	 * @access public
 30	 * @since 2.0
 31	 * @param string $scope public for public timeline, user for user timeline, mentions for @ replies
 32	 * @param array $args Parameters that can be passed in key/pair format. All are optional. Some only apply to certain scopes.
 33	 *  - since_id: INT. Only tweets after the specified Status ID are returned
 34	 *  - max_id: INT. Only tweets up to a specified Status ID are returned
 35	 *  - count: INT. Number of tweets to return. Defaults to 20.
 36	 *  - page: INT: Paged result set to display.
 37	 *  - id: INT or STRING: Twitter ID or screen name (Only User scope)
 38	 *  - user_id: INT Twitter ID (Only User scope)
 39	 *  - screen_name: STRING Twitter ID (Only User scope)
 40	 * @return object
 41	 */
 42	public function get_timeline( $scope = 'public', $args = array() )
 43	{
 44		extract( $args, EXTR_SKIP );
 45		$defaults = array(
 46			'count'		=> 20,
 47			'page'		=> 1
 48			);
 49		
 50		$qs = array();
 51		switch( $scope )
 52		{
 53			case 'home' :							#COMING SOON TO TWITTER
 54			case 'friends' :
 55				$this->api_url = 'http://twitter.com/statuses/friends_timeline.' . $this->type;
 56				if( $since_id )
 57					$qs['since_id'] = (int) $since_id;
 58				if( $max_id )
 59					$qs['max_id'] = (int) $max_id;
 60				if( $count )
 61					$qs['count'] = ( $count > 200 ) ? 200 : (int) $count;
 62				if( $page )
 63					$qs['page'] = (int) $page;
 64				$query_vars = wp_parse_args( $qs, $defaults );
 65				break;
 66			case 'user' :
 67				$this->api_url = 'http://twitter.com/statuses/user_timeline.' . $this->type;
 68				$url_scope = '/user_timeline';
 69				if( $id )
 70					$qs['id'] = $id;
 71				if( $user_id )
 72					$qs['user_id'] = (int) $user_id;
 73				if( $screen_name )
 74					$qs['screen_name'] = (string) $screen_name;
 75				if( $since_id )
 76					$qs['since_id'] = (int) $since_id;
 77				if( $max_id )
 78					$qs['max_id'] = (int) $max_id;
 79				if( $count )
 80					$qs['count'] = ( $count > 200 ) ? 200 : (int) $count;
 81				if( $page )
 82					$qs['page'] = (int) $page;
 83				$query_vars = wp_parse_args( $qs, $defaults );
 84				break;
 85			case 'mentions' :
 86				$this->api_url = 'http://twitter.com/statuses/mentions.' . $this->type;
 87				if( $since_id )
 88					$qs['since_id'] = (int) $since_id;
 89				if( $max_id )
 90					$qs['max_id'] = (int) $max_id;
 91				if( $count )
 92					$qs['count'] = ( $count > 200 ) ? 200 : (int) $count;
 93				if( $page )
 94					$qs['page'] = (int) $page;
 95				$query_vars = wp_parse_args( $qs, $defaults );
 96				break;
 97			case 'dm' :
 98				$this->api_url = 'http://twitter.com/direct_mesages.' . $this->type;
 99				if( $since_id )
100					$qs['since_id'] = (int) $since_id;
101				if( $max_id )
102					$qs['max_id'] = (int) $max_id;
103				if( $count )
104					$qs['count'] = ( $count > 200 ) ? 200 : (int) $count;
105				if( $page )
106					$qs['page'] = (int) $page;
107				$query_vars = wp_parse_args( $qs, $defaults );
108				break;
109			case 'dmsent' :
110				$this->api_url = 'http://twitter.com/direct_mesages/sent.' . $this->type;
111				if( $since_id )
112					$qs['since_id'] = (int) $since_id;
113				if( $max_id )
114					$qs['max_id'] = (int) $max_id;
115				if( $count )
116					$qs['count'] = ( $count > 200 ) ? 200 : (int) $count;
117				if( $page )
118					$qs['page'] = (int) $page;
119				$query_vars = wp_parse_args( $qs, $defaults );
120				break;
121			case 'public' :
122			default :
123				$this->api_url = 'http://twitter.com/statuses/public_timeline.' . $this->type;
124				break;
125		}
126		if( $query_vars )
127			$this->api_url = $this->api_url . $this->_glue( $query_vars );
128		return $this->_get( $this->api_url );
129	}
130	
131	/**
132	 * Quick method to return 20 most recent results from the public timeline
133	 *
134	 * @access public
135	 * @since 2.0
136	 * @return object
137	 */
138	public function get_public_timeline()
139	{
140		return get_timeline();
141	}
142
143	/**
144	 * Quick method to return 20 most recent results from a user
145	 *
146	 * @access public
147	 * @since 2.0
148	 * @return object
149	 */	
150	public function get_user_timeline( $twitter_name )
151	{
152		return get_timeline( 'user', array('screen_name' => $twitter_name ) );
153	}
154	
155	/**
156	 * Quick method to return 20 most recent mentions
157	 *
158	 * @access public
159	 * @since 2.0
160	 * @return object
161	 */
162	public function get_mentions()
163	{
164		return get_timeline( 'mentions' );
165	}
166	
167	/**
168	 * Retrieve a single tweet by ID
169	 *
170	 * @access public
171	 * @since 2.0
172	 * @param integer $tweet_id 
173	 * @return object
174	 */
175	public function get_tweet( $tweet_id )
176	{
177		$this->api_url = 'http://twitter.com/statuses/show/' . (int) $tweet_id . $this->type;
178		return $this->_get( $this->api_url );
179	}
180	
181	/**
182	 * Post a tweet
183	 *
184	 * @access public
185	 * @since 2.0
186	 * @param array $tweet An array of key/value pairs. The only required key is 'status'
187	 *  - status: REQUIRED. Cannot exceed 140 characters. You do not need to URL encode it but status will be truncated if, after URL encoding, the status exceeds 140 charachters
188	 *  - in_reply_to_status_id: The Tweet ID being responded to. The Tweet replied to must have the authenticating Twitter user mentioned in the status
189	 *  - lat: COMING SOON. A latitude between -90.0 and +90.0
190	 *  - long: COMING SOON. A longitude between -180.0 and +180.0
191	 * @return object
192	 */
193	public function post_tweet( $tweet )
194	{
195		if( !is_array( $tweet ) )
196		{
197			$newtweet = array('status' => $tweet );
198			$tweet = $newtweet;
199		}
200		$this->api_url = 'http://twitter.com/statuses/update.' . $this->type;
201		return $this->_post( $this->api_url, $tweet );
202	}
203	
204	/**
205	 * Deletes a tweet by ID
206	 *
207	 * @access public
208	 * @since 2.0
209	 * @param integer $tweet_id 
210	 * @return object
211	 */
212	public function delete_tweet( $tweet_id )
213	{
214		$this->api_url = 'http://twitter.com/statuses/destroy/' . (int) $tweet_id . $this->type;
215		return $this->_post( $this->api_url, array( 'id' => (int) $tweet_id ) );
216	}
217	
218	public function do_retweet( $tweet_id )
219	{
220		# COMING SOON TO TWITTER
221	}
222	public function get_retweets( $tweet_id )
223	{
224		# COMING SOON TO TWITTER
225	}
226	
227	public function my_retweets()
228	{
229		# COMING SOON TO TWITTER
230	}
231	
232	public function has_retweets()
233	{
234		# COMING SOON TO TWITTER
235	}
236	
237	public function friends_retweets()
238	{
239		# COMING SOON TO TWITTER
240	}
241	
242	/**
243	 * Destroys the object
244	 *
245	 * @access public
246	 * @since 2.0
247	 * @return null
248	 */
249	public function __destruct() {}
250}