Search This Blog

Thursday 16 July 2009

Tweet your files with curl

This post shows how you can let WatchDirectory or WatchFTP post a new twitter message when they detect new files. Excuse me if I get some of the twitter terminology wrong - I grew up when computers were something you see in movies ;-)

Curl

This page shows that posting a message to twitter is as simple as

curl -u username:password -d status="your message here" http://twitter.com/statuses/update.json

So, we need Curl to post. I used the non-ssl version while testing. Make sure to download a Windows version from their download page.

Batch File

If you want to "tweet" when a new file is detected, create a new task and select the Run a Batch Script task (for WatchDirectory) or Run a Batch Script Action (for WatchFTP).

Let it run the following batch file (may not show correctly in all blog-readers):

@echo off
SET CURL=E:\bin\curl-7.19.5\curl.exe
SET TWUSER=TheTwitterUser
SET TWPASS=TheTwitterPassword

rem if we are called by WatchFTP, copy its variables to WD variables
rem so this script is compatible with both programs

IF "%WF_REASON%" NEQ "" (
  SET WD_REASON=%WF_REASON%
  SET WD_FILE=%WF_FILE%
)


GOTO :%WD_REASON%

:FILENEW
SET MSG="New file: %WD_FILE%"
GOTO :SendIt
:FILEDEL
SET MSG="Deleted file: %WD_FILE%"
GOTO :SendIt
:FILECHNG
SET MSG="Changed file: %WD_FILE%"
GOTO :SendIt
:FILEREN
SET MSG="Renamed file: %WD_OFILE% to %WD_FILE%"
GOTO :SendIt
:DIRNEW
SET MSG="New directory: %WD_FILE%"
GOTO :SendIt
:DIRDEL
SET MSG="Deleted directory: %WD_FILE%"
GOTO :SendIt
:DIRREN
SET MSG="Renamed directory: %WD_OFILE% to %WD_FILE%"
GOTO :SendIt


:SendIt
"%CURL%" -u %TWUSER%:%TWPASS% -d status=%MSG% http://twitter.com/statuses/update.json

Make sure to properly change the first few lines (where did you install curl, what is your twitter userid and password).

Twitter Limits

A tweet is a short message (I believe limited to about 140 characters). If your directory and filenames are long, you may need to shorten the messages a bit. For example, change %WD_FILE% to %WD_FILE_N% everywhere in the script so it will only tweet the filename (without the directory path).

Twitter also has some other limits you may hit if you call it too often.

Blogging your Files?

This post shows you how.

No comments: