[twitter]

Description

Link: [twitter]
Author: Jason Huck
Category: Utility
Version: 8.5.x
License: Public Domain
Posted: Jan. 04, 2008
Updated: Jan. 03, 2011
More by this author...

This custom type provides an interface to the Twitter API from Lasso. It requires Fletcher Sandbeck's JSON library for Lasso and the [string_truncate] tag here. In addition to the usage examples below, consult the official documentation for a complete list of supported methods. A few method names were changed slightly to avoid duplicate member tag names. For example, there are several "show" methods, which I renamed to be more specific, such as ->show_user and ->show_status. A couple methods required special formatting for dates, so a [date_toHTTP] tag is included in this download.

Parameters

none


Sample Usage

// create a new instance of the twitter type
var('t') = twitter(
	-username='xxxxxxxxx',
	-password='xxxxxxxxx'
);

// get the first item from the public timeline
$t->public_timeline->first;

// get a slice of your friends timeline
$t->friends_timeline( -since=date->subtract( -hour=2)&);

// get 4 items from the user timeline
$t->user_timeline( -count=4);

// show a specific status message
$t->show_status(xxxxxxxxx);

// update your twitter status
$t->update('This Tweet Powered By Lasso.');

// get all replies
$t->replies;

// get a list of your friends
$t->friends;

// get a list of your followers (the -lite option does not appear to work)
$t->followers( -lite=true);

// DEPRECATED: get the current featured twitterers
// $t->featured;

// view all of your direct messages
$t->direct_messages;

// view all of your sent direct messages
$t->sent;

// send a new direct message to a specific user
$t->new( -user='xxxxxxxxx', -text='Sent from Lasso. Let me know if you get this.');
						

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
// Twitter API for Lasso
// (c)2007 - 2011 Jason Huck/Core Five Creative
// Thanks to Lieven Gekiere, Brian Loomis, and Rich Fortnum for contributions.
// requires: JSON library, string_truncate, date_toHTTP (below)

define_tag(
	'toHTTP',
	-namespace='date_',
	-req='in', -type='date',
	-priority='replace',
	-encodenone,
	-description='HTTP-formats the given date.'
);
	local('out') = null;
	!#in->gmt ? #in = date_localtogmt(#in);
	#in->setformat('%Q %T');
	#out = encode_url(string(#in))->replace('%20','+')&;
	return(#out);	
/define_tag;


define_type(
	'twitter',
	-description='Implements the Twitter API in Lasso.'
);
	local(
		'username' = string,
		'password' = string,
		'version' = 1
	);
	
	define_tag(
		'oncreate',
		-opt='username', -type='string',
		-opt='password', -type='string',
		-opt='version'
	);
		local_defined('username') ? self->username = #username;
		local_defined('password') ? self->password = #password;
		local_defined('version') ? self->version = #version;
	/define_tag;

	define_tag('authcheck');
		return(self->username != '' && self->password != '');
	/define_tag;
	
	define_tag(
		'retrieve', 
		-req='path', -type='string',
		-opt='post', -type='array',
		-encodenone
	);
		fail_if(!self->authcheck, -1, 'Username or password not set.');
		
		protect;
			local('response') = string(
				local_defined('post') ? include_url(
					'http://api.supertweet.net/' + self->version + #path,
					-username=self->username,
					-password=self->password,
					-postparams=#post,
					-timeout=15
				) | include_url(
					'http://api.supertweet.net/' + self->version + #path,
					-username=self->username,
					-password=self->password,
					-timeout=15
				)
			);
			
			handle_error;
				local('response') = 'There was a problem communicating with Twitter.';
			/handle_error;
		/protect;
		
		return(decode_json(#response));
	/define_tag;
	
	define_tag(
		'public_timeline',
		-opt='since_id', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/public_timeline.json' + (local_defined('since_id') ? '?since_id=' + #since_id);
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'friends_timeline',
		-opt='id',
		-opt='since', -type='date',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/friends_timeline' + (local_defined('id') ? '/' + #id) + '.json';
		local_defined('since') || local_defined('page') ? #path += '?';
		local_defined('since') ? #path += 'since=' + date_toHTTP(#since);
		local_defined('page') ? #path += (!#path->endswith('?') ? '&') + 'page=' + #page;
		return(self->retrieve(#path));			
	/define_tag;
	
	define_tag(
		'user_timeline',
		-opt='id',
		-opt='count', -type='integer',
		-opt='since', -type='date',
		-encodenone
	);
		local('path') = '/statuses/user_timeline' + (local_defined('id') ? '/' + #id) + '.json';
		local_defined('count') || local_defined('since') ? #path += '?';
		local_defined('count') ? #path += 'count=' + #count;
		local_defined('since') ? #path += (!#path->endswith('?') ? '&') + 'since=' + date_toHTTP(#since);
		return(self->retrieve(#path));			
	/define_tag;
	
	define_tag(
		'show_status',
		-req='id', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/show/' + #id + '.json';
		return(self->retrieve(#path));			
	/define_tag;
	
	define_tag(
		'update',
		-req='status', -type='string',
		-encodenone
	);
		local('path') = '/statuses/update.json';
		local('post') = array('status' = string_truncate(#status, 160));
		return(self->retrieve(#path, #post));			
	/define_tag;

	define_tag(
		'replies',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/replies.json';
		local_defined('page') ? #path += '?page=' + #page;
		return(self->retrieve(#path));			
	/define_tag;
	
	define_tag(
		'destroy_status',
		-req='id', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/destroy/' + #id + '.json';
		return(self->retrieve(#path));			
	/define_tag;
	
	define_tag(
		'friends',
		-opt='id',
		-encodenone
	);
		local('path') = '/statuses/friends' + (local_defined('id') ? '/' + #id) + '.json';
		return(self->retrieve(#path));			
	/define_tag;
			
	define_tag(
		'followers',
		-opt='id',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/statuses/followers';
		#path += (local_defined('id') ? '/' + #id) + '.json';
		local_defined('page') && #page ? #path += '?page=' + #page;
		return(self->retrieve(#path));
	/define_tag;

	/* deprecated?
	define_tag('featured');
		local('path') = '/statuses/featured.json';
		return(self->retrieve(#path));
	/define_tag;
	*/
	
	define_tag( // not working
		'show_user',
		-opt='id',
		-opt='email', -type='string',
		-encodenone
	);
		local_defined('id') ? local('path') = '/users/show/' + #id + '.json';
		local_defined('email') ? local('path') = '/users/show.json?email=' + #email;
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'direct_messages',
		-opt='since', -type='date',
		-opt='since_id', -type='integer',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/direct_messages.json';
		params->size ? #path += '?';
		local_defined('since') ? #path += 'since=' + date_toHTTP(#since) + '&';
		local_defined('since_id') ? #path += 'since_id=' + #since_id + '&';
		local_defined('page') ? #path += 'page=' + #page;
		#path->removetrailing('&');
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'sent_direct',
		-opt='since', -type='date',
		-opt='since_id', -type='integer',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/direct_messages/sent.json';
		params->size ? #path += '?';
		local_defined('since') ? #path += 'since=' + date_toHTTP(#since) + '&';
		local_defined('since_id') ? #path += 'since_id=' + #since_id + '&';
		local_defined('page') ? #path += 'page=' + #page;
		#path->removetrailing('&');
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'new_direct',
		-req='user',
		-req='text', -type='string',
		-encodenone
	);
		local('path') = '/direct_messages/new.json';
		local('post') = array(
			'user' = #user,
			'text' = string_truncate(#text, -length=140)
		);
		
		return(self->retrieve(#path, #post));
	/define_tag;
	
	define_tag(
		'destroy_direct',
		-req='id', -type='integer',
		-encodenone
	);
		local('path') = '/direct_messages/destroy/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'create_friendship',
		-req='id',
		-encodenone
	);
		local('path') = '/friendships/create/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'destroy_friendship',
		-req='id',
		-encodenone
	);
		local('path') = '/friendships/destroy/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'exists_friendship',
		-req='user_a',
		-req='user_b',
		-encodenone
	);
		local('path') = '/friendships/exists.json?user_a=';
		#path += encode_url(#user_a) + '&user_b=' + encode_url(#user_b);
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'ids_friends',
		-opt='id',
		-encodenone
	);
		local('path') = '/friends/ids';
		local_defined('id') ? #path += '/' + encode_url(#id);
		#path += '.json';
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'ids_followers',
		-opt='id',
		-encodenone
	);
		local('path') = '/followers/ids';
		local_defined('id') ? #path += '/' + encode_url(#id);
		#path += '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'verify_credentials',
		-encodenone
	);
		local('path') = '/account/verify_credentials.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'end_session',
		-encodenone
	);
		local('path') = '/account/end_session';
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'update_location',
		-req='location', -type='string',
		-encodenone
	);
		local('path') = '/account/update_location.json';
		local('post') = array('location' = string_truncate(#location, 100));
		return(self->retrieve(#path, #post));
	/define_tag;

	define_tag(
		'update_delivery_device',
		-req='device', -type='string',
		-encodenone
	);
		fail_if((: 'sms', 'im', 'none') !>> #device, -1, 'Not an allowed device.');
		local('path') = '/account/update_delivery_device.json?device=' + #device;
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'update_profile_colors',
		-opt='profile_background_color',
		-opt='profile_text_color',
		-opt='profile_link_color',
		-opt='profile_sidebar_fill_color',
		-opt='profile_sidebar_border_color',
		-encodenone
	);
		fail_if(!params->size, -1, 'At least one color must be specified.');
		local('path') = '/account/update_profile_colors.json';
		local('post') = array;
		
		iterate(params, local('i'));
			#post->insert(string(#i->first)->removeleading('-')& = #i->second);
		/iterate;
		
		return(self->retrieve(#path, #post));
	/define_tag;

	/* Not currently implemented: 
		update_profile_image, 
		update_profile_background_image
	*/

	define_tag(
		'rate_limit_status',
		-encodenone
	);
		local('path') = '/account/rate_limit_status.json';
		return(self->retrieve(#path));
	/define_tag;

	define_tag(
		'update_profile',
		-opt='name',
		-opt='email',
		-opt='url',
		-opt='location',
		-opt='description',
		-encodenone
	);
		fail_if(!params->size, -1, 'At least one parameter must be provided.');
		local('path') = '/account/update_profile.json';
		local('post') = array;
		
		iterate(params, local('i'));
			#post->insert(string(#i->first)->removeleading('-')& = #i->second);
		/iterate;
		
		return(self->retrieve(#path, #post));
	/define_tag;
	
	/* deprecated?
	define_tag(
		'archive',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/account/archive.json';
		local_defined('page') ? #path += '?page=' + #page;
		return(self->retrieve(#path));
	/define_tag;
	*/
	
	define_tag(
		'favorites',
		-opt='id',
		-opt='page', -type='integer',
		-encodenone
	);
		local('path') = '/favorites' + (local_defined('id') ? '/' + #id) + '.json';
		local_defined('page') ? #path += '?page=' + #page;
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'create_favorite',
		-req='id', -type='integer',
		-encodenone
	);
		local('path') = '/favorites/create/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'destroy_favorite',
		-req='id', -type='integer',
		-encodenone
	);
		local('path') = '/favorites/destroy/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'follow_notification',
		-req='id',
		-encodenone
	);
		local('path') = '/notifications/follow/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'leave_notification',
		-req='id',
		-encodenone
	);
		local('path') = '/notifications/leave/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'create_block',
		-req='id',
		-encodenone
	);
		local('path') = '/blocks/create/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag(
		'destroy_block',
		-req='id',
		-encodenone
	);
		local('path') = '/blocks/destroy/' + #id + '.json';
		return(self->retrieve(#path));
	/define_tag;
	
	define_tag('test');
		local('path') = '/help/test.json';
		return(self->retrieve(#path));
	/define_tag;
/define_type;

 

Related Tags



Comments

11/03/2011, Wade Maxfield
t.co wrapping?
Please ignore: Testing to see if new t.co wrapping breaks hyperlinks in twitterific
01/03/2011, Jason Huck
Updated to reflect API changes.
Updated to include the version number in the API endpoints. Defaults to '1', but can be overridden by passing a value to the -version keyword in the constructor. Thanks to Rich Fortnum for bringing this to my attention.
01/03/2011, Jason Huck
Testing API changes (again).
Testing update to include version number in API endpoints.
09/09/2010, Jason Huck
Okay, one more try. :)
Testing SuperTweet integration.
09/09/2010, Jason Huck
Testing SuperTweet Integration
tagSwap's Twitter notifications should be back up and running now, thanks to SuperTweet!
09/09/2010, Jason Huck
SuperTweet Integration
Updated to use the SuperTweet proxy to work around the need for OAuth support. See http://www.supertweet.net/.
02/24/2009, Jason Huck
Bug Fixes
Corrected several syntax errors caused by some 4am coding.
02/24/2009, Jason Huck
More New Methods
As of this posting, the [twitter] type supports all documented methods of the Twitter API with the exception of ->update_profile_image and ->update_profile_background_image.
02/24/2009, Jason Huck
New Methods Added
Two new methods have been added: ->follows_user, contributed by Brian Loomis, and ->update_location, contributed by Lieven Gekiere.
05/14/2008, Lieven Gekiere
Twitter API updated
The last few weeks the Twitter API has received a few updates.... It is now possible to add your location to your profile... Just add the following lines to the Twitter custom type. define_tag( 'update_location', -req='location', -type='string', -encodenone ); local('path') = '/account/update_location.json'; local('post') = array('location' = string_truncate(#location, 100)); return(self->retrieve(#path, #post)); /define_tag; And post to Twitter via : $t->update_location('My location in the world');
02/02/2008, Jason Huck
Follow tagSwap on Twitter
Thanks to this custom type, you can now receive updates about tagSwap.net via Twitter. Follow tagSwap to get instant notifications whenever tags are added or edited, or when comments are posted. (http://twitter.com/tagSwap)
Email:


Password:



Newest

Most Popular

Support tagSwap.net