Re: Twitter examples with JScript

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 24 Jun 2009 15:46:55 -0400 (EDT)

I added another example to the archive
http://EmpowermentZone.com/TwitterExamples.zip

This one uses the Twitter search API and parses returned data in XML
format.  Also pasted below.

Jamal

/*
Search Twitter via the Windows Script Host and JScript (JavaScript on
Windows)
Public domain by Jamal Mazrui
June 24, 2009
Syntax:
CScript.exe /nologo SearchTwitter.js %1 %2 %3
*/

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

// Convert search text to the encoded format needed
sData = encodeURIComponent(sText)
sData = 'q=' + sData

// Set URL for this Twitter request
sUrl = 'http://search.twitter.com/search.atom'

// 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)
// oRequest.setRequestHeader('X-Twitter-Client', 'TwitterExamples')

// 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

oDoc = new ActiveXObject('MSXML2.DOMDocument')
oDoc.loadXML(sResponse)
aMessages = oDoc.getElementsByTagName('entry')
iLength = aMessages.length
for (i = 0; i < iLength; i++) {
oMessage = aMessages[i]
sAuthor = oMessage.selectSingleNode('author/name').text
sTitle = oMessage.selectSingleNode('title').text
WScript.Echo(sAuthor + ': ' + sTitle)
}

On Fri,
19 Jun
2009,
Jamal
Mazrui
wrote:

> Date: Fri, 19 Jun 2009 15:06:54 -0400 (EDT)
> From: Jamal Mazrui <empower@xxxxxxxxx>
> Reply-To: programmingblind@xxxxxxxxxxxxx
> To: ProgrammingBlind@xxxxxxxxxxxxx
> Subject: Twitter examples with JScript
>
> >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
>
__________
View the list's information and change your settings at 
//www.freelists.org/list/programmingblind

Other related posts: