Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timestamp appended to message #17

Merged
merged 2 commits into from
Jul 13, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions html/using.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ <h3>Settings</h3>
<li><code>twirssi_avoid_ssl</code> - Default off. If off, twirssi will
attempt to use SSL for all network communications. Changes take effect
next time twirssi is loaded.</li>
<li><code>twirssi_timestamp_append</code> - If on, twirssi will append the actual
timestamp of the tweet/search/dm to the end of the message (default is off).</li>
<li><code>twirssi_timestamp_color</code> - If set, this string defines the color
of the timestamp (see twirssi_timestamp_append) (default is "%y").</li>
<li><code>twirssi_timestamp_format</code> - This string defines the format
(see strftime) of timestamps (see twirssi_timestamp_append)
(default is "%Y-%m-%d %H:%M:%S").</li>
<li><code>twirssi_timestamp_truncate</code> - If on, then timestamps (see
twirssi_timestamp_append) are shortened by removing any prefix that is
common with (i.e. also prefixes) the current time (default is on).
See twirssi_timestamp_truncate_ws_only for how truncation occurs.</li>
<li><code>twirssi_timestamp_truncate_ws_only</code> - If on (the default),
then timestamps truncation occurs at whitespace only. When off, any
truncated occurs at a non-alphanumeric character. Note:
twirssi_timestamp_truncate must also be on.</li>

<li><code>twitter_passwords</code> - A comma separated list of passwords to auto-login</li>
<li><code>twitter_poll_interval</code> - How often should new messages be
Expand Down
113 changes: 95 additions & 18 deletions twirssi.pl
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
use Data::Dumper;
use Encode;
use FileHandle;
use POSIX qw/:sys_wait_h/;
use POSIX qw/:sys_wait_h strftime floor/;
use Net::Twitter qw/3.11009/;
# may be required: use DateTime::Format::Strptime;
$Data::Dumper::Indent = 1;

use vars qw($VERSION %IRSSI);
Expand Down Expand Up @@ -46,6 +47,7 @@
my $update_is_running = 0;
my $logfile_fh;
my %settings;
my @datetime_parser;

my %irssi_to_mirc_colors = (
'%k' => '01',
Expand Down Expand Up @@ -1314,17 +1316,20 @@ sub do_updates {

if ($context) {
my $ctext = &get_text( $context, $obj );
printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
printf $fh "id:%s account:%s nick:%s type:tweet created_at:%s %s\n",
$context->{id}, $username,
$context->{user}{screen_name}, $ctext;
$context->{user}{screen_name},
&encode_for_file($context->{created_at}),
$ctext;
$reply = "reply";
}
}
next
if $t->{user}{screen_name} eq $username
and not $settings{own_tweets};
printf $fh "id:%s account:%s nick:%s type:%s %s\n",
$t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
printf $fh "id:%s account:%s nick:%s type:%s created_at:%s %s\n",
$t->{id}, $username, $t->{user}{screen_name}, $reply,
&encode_for_file($t->{created_at}), $text;
$new_poll_id = $t->{id} if $new_poll_id < $t->{id};
}
printf $fh "id:%s account:%s type:last_id timeline\n",
Expand Down Expand Up @@ -1355,8 +1360,9 @@ sub do_updates {
if exists $friends{ $t->{user}{screen_name} };

my $text = &get_text( $t, $obj );
printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
$t->{id}, $username, $t->{user}{screen_name}, $text;
printf $fh "id:%s account:%s nick:%s type:tweet created_at:%s %s\n",
$t->{id}, $username, $t->{user}{screen_name},
&encode_for_file($t->{created_at}), $text;
$new_poll_id = $t->{id} if $new_poll_id < $t->{id};
}
printf $fh "id:%s account:%s type:last_id reply\n", $new_poll_id, $username;
Expand All @@ -1382,8 +1388,9 @@ sub do_updates {
foreach my $t ( reverse @$tweets ) {
my $text = decode_entities( $t->{text} );
$text =~ s/[\n\r]/ /g;
printf $fh "id:%s account:%s nick:%s type:dm %s\n",
$t->{id}, $username, $t->{sender_screen_name}, $text;
printf $fh "id:%s account:%s nick:%s type:dm created_at:%s %s\n",
$t->{id}, $username, $t->{sender_screen_name},
&encode_for_file($t->{created_at}), $text;
$new_poll_id = $t->{id} if $new_poll_id < $t->{id};
}
printf $fh "id:%s account:%s type:last_id dm\n", $new_poll_id, $username;
Expand Down Expand Up @@ -1422,8 +1429,9 @@ sub do_updates {

foreach my $t ( reverse @{ $search->{results} } ) {
my $text = &get_text( $t, $obj );
printf $fh "id:%s account:%s nick:%s type:search topic:%s %s\n",
$t->{id}, $username, $t->{from_user}, $topic, $text;
printf $fh "id:%s account:%s nick:%s type:search topic:%s created_at:%s %s\n",
$t->{id}, $username, $t->{from_user}, $topic,
&encode_for_file($t->{created_at}), $text;
$new_poll_id = $t->{id}
if not $new_poll_id
or $t->{id} < $new_poll_id;
Expand Down Expand Up @@ -1464,8 +1472,9 @@ sub do_updates {

my $text = &get_text( $t, $obj );
printf $fh
"id:%s account:%s nick:%s type:search_once topic:%s %s\n",
$t->{id}, $username, $t->{from_user}, $topic, $text;
"id:%s account:%s nick:%s type:search_once topic:%s created_at:%s %s\n",
$t->{id}, $username, $t->{from_user}, $topic,
&encode_for_file($t->{created_at}), $text;
}
}
}
Expand Down Expand Up @@ -1525,14 +1534,17 @@ sub get_timeline {

if ($context) {
my $ctext = &get_text( $context, $obj );
printf $fh "id:%s account:%s nick:%s type:tweet %s\n",
printf $fh "id:%s account:%s nick:%s type:tweet created_at:%s %s\n",
$context->{id}, $username,
$context->{user}{screen_name}, $ctext;
$context->{user}{screen_name},
&encode_for_file($context->{created_at}),
$ctext;
$reply = "reply";
}
}
printf $fh "id:%s account:%s nick:%s type:%s %s\n",
$t->{id}, $username, $t->{user}{screen_name}, $reply, $text;
printf $fh "id:%s account:%s nick:%s type:%s created_at:%s %s\n",
$t->{id}, $username, $t->{user}{screen_name}, $reply,
&encode_for_file($t->{created_at}), $text;
$last_id = $t->{id} if $last_id < $t->{id};
}
printf $fh "id:%s account:%s type:last_id_fixreplies %s\n",
Expand All @@ -1541,6 +1553,36 @@ sub get_timeline {
return 1;
}

sub encode_for_file {
my $datum = shift;
$datum =~ s/ /%20/g;
return $datum;
}

sub date_to_epoch {
# parse created_at style date to epoch time
my $date = shift;
if (not @datetime_parser) {
require DateTime::Format::Strptime;
foreach my $date_fmt (
'%a %b %d %T %z %Y', # Fri Nov 05 10:14:05 +0000 2010
'%a, %d %b %Y %T %z', # Fri, 05 Nov 2010 16:59:40 +0000
) {
my $parser = DateTime::Format::Strptime->new(pattern => $date_fmt);
if (not defined $parser) {
@datetime_parser = ();
return;
}
push @datetime_parser, $parser;
}
}
# my $orig_date = $date;
$date = $datetime_parser[index($date, ',') == -1 ? 0 : 1]->parse_datetime($date);
# print "date '$orig_date': " . ref($date) if &debug;
return if not defined $date;
return $date->epoch();
}

sub monitor_child {
my ($data) = @_;
my $filename = $data->[0];
Expand Down Expand Up @@ -1570,7 +1612,7 @@ sub monitor_child {
my $hilight = 0;
my %meta;

foreach my $key (qw/id account nick type topic/) {
foreach my $key (qw/id account nick type topic created_at/) {
if (s/^$key:((?:\S|\\ )+)\s*//) {
$meta{$key} = $1;
$meta{$key} =~ s/%20/ /g;
Expand Down Expand Up @@ -1624,6 +1666,31 @@ sub monitor_child {
$hilight = MSGLEVEL_HILIGHT;
}

if ( $meta{type} =~ /tweet|reply|search|dm/
and $settings{timestamp_append}
and defined $meta{created_at} ) {
my $now = strftime($settings{timestamp_format}, localtime());
my $timestamp = strftime($settings{timestamp_format}, localtime(&date_to_epoch($meta{created_at})));
while ($settings{timestamp_truncate} and $timestamp ne '') {
my $sep_pos = 0;
while ($sep_pos < length($now)
and ((not $settings{timestamp_truncate_ws}
and substr($now, $sep_pos, 1) =~ /[[:alnum:]]/)
or ($settings{timestamp_truncate_ws}
and substr($now, $sep_pos, 1) =~ /\S/))) {
$sep_pos++;
}
last if substr($now, 0, $sep_pos+1) ne substr($timestamp, 0, $sep_pos+1);
$now = substr($now, $sep_pos+1);
$timestamp = substr($timestamp, $sep_pos+1);
}
if ( $settings{timestamp_color} ne '' ) {
my $ts_color = $irssi_to_mirc_colors{ $settings{timestamp_color} };
$timestamp = "\cC$ts_color$timestamp\cO";
}
$_ .= ' ' . $timestamp if $timestamp ne '';
}

if ( $meta{type} =~ /tweet|reply/ ) {
push @lines,
[
Expand Down Expand Up @@ -1993,6 +2060,8 @@ sub event_setup_changed {
retweet_format
stripped_tags
topic_color
timestamp_color
timestamp_format
/
)
{
Expand All @@ -2010,6 +2079,9 @@ sub event_setup_changed {
[ 'use_oauth', 'twirssi_use_oauth' ],
[ 'use_reply_aliases', 'twirssi_use_reply_aliases' ],
[ 'window_input', 'tweet_window_input' ],
[ 'timestamp_append', 'twirssi_timestamp_append' ],
[ 'timestamp_truncate', 'twirssi_timestamp_truncate' ],
[ 'timestamp_truncate_ws', 'twirssi_timestamp_truncate_ws_only' ],
)
{
$settings{ $_->[0] } = Irssi::settings_get_bool( $_->[1] );
Expand Down Expand Up @@ -2235,6 +2307,8 @@ sub window_to_account {
Irssi::settings_add_str( "twirssi", "twirssi_default_service", "Twitter" );
Irssi::settings_add_str( "twirssi", "twirssi_nick_color", "%B" );
Irssi::settings_add_str( "twirssi", "twirssi_topic_color", "%r" );
Irssi::settings_add_str( "twirssi", "twirssi_timestamp_color", "%y" );
Irssi::settings_add_str( "twirssi", "twirssi_timestamp_format", "%Y-%m-%d %H:%M:%S" );
Irssi::settings_add_str( "twirssi", "twirssi_ignored_tags", "" );
Irssi::settings_add_str( "twirssi", "twirssi_stripped_tags", "" );
Irssi::settings_add_str( "twirssi", "twirssi_ignored_accounts", "" );
Expand Down Expand Up @@ -2262,6 +2336,9 @@ sub window_to_account {
Irssi::settings_add_bool( "twirssi", "tweet_window_input", 0 );
Irssi::settings_add_bool( "twirssi", "twirssi_avoid_ssl", 0 );
Irssi::settings_add_bool( "twirssi", "twirssi_use_oauth", 1 );
Irssi::settings_add_bool( "twirssi", "twirssi_timestamp_append", 0 );
Irssi::settings_add_bool( "twirssi", "twirssi_timestamp_truncate", 1 );
Irssi::settings_add_bool( "twirssi", "twirssi_timestamp_truncate_ws_only", 1 );

$last_poll = time - &get_poll_time;

Expand Down