Twitter examples with JScript

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: ProgrammingBlind@xxxxxxxxxxxxx
  • Date: Fri, 19 Jun 2009 15:06:54 -0400 (EDT)

From the archive
http://EmpowermentZone.com/TwitterExamples.zip


Here are two examples of using the Twitter API via the Windows Script Host
and JScript (JavaScript on Windows).  These may be run at a command prompt
without external libraries.  Occasionally, a computer does not seem to
include the Windows Script host, so get it from microsoft.com if needed.

The source code in PostTweet.js is an example of posting a tweet.  The
batch file, RunPostTweet.bat, takes three parameters:  the Twitter user
name, password, and text of the tweet.  Enclose a parameter in quotes if
it contains a space.

GetTweets.js is an example of getting a page of tweets (20 messages per
page).  RunGetTweets.bat also takes three parameters:  the Twitter user
name, password, and page number.

The Twitter.com web server requires a POST request if its database is to
be changed, and a GET request otherwise.  A POST request sends data in the
body of the request, whereas a GET request sends data in a query string
appended to the URL.  Such data must be encoded so that certain, special
characters are preserved over HTTP communication.

A client request can include a header with authentication credentials -- a
user name and password.  One of the server response headers indicates
whether the request is successful.

The examples illustrate these issues using the Windows COM server for HTTP
and XML operations.  Also shown is the use of data returned in JSON format
(JavaScript Object Notation).
----------

[Content of PostTweet.js]

/*
Syntax:
        CScript.exe /nologo PostTweet.js %1 %2 %3
*/

// Get command-line parameters for Twitter user, password, and text of
tweet
sUser = WScript.Arguments(0)
sPassword = WScript.Arguments(1)
sText = WScript.Arguments(2)

// Convert tweet text to the encoded format needed
sText = 'status=' + sText
sData = encodeURI(sText)

// Set URL for this Twitter request
sUrl = 'http://twitter.com/statuses/update.json'

// Create the request object from the Windows COM server for XML
operations
oRequest = new ActiveXObject('MSXML2.XMLHTTP')

// Prepare the request with appropriate parameters
oRequest.Open('POST', sUrl, false, sUser, sPassword)

// Send the request to twitter.com (include data in the body for a POST
request)
oRequest.Send(sData)

// Show the status of the HTTP response from the web server, indicating
whether the tweet was posted successfully
sStatus = oRequest.statusText
WScript.Echo(sStatus)


----------

[Content of GetTweets.js]

/*
Syntax:
CScript.exe /nologo GetTweets.js %1 %2 %3
*/

// Function for testing data in JSON format (JavaScript Object Notation)
function printObject(sName, oValue) {
sType = typeof(oValue)
if (sType != 'object') return WScript.echo(sName + ', ' + sType + ', ' +
oValue)

WScript.Echo(sName + ', ' + sType)
for (sAttribute in oValue) {
vValue = oValue[sAttribute]
sType = typeof(vValue)
if (sType == 'object') printObject(sAttribute, vValue)
else WScript.Echo(sAttribute + ', ' + sType + ', ' + vValue)
}
}

// Get command-line parameters for Twitter user, password, and page of
tweets (20 messages per page)
sUser = WScript.Arguments(0)
sPassword = WScript.Arguments(1)
sPage = WScript.Arguments(2)

// Convert page number to the encoded format needed
sPage = 'page=' + sPage
sData = encodeURIComponent(sPage)

// Set URL for this Twitter request
sUrl = 'http://twitter.com/statuses/friends_timeline.json'

// Add the query string to the URL (used by a GET  request)
sUrl += '?' + sData

// Create the request object from the Windows COM server for XML
operations
oRequest = new ActiveXObject('MSXML2.XMLHTTP')

// Prepare the request with appropriate parameters
oRequest.Open('GET', sUrl, false, sUser, sPassword)

// Send the request to twitter.com
oRequest.Send()

// Check for success, and abort if not
sStatus = oRequest.statusText
if (sStatus != 'OK') {
WScript.Echo(sStatus)
WScript.Quit()
}

// Get the web server response
sResponse = oRequest.responseText

// Ensure the response will be interpreted as a JScript expression
sResponse = '(' + sResponse + ')'

// Evaluate the expression, thereby creating an array of message objects
aMessages = eval(sResponse)

// Uncomment the following line to print the complete response
// printObject('Response', aMessages)

// Print the sender and text of each message
iLength = aMessages.length
for (i = 0; i < iLength; i++) {
oMessage = aMessages[i]
sSender = oMessage.user.screen_name
sText = oMessage.text
WScript.Echo(sSender + ': ' + sText)
}

__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: