99_twitter.pm 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. ##############################################
  2. #
  3. # Twitter for FHEM
  4. #
  5. ##############################################
  6. #
  7. # Copyright notice
  8. #
  9. # (c) 2010
  10. # Copyright: Axel Rieger (fhem BEI anax PUNKT info)
  11. # All rights reserved
  12. #
  13. # This script free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation; either version 2 of the License, or
  16. # (at your option) any later version.
  17. #
  18. # The GNU General Public License can be found at
  19. # http://www.gnu.org/copyleft/gpl.html.
  20. # A copy is found in the textfile GPL.txt and important notices to the license
  21. # from the author is found in LICENSE.txt distributed with these scripts.
  22. #
  23. # This script is distributed in the hope that it will be useful,
  24. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. # GNU General Public License for more details.
  27. #
  28. ##############################################
  29. # Sends Twitter-MSG
  30. #
  31. # Usage:
  32. # twitter <MESSAGE>
  33. # Beispiel
  34. # twitter "MEINE TWITTER NACHRICHT"
  35. # per AT-Job alle 15min
  36. # define TW001 at +00:15:00 {my $msg = TimeNow() . " " . $defs{<DEVICE-NAME>}{'STATE'} ;; fhem "twitter $msg"}
  37. #
  38. # twitter STATUS
  39. # Shows status of twitter-command:
  40. #--------------------------------------------------------------------------------
  41. #TWITTER
  42. #--------------------------------------------------------------------------------
  43. # Status: Enabeld
  44. # Message-Count: 17
  45. # Message-Count-Max: 1000
  46. # Error-Count: 0
  47. # Error-Count-Max: 5
  48. # Last Message: <TIMESTAMP>
  49. # Last Message: <MESSAGE>
  50. #--------------------------------------------------------------------------------
  51. #
  52. # twitter ENABLE/DISABLE
  53. # Enables/disbales twitter-command
  54. #
  55. # Twitter Limits
  56. # http://help.twitter.com/forums/10711/entries/15364
  57. # Updates: 1,000 per day. The daily update limit is further broken down into
  58. # smaller limits for semi-hourly intervals. Retweets are counted as updates.
  59. #
  60. # My Limits ;-)
  61. # LPW-Timeout 5sec
  62. # Max-Errors = 10
  63. ##############################################
  64. package main;
  65. use strict;
  66. use warnings;
  67. use POSIX;
  68. use Data::Dumper;
  69. use LWP::UserAgent;
  70. use vars qw(%data);
  71. use vars qw(%cmds);
  72. sub Commandtwitter($);
  73. #####################################
  74. sub
  75. twitter_Initialize($)
  76. {
  77. # Config-Data
  78. # Account-Data
  79. $data{twitter}{login}{user} = "********";
  80. $data{twitter}{login}{pwd} = "********";
  81. # disabled ?? -> enable
  82. if(defined($data{twitter}{conf}{disabeld})){
  83. delete $data{twitter}{conf}{disabeld};
  84. }
  85. # $data{twitter}{conf}{disabeld} = 1;
  86. # Max 1000 MSGs per Day
  87. $data{twitter}{conf}{msg_count} = 0;
  88. $data{twitter}{conf}{day} = (localtime(time()))[3];
  89. # Error-Counter
  90. $data{twitter}{error} = 0;
  91. $data{twitter}{error_max} = 5;
  92. $data{twitter}{last_msg} = "";
  93. $data{twitter}{last_msg_time} = "";
  94. #FHEM-Command
  95. $cmds{twitter}{Fn} = "Commandtwitter";
  96. $cmds{twitter}{Hlp} = "Sends Twitter Messages (max. 140 Chars): twitter MSG";
  97. }
  98. #####################################
  99. sub Commandtwitter($)
  100. {
  101. my ($cl, $msg) = @_;
  102. if(!$msg) {
  103. Log 0, "TWITTER[ERROR] Keine Nachricht";
  104. return undef;
  105. }
  106. #Show Status
  107. my $status;
  108. $status .= "--------------------------------------------------------------------------------\n";
  109. $status .= "TWITTER \n";
  110. $status .= "--------------------------------------------------------------------------------\n";
  111. if($msg eq "STATUS"){
  112. if(defined($data{twitter}{conf}{disabeld})){$status .= "\t Status: Disbaled\n";}
  113. else{$status .= "\t Status: Enabeld\n";}
  114. $status .= "\t Message-Count: " . $data{twitter}{conf}{msg_count} . "\n";
  115. $status .= "\t Message-Count-Max: 1000\n";
  116. $status .= "\t Error-Count: " . $data{twitter}{error} . "\n";
  117. $status .= "\t Error-Count-Max: " . $data{twitter}{error_max} . "\n";
  118. $status .= "\t Last Message: " . $data{twitter}{last_msg_time} . "\n";
  119. $status .= "\t Last Message: " . $data{twitter}{last_msg} . "\n";
  120. $status .= "--------------------------------------------------------------------------------\n";
  121. return $status;
  122. }
  123. #Enable
  124. if($msg eq "ENABLE"){
  125. if(defined($data{twitter}{conf}{disabeld})){
  126. $status = "Twitter enabled";
  127. return $status;
  128. }
  129. return "Twitter already Enabeld";
  130. }
  131. # Disable
  132. if($msg eq "DISABLE"){
  133. $data{twitter}{conf}{disabeld} = 1;
  134. return "Twitter disabeld";
  135. }
  136. #ERROR-Counter
  137. my ($err_cnt,$err_max);
  138. if(defined($data{twitter}{error})){
  139. $err_cnt = $data{twitter}{error};
  140. $err_max = $data{twitter}{error_max};
  141. if($err_cnt > $err_max){
  142. # ERROR disable twitter
  143. Log 0, "TWITTER[INFO] ErrCounter exceeded $err_max DISABLED";
  144. $data{twitter}{last_msg_time} = TimeNow();
  145. $data{twitter}{last_msg} = "TWITTER[INFO] ErrCounter exceeded $err_max DISABLED";
  146. $data{twitter}{conf}{disabeld} = 1;
  147. }
  148. }
  149. # Disbaled ??
  150. if(defined($data{twitter}{conf}{disabeld})){
  151. Log 0, "TWITTER[STATUS] DISABLED";
  152. return undef;
  153. }
  154. #Changed Day
  155. my $d = (localtime(time()))[3];
  156. my $d_old = $data{twitter}{conf}{day};
  157. if($d_old ne $d){
  158. $data{twitter}{conf}{day} = $d;
  159. Log 0,"TWITTER[INFO] DAY-CHANGE: DAY: $d_old MSG-COUNT: " . $data{twitter}{conf}{msg_count};
  160. $data{twitter}{conf}{msg_count} = 0;
  161. #Reset ERROR-Counter
  162. $data{twitter}{error} = 0;
  163. }
  164. #Count MSG
  165. my $msg_cnt = $data{twitter}{conf}{msg_count};
  166. if($msg_cnt > 1000) {
  167. Log 0, "TWITTER[INFO] MessageCount exceede 1000 Messages per Day";
  168. $data{twitter}{last_msg_time} = TimeNow() ;
  169. $data{twitter}{last_msg} = "TWITTER[INFO] Message-Count exceeded 1000 Messages per Day";
  170. return undef;
  171. }
  172. my $t_user = $data{twitter}{login}{user};
  173. my $t_pdw = $data{twitter}{login}{pwd};
  174. my $t_log;
  175. #Add MSG-Count
  176. $msg = "[" . $msg_cnt ."] " . $msg;
  177. # Max ;SG-Length 140
  178. my $ml = length ($msg);
  179. if($ml > 140){
  180. Log 0, "TWITTER[INFO] Die Nachricht ist mit $ml Zeichen zu lang (max. 140)";
  181. $msg = substr($msg, 0, 140);
  182. }
  183. my $browser = LWP::UserAgent->new;
  184. # Proxy-Server
  185. # $browser->proxy(['http'], "http://PROXYSERVER:PORT");
  186. $browser->timeout(5);
  187. my $url = 'http://twitter.com/statuses/update.json';
  188. $browser->credentials('twitter.com:80', 'Twitter API', $t_user , $t_pdw);
  189. my $response = $browser->get("http://twitter.com/account/verify_credentials.json");
  190. if($response->code eq 200){
  191. $t_log = "LOGIN=SUCCESS";
  192. }
  193. else {
  194. $t_log = "TWITTER[ERROR] LOGIN: " . $response->code .":".$response->message . " DISABLED";
  195. $data{twitter}{error}++;
  196. $data{twitter}{last_msg_time} = TimeNow() ;
  197. $data{twitter}{last_msg} = $t_log;
  198. return undef;
  199. }
  200. $response = $browser->post($url, {status => $msg});
  201. if($response->code eq 200){
  202. $t_log .= " UPDATE=SUCCESS";
  203. }
  204. else {
  205. $t_log = "TWITTER[ERROR] UPDATE: " . $response->code .":".$response->message . " DISABLED";
  206. $data{twitter}{error}++;
  207. $data{twitter}{last_msg_time} = TimeNow() ;
  208. $data{twitter}{last_msg} = $t_log;
  209. return undef;
  210. }
  211. Log 0, "TWITTER[INFO] " . $t_log . " MSG-$msg_cnt-: $msg";
  212. $data{twitter}{last_msg_time} = TimeNow() ;
  213. $data{twitter}{last_msg} = $msg;
  214. $data{twitter}{conf}{msg_count}++;
  215. # Reset ERROR-COUNTER
  216. $data{twitter}{error} = 0;
  217. $msg_cnt++;
  218. return undef;
  219. }
  220. 1;