Guest User

Untitled

a guest
Mar 10th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #!/usr/bin/env perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. use Config::Std { def_sep => '=' };
  7. use Getopt::Std;
  8. use Net::Twitter::Lite;
  9. use WebService::Prowl;
  10.  
  11. my %opt;
  12.  
  13. read_config 'twitter.conf' => my %config;
  14. getopts( 'a:dms', \%opt );
  15.  
  16. my $prowl = WebService::Prowl->new( apikey => $config{''}{'prowl_api_key'} );
  17. $prowl->verify()
  18. or die $prowl->error();
  19.  
  20. my @accounts = get_accounts();
  21. my $twitter;
  22.  
  23. foreach my $account ( sort @accounts ) {
  24. status( "-> $account" );
  25. $twitter = Net::Twitter::Lite->new(
  26. username => $account,
  27. password => $config{ $account }{'password'},
  28. );
  29.  
  30. if ( !defined $config{ $account }{'skip_dm'} ) {
  31. check_direct_messages( $account );
  32. }
  33. if ( !defined $config{ $account }{'skip_mention'} ) {
  34. check_mentions( $account );
  35. }
  36. }
  37.  
  38. write_config %config;
  39. exit;
  40.  
  41.  
  42.  
  43. sub get_accounts {
  44. my @accounts;
  45.  
  46. if ( $opt{'a'} ) {
  47. push @accounts, $opt{'a'};
  48. }
  49. else {
  50. foreach my $section ( keys %config ) {
  51. next if '' eq $section;
  52. push @accounts, $section;
  53. }
  54. }
  55. return @accounts;
  56. }
  57. sub check_direct_messages {
  58. my $account = shift;
  59.  
  60. my $last_dm = $config{ $account }{'last_dm'} || 1;
  61. my $dms = $twitter->direct_messages( { since_id => $last_dm } );
  62.  
  63. foreach my $dm ( @{ $dms } ) {
  64. my $from = $dm->{'sender_screen_name'};
  65. my $body = $dm->{'text'};
  66. my $id = $dm->{'id'};
  67.  
  68. if ( $id > $last_dm ) {
  69. $last_dm = $id;
  70. }
  71.  
  72. my $message = sprintf "%s: %s\n", $from, $body;
  73. status( $message );
  74.  
  75. $prowl->add(
  76. application => 'Twitter DM',
  77. event => $account,
  78. description => $message
  79. );
  80. }
  81.  
  82. $config{ $account }{'last_dm'} = $last_dm;
  83. }
  84. sub check_mentions {
  85. my $account = shift;
  86.  
  87. my $last_mention = $config{ $account }{'last_mention'} || 1;
  88. my $mentions = $twitter->mentions( { since_id => $last_mention } );
  89.  
  90. foreach my $mention ( @{ $mentions } ) {
  91. my $from = $mention->{'user'}{'screen_name'};
  92. my $body = $mention->{'text'};
  93. my $id = $mention->{'id'};
  94.  
  95. if ( $id > $last_mention ) {
  96. $last_mention = $id;
  97. }
  98.  
  99. my $message = sprintf "%s: %s\n", $from, $body;
  100. status( $message );
  101.  
  102. $prowl->add(
  103. application => 'Twitter Mention',
  104. event => $account,
  105. description => $message
  106. );
  107. }
  108.  
  109. $config{ $account }{'last_mention'} = $last_mention;
  110. }
  111. sub status {
  112. my $text = shift;
  113.  
  114. print "${text}\n" if $opt{'s'};
  115. }
Add Comment
Please, Sign In to add comment