Tuesday 2 December 2008

Using Tweet-SQL to post multiple updates

Many Twitter users seem to post large numbers of updates to promote news, events, or their websites. Doing this manually would be rather time consuming. So here's how easy it is to do with Tweet-SQL...

First lets create a database specifically for this purpose.

-- Create a db called twitter

CREATE DATABASE twitter;


Now create a table and insert some Tweets


USE twitter;

 

-- Create a table to hold our tweets

CREATE TABLE TwitterPosts

(

      id INT PRIMARY KEY IDENTITY(1, 1),

      tweet VARCHAR(140) NOT NULL

);

 

 

-- Post some links to YouTube videos of

-- Peter Sellers in the Pink Panther films

INSERT INTO TwitterPosts

(

      tweet

)

SELECT 'Does your dog bite? http://uk.youtube.com/watch?v=SXn2QVipK2o'

UNION ALL

SELECT 'Not now Kato! http://uk.youtube.com/watch?v=c_Jb2Vd4BJY'

UNION ALL

SELECT 'The lift fart... http://uk.youtube.com/watch?v=ViYclyAkkDg'

UNION ALL

SELECT 'The bath scene... http://uk.youtube.com/watch?v=AtLbsyx-1f4'

UNION ALL

SELECT 'The opening to "The Pink Panther"... http://uk.youtube.com/watch?v=HhHwnrlZRus'

UNION ALL

SELECT 'The famous Theme music performed by a live band... http://uk.youtube.com/watch?v=jBupII3LH_Q'

UNION ALL

SELECT 'Outakes... http://uk.youtube.com/watch?v=h0vW5O42cFA'

UNION ALL

SELECT 'The Revenge of the Pink Panther... http://uk.youtube.com/watch?v=r1cbl0zwFws'

UNION ALL

SELECT 'The Pink Panther Strikes Again... http://uk.youtube.com/watch?v=1SG4xb_o8BM'

UNION ALL

SELECT 'Can Steve Martin compare? http://uk.youtube.com/watch?v=iUCDhvbQFmU';

Here's the TSQL script that makes posting your table of Tweets easy...

-- Setup variables for tweeting

DECLARE @tweet VARCHAR(140),

            @id INT;

 

-- Turn off resultsets from Tweet-SQL

EXEC dbo.tweet_cfg_resultset_send 0;

 

-- Setup a cursor

DECLARE tweetCursor CURSOR LOCAL FAST_FORWARD FOR SELECT id

                                                  FROM dbo.TwitterPosts;

 

-- Open the cursor and get the first row

OPEN tweetCursor;

FETCH NEXT FROM tweetCursor INTO @id;

 

WHILE (@@FETCH_STATUS = 0)

BEGIN

 

      -- Get the Tweet to post

      SET @tweet = (SELECT tweet FROM dbo.TwitterPosts WHERE id = @id);

     

      -- Tweet!

      EXEC dbo.tweet_sts_update @tweet, null;

      PRINT 'Posted tweet with id = ' + CAST(@id AS VARCHAR(4)) + ', "' + @tweet + '".';

 

      -- Wait for a while to avoid hitting the API limit

      -- One minute delay here

      WAITFOR DELAY '00:01';

 

      -- Get the next row

      FETCH NEXT FROM tweetCursor INTO @id;

 

END

 

-- Clean up

CLOSE tweetCursor;

DEALLOCATE tweetCursor;

 

-- Turn on resultsets from Tweet-SQL as appropriate

EXEC dbo.tweet_cfg_resultset_send 1;

That's it! If all goes well SQL Server Management Studio will return the following...

200 OK: everything went awesome.
Posted tweet with id = 1, "Does your dog bite? http://uk.youtube.com/watch?v=SXn2QVipK2o".
200 OK: everything went awesome.
Posted tweet with id = 2, "Not now Kato! http://uk.youtube.com/watch?v=c_Jb2Vd4BJY".
200 OK: everything went awesome.
Posted tweet with id = 3, "The lift fart... http://uk.youtube.com/watch?v=ViYclyAkkDg".
200 OK: everything went awesome.
Posted tweet with id = 4, "The bath scene... http://uk.youtube.com/watch?v=AtLbsyx-1f4".
200 OK: everything went awesome.
Posted tweet with id = 5, "The opening to "The Pink Panther"... http://uk.youtube.com/watch?v=HhHwnrlZRus".
200 OK: everything went awesome.
Posted tweet with id = 6, "The famous Theme music performed by a live band... http://uk.youtube.com/watch?v=jBupII3LH_Q".
200 OK: everything went awesome.
Posted tweet with id = 7, "Outakes... http://uk.youtube.com/watch?v=h0vW5O42cFA".
200 OK: everything went awesome.
Posted tweet with id = 8, "The Revenge of the Pink Panther... http://uk.youtube.com/watch?v=r1cbl0zwFws".
200 OK: everything went awesome.
Posted tweet with id = 9, "The Pink Panther Strikes Again... http://uk.youtube.com/watch?v=1SG4xb_o8BM".
200 OK: everything went awesome.
Posted tweet with id = 10, "Can Steve Martin compare? http://uk.youtube.com/watch?v=iUCDhvbQFmU".

No comments: