The files contained in this repository can be downloaded to your computer using a svn client.
On Linux you simply type the command displayed below.
This URL has Read-Only access.
root / trunk / DomotiGa / Twitter.module @ 339
History | View | Annotate | Download (2.4 kB)
| 1 | ' Gambas module file |
|---|---|
| 2 | |
| 3 | ' Description: |
| 4 | ' Twitter.module |
| 5 | ' Support for sending tweets to twitter.com |
| 6 | |
| 7 | ' Development Status: |
| 8 | ' Working. |
| 9 | |
| 10 | ' DomotiGa - an open source home automation program. |
| 11 | ' Copyright(C) 2008-2010 Ron Klinkien |
| 12 | |
| 13 | ' Read file called COPYING for license details. |
| 14 | |
| 15 | PUBLIC hTweet AS NEW HttpClient AS "hTweet" |
| 16 | |
| 17 | PUBLIC SUB PostTweet(sMessage AS String) |
| 18 | |
| 19 | ' if not enabled, return |
| 20 | IF NOT Main.bTwitterEnabled THEN RETURN |
| 21 | |
| 22 | ' if we are already sending a tweet return |
| 23 | IF hTweet.Status > 0 THEN |
| 24 | Main.WriteLog(("I'm already sending a Tweet, skipping."))
|
| 25 | RETURN |
| 26 | END IF |
| 27 | |
| 28 | ' use httpclient to post tweet to service |
| 29 | hTweet.URL = "http://twitter.com/statuses/update.xml?status=" & EncodeHTML(sMessage) |
| 30 | hTweet.Auth = 1 |
| 31 | hTweet.Async = TRUE |
| 32 | hTweet.User = Main.sTwitterUser |
| 33 | hTweet.Password = Main.sTwitterPassword |
| 34 | hTweet.TimeOut = 10 |
| 35 | hTweet.Post("text/plain", " ")
|
| 36 | |
| 37 | END |
| 38 | |
| 39 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 40 | ' replace signs with html amp code |
| 41 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 42 | PRIVATE FUNCTION EncodeHTML(sStr AS String) AS String |
| 43 | |
| 44 | DIM iPos AS Integer |
| 45 | DIM sRes, sCar AS String |
| 46 | |
| 47 | FOR iPos = 1 TO Len(sStr) |
| 48 | sCar = Mid$(sStr, iPos, 1) |
| 49 | SELECT sCar |
| 50 | CASE "<" |
| 51 | sCar = "<" |
| 52 | CASE ">" |
| 53 | sCar = ">" |
| 54 | CASE "&" |
| 55 | sCar = "&" |
| 56 | CASE "\"" |
| 57 | sCar = """ |
| 58 | CASE " " |
| 59 | sCar = "%20" |
| 60 | END SELECT |
| 61 | sRes = sRes & sCar |
| 62 | NEXT |
| 63 | |
| 64 | RETURN sRes |
| 65 | |
| 66 | END |
| 67 | |
| 68 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 69 | ' catch error |
| 70 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 71 | PUBLIC SUB hTweet_Error() |
| 72 | |
| 73 | Main.WriteDebugLog(("[Twitter] Error ") & hTweet.Status)
|
| 74 | |
| 75 | END |
| 76 | |
| 77 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 78 | ' check http return code |
| 79 | '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 80 | PUBLIC SUB hTweet_Finished() |
| 81 | |
| 82 | DIM iCount AS Integer |
| 83 | |
| 84 | ' select on http result code and display message |
| 85 | SELECT hTweet.Code |
| 86 | CASE 200 |
| 87 | Main.WriteLog(("I have sent a Tweet to 'http://twitter.com/") & Main.sTwitterUser & "'.")
|
| 88 | CASE 401 |
| 89 | Main.WriteLog(("Error authenticating while sending a Tweet!"))
|
| 90 | CASE ELSE |
| 91 | Main.WriteLog(("Unknown error occured while sending a Tweet!"))
|
| 92 | END SELECT |
| 93 | |
| 94 | ' if debug is on print all http headers |
| 95 | IF NOT Main.bTwitterDebug THEN RETURN |
| 96 | FOR iCount = 0 TO hTweet.Headers.Count - 1 |
| 97 | Main.WriteDebugLog("[Twitter] " & hTweet.Headers[iCount], 1)
|
| 98 | NEXT |
| 99 | |
| 100 | END |
