'-------------------------------------------------------------------------------------------------------------------------------------------------- ' TWITTER integrtation with HOMESEER ' by DJF3 - http://domotica.nldj.com - domotica@nldj.com ' version 0.2 - Sep 13 2009 ' Added variables for 'wget.exe' path and Twitter username and password ' Fine-tuned debug levels (0, 1 or 2) ' Planned to: remove requirement for space after vv/ss, add option for date and time (seperately), add special character for ss/vv/date/time (feedback jruys) ' Removed "Add Date" parameter. Use the "dd[]" instead ' Added code that checks if the device exists or not ' version 0.1 - May 22 2009 ' The string send to twitter will be stripped of HTML tags. (important for device-strings) 'PARAMETERS: ' strTwitter: text you want to send. use ' 'vv[]' to insert VALUE of a HS device ' 'ss[]' to insert the device STRING ' 'dd[] to insert a date. can contain D=day, DD=day-short, DDD=day-full, M=month, MM=month-short, MMM=month-full, ' YYYY=year, hh=hours, mm=minutes and characters like / - : etc ' strSend: (optional) actually send the twitt? 'yes' or 'no'. if empty it is set to 'yes', 'no' used for debugging 'TODO (see http://domotica.nldj.com for detailed instructions) ' Download wget and put in HomeSeer directory ' Change 3 variables below (username/password/wget-path) ' ' Example: ("Main","Front door is ss[^7] and the temperature is vv[T8] on dd[DDD, D MMM YYYY]") ' Example: ("Main","The temperature is vv[T8] |no") (if you only want to log the event and not actually send it) ' 'DONE - SEV3: Remove leading and trailing spaces from "ss" output, because output "power used:ss^9 ." looks like "power used:[2 spaces]12kWh[space]." 'DONE - SEV2: Error if string passed has a variable at end but no space. Example "Test vv^1|yes|yes" fails but "Test vv^1 |yes|yes" works 'DONE - SEV3: Remove requirement to have a space after "ss" command. "ss^9 test" is accepted, "ss^9test" or "ss^09test" strips the "test" text. Suggestion: force nr 2 digits or terminate with character. Maybe "ss[^09]"? 'DONE - SEV4: Would like option to only inject time (so not time *and* date) and at beginning of string. Maybe "dd" to inject date and "tt" for time? 'DONE - SEV4: Strings may contain "ss" or "vv" which will upset the system - use escape character? "~ss or %ss?" 'SEV4: Work out issue with "+" and "-". Twitter accepts them but maybe this is an issue with the command line tool? '-------------------------------------------------------------------------------------------------------------------------------------------------- sub Main(byVal parm As Object) dim strDebug as String dim strTwitter,strTwitDate as String dim strSend as String dim varCounter, varVersion as String dim vvStart, vvEnd, Counter as Integer dim ssStart, ssEnd as Integer dim ddStart, ddEnd as Integer dim strTwitterUser, strTwitterPwd, strWGETdirectory as String strWGETdirectory = "C:\Program Files\HomeSeer 2\" strTwitterUser = "enter-your-twitter-accountname-here" strTwitterPwd = "enter-your-twitter-password-here" strDebug = 1 varVersion = "0.2" strTwitter = hs.stringitem(parm, 1, "|") strSend = hs.stringitem(parm, 2, "|") If strSend = "" then strSend = "yes" varVersion = "DJ_Twitter_" + varVersion If strDebug > 0 then hs.writelog(varVersion,"Starting " + varVersion) 'DJ:======================= DD = DATE and/or TIME =========================== ddStart = instr(strTwitter,"dd[")+3 varCounter = ubound(split(lcase(strTwitter), lcase("dd["))) If strDebug > 1 and varCounter > 0 then hs.writelog(varVersion,"TOTAL DD="+Cstr(varCounter)+"") ddStart = instr(strTwitter,"dd[")+3 Counter = 0 While Counter < varCounter ddEnd = (instr(ddStart+1, strTwitter, "]"))- ddStart If strDebug > 1 then hs.writelog(varVersion,"dd start:" + Cstr(ddStart) + " ddend:" + Cstr(ddEnd)) If strDebug > 1 then hs.writelog(varVersion,"dd strTwitter="+strTwitter) If strDebug > 1 then hs.writelog(varVersion,"dd Device:" + Mid(strTwitter, ddStart, ddEnd) + ":") strTwitDate = Mid(strTwitter, ddStart, ddEnd) strTwitDate = Replace(strTwitDate,"DDD",WeekDayName(Weekday(Now),FALSE)) strTwitDate = Replace(strTwitDate,"DD",WeekDayName(Weekday(Now),TRUE)) strTwitDate = Replace(strTwitDate,"D",Cstr(Day(Now))) strTwitDate = Replace(strTwitDate,"MMM",MonthName(Month(Now),FALSE)) strTwitDate = Replace(strTwitDate,"MM",MonthName(Month(Now),TRUE)) strTwitDate = Replace(strTwitDate,"M",Cstr(Month(Now))) strTwitDate = Replace(strTwitDate,"YYYY",Cstr(Year(Now))) strTwitDate = Replace(strTwitDate,"hh",Cstr(Hour(Now))) If len(Cstr(Minute(Now))) < 2 then 'DJ: If the minutes are < 10, prefix a '0'. Otherwise the time is 14:5 instead of 14:05 strTwitDate = Replace(strTwitDate,"mm","0"+Cstr(Minute(Now))) Else strTwitDate = Replace(strTwitDate,"mm",Cstr(Minute(Now))) End if If strDebug > 1 then hs.writelog(varVersion,"strTwitDate:" + strTwitDate) strTwitter = replace(strTwitter, Mid(strTwitter, ddStart-3, ddEnd+4), strTwitDate) Counter = Counter + 1 If strDebug > 1 then hs.writelog(varVersion,"dd Counter:" + Cstr(Counter)) ddStart = instr(ddStart+ddEnd, strTwitter,"dd[")+3 If strDebug > 0 then hs.writelog(varVersion,"dd strTwitter="+strTwitter) End While 'DJ:======================= VV = VALUE =========================== varCounter = ubound(split(lcase(strTwitter), lcase("vv["))) If strDebug > 1 and varCounter > 0 then hs.writelog(varVersion,"TOTAL VV="+Cstr(varCounter)+"") vvStart = instr(strTwitter,"vv[")+3 Counter = 0 While Counter < varCounter vvEnd = (instr(vvStart+1, strTwitter, "]"))- vvStart If strDebug > 1 then hs.writelog(varVersion,"vv start:" + Cstr(vvStart) + " vvend:" + Cstr(vvEnd)) If strDebug > 1 then hs.writelog(varVersion,"vv strTwitter="+strTwitter) If strDebug > 1 then hs.writelog(varVersion,"vv Device:" + Mid(strTwitter, vvStart, vvEnd) + ":") hs.writelog(varVersion,"device id: " + Mid(strTwitter, vvStart, vvEnd) + " which means it exists:" + Cstr(hs.deviceexistsref(Mid(strTwitter, vvStart, vvEnd)))) If hs.deviceexistsref(Mid(strTwitter, vvStart, vvEnd)) <> -1 then strTwitter = stripHTML(replace(strTwitter, Mid(strTwitter, vvStart-3, vvEnd+4), Cstr(hs.devicevalue(Mid(strTwitter, vvStart, vvEnd))))) Else hs.writelog(varVersion,"ERROR value: device " + Mid(strTwitter, vvStart, vvEnd) + " does not exist") strTwitter = replace(strTwitter, Mid(strTwitter, vvStart-3, vvEnd+4), "WRONG_DEVICE") End if Counter = Counter + 1 If strDebug > 1 then hs.writelog(varVersion,"vv Counter:" + Cstr(Counter)) vvStart = instr(vvStart+vvEnd, strTwitter,"vv[")+3 If strDebug > 0 then hs.writelog(varVersion,"vv strTwitter="+strTwitter) End While 'DJ:======================= ss = STRING =========================== varCounter = ubound(split(lcase(strTwitter), lcase("ss["))) If strDebug > 1 and varCounter > 0 then hs.writelog(varVersion,"TOTAL SS="+Cstr(varCounter)+"") ssStart = instr(strTwitter,"ss[")+3 Counter = 0 While Counter < varCounter ssEnd = (instr(ssStart+1, strTwitter, "]"))- ssStart If strDebug > 1 then hs.writelog(varVersion,"ss start:" + Cstr(ssStart) + " ssend:" + Cstr(ssEnd)) If strDebug > 1 then hs.writelog(varVersion,"ss strTwitter="+strTwitter) If strDebug > 1 then hs.writelog(varVersion,"ss Device:" + Mid(strTwitter, ssStart, ssEnd) + ":") 'DJ: added 'TRIM' to remove spaces before and after HTML-stripped string (feedback jruys) hs.writelog(varVersion,"device id: " + Mid(strTwitter, ssStart, ssEnd) + " which means it exists:" + Cstr(hs.deviceexistsref(Mid(strTwitter, ssStart, ssEnd)))) If hs.deviceexistsref(Mid(strTwitter, ssStart, ssEnd)) <> -1 then strTwitter = Trim(stripHTML(replace(strTwitter, Mid(strTwitter, ssStart-3, ssEnd+4), hs.devicestring(Mid(strTwitter, ssStart, ssEnd))))) Else hs.writelog(varVersion,"ERROR string: device " + Mid(strTwitter, ssStart, ssEnd) + " does not exist") strTwitter = Replace(strTwitter, Mid(strTwitter, ssStart-3, ssEnd+4), "WRONG_DEVICE") End if Counter = Counter + 1 If strDebug > 1 then hs.writelog(varVersion,"ss Counter:" + Cstr(Counter)) ssStart = instr(ssStart+ssEnd, strTwitter,"ss[")+3 If strDebug > 0 then hs.writelog(varVersion,"ss strTwitter="+strTwitter) End While If strDebug > 0 then hs.writelog(varVersion,"FINAL, Sending:"+strTwitter) If strSend = "yes" then If strDebug > 1 then hs.writelog(varVersion,"Sending is turned ON") If len(strTwitter) < 150 then If strDebug > 1 then hs.writelog(varVersion,"CALLING: hs.launch('"+strWGETdirectory+"wget.exe','--keep-session-cookies --quiet --http-user="+strTwitterUser+" --http-password="+strTwitterPwd+" --post-data=status=+chr(34)+"+strTwitter+"+chr(34)+http://twitter.com:80/statuses/update.xml") hs.launch(strWGETdirectory & "wget.exe","--keep-session-cookies --quiet --http-user="+strTwitterUser+" --http-password="+strTwitterPwd+" --post-data=status="+chr(34)+strTwitter+chr(34)+" http://twitter.com:80/statuses/update.xml") Else hs.writelog(varVersion,"Error: Twitts cannot exceed 150 characters. This one was "+Cstr(len(strTwitter))+"
String:" + strTwitter+ "") strTwitter = replace(strTwitter, Mid(strTwitter, vvStart-3, vvEnd+4), "") End if else hs.writelog(varVersion,"Posting has been turned OFF") End if end sub Function stripHTML(strHTML) 'Strips the HTML tags from strHTML using split and join. Ensure that strHTML contains something If len(strHTML) = 0 then stripHTML = strHTML Exit Function End If dim arysplit, i, j, strOutput arysplit = split(strHTML, "<") 'Assuming strHTML is nonempty, we want to start iterating from the 2nd array postition if len(arysplit(0)) > 0 then j = 1 else j = 0 for i=j to ubound(arysplit) 'Loop through each instance of the array if instr(arysplit(i), ">") then 'Do we find a matching > sign? If so, snip out all the text between the start of the string and the > sign arysplit(i) = mid(arysplit(i), instr(arysplit(i), ">") + 1) else 'Ah, the < was was nonmatching arysplit(i) = "<" & arysplit(i) end if next strOutput = join(arysplit, "") 'Rejoin the array into a single string strOutput = mid(strOutput, 2-j) 'Snip out the first < strOutput = replace(strOutput,">",">") 'Convert < and > to < and > strOutput = replace(strOutput,"<","<") stripHTML = Replace(Replace(strOutput, Chr(194), ""), Chr(160), "") End Function