From 3305e9bfbc94a52bfc1242305e3f41b77a90f446 Mon Sep 17 00:00:00 2001 From: Geraint Edwards Date: Sun, 7 Nov 2010 09:36:15 +0000 Subject: [PATCH 1/2] append original timestamps to some messages --- html/using.html | 11 +++++++++++ twirssi.pl | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/html/using.html b/html/using.html index 485d50e..fd9b0c4 100644 --- a/html/using.html +++ b/html/using.html @@ -129,6 +129,17 @@

Settings

  • twirssi_avoid_ssl - Default off. If off, twirssi will attempt to use SSL for all network communications. Changes take effect next time twirssi is loaded.
  • +
  • twirssi_timestamp_append - If on, twirssi will append the actual + timestamp of the tweet/search/dm to the end of the message (default is off).
  • +
  • twirssi_timestamp_color - If set, this string defines the color + of the timestamp (see twirssi_timestamp_append) (default is "%y").
  • +
  • twirssi_timestamp_format - This string defines the format + (see strftime) of timestamps (see twirssi_timestamp_append) + (default is "%Y-%m-%d %H:%M:%S").
  • +
  • twirssi_timestamp_truncate - If on, then timestamps (see + twirssi_timestamp_append) are shortened by removing any prefix (terminated + by a non-alphanumeric) that is common with (i.e. also prefixes) + the current time (default is on).
  • twitter_passwords - A comma separated list of passwords to auto-login
  • twitter_poll_interval - How often should new messages be diff --git a/twirssi.pl b/twirssi.pl index eef1cbb..ff0a197 100644 --- a/twirssi.pl +++ b/twirssi.pl @@ -8,7 +8,7 @@ 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/; $Data::Dumper::Indent = 1; @@ -1541,6 +1541,11 @@ sub get_timeline { return 1; } +sub id_to_time { + my $id = shift; + return floor((($id >> 22) + 1288834974657)/1000); +} + sub monitor_child { my ($data) = @_; my $filename = $data->[0]; @@ -1624,6 +1629,26 @@ sub monitor_child { $hilight = MSGLEVEL_HILIGHT; } + if ( $meta{type} =~ /tweet|reply|search|dm/ + and $settings{timestamp_append} ) { + my $now = strftime($settings{timestamp_format}, localtime()); + my $timestamp = strftime($settings{timestamp_format}, localtime(&id_to_time($meta{id}))); + while ($settings{timestamp_truncate} and $timestamp ne '') { + my $non_digit_pos = 0; + while ($non_digit_pos < length($now) and substr($now, $non_digit_pos, 1) =~ /[[:alnum:]]/) { + $non_digit_pos++; + } + last if substr($now, 0, $non_digit_pos+1) ne substr($timestamp, 0, $non_digit_pos+1); + $now = substr($now, $non_digit_pos+1); + $timestamp = substr($timestamp, $non_digit_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, [ @@ -1993,6 +2018,8 @@ sub event_setup_changed { retweet_format stripped_tags topic_color + timestamp_color + timestamp_format / ) { @@ -2010,6 +2037,8 @@ 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' ], ) { $settings{ $_->[0] } = Irssi::settings_get_bool( $_->[1] ); @@ -2235,6 +2264,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", "" ); @@ -2262,6 +2293,8 @@ 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 ); $last_poll = time - &get_poll_time; From 763009999fdce9ff131efc3514028058ecba6f4d Mon Sep 17 00:00:00 2001 From: Geraint Edwards Date: Sun, 7 Nov 2010 23:31:31 +0000 Subject: [PATCH 2/2] timestamp from 'created_by' - add twirssi_timestamp_truncate_ws_only --- html/using.html | 10 +++-- twirssi.pl | 100 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 79 insertions(+), 31 deletions(-) diff --git a/html/using.html b/html/using.html index fd9b0c4..afeef30 100644 --- a/html/using.html +++ b/html/using.html @@ -137,9 +137,13 @@

    Settings

    (see strftime) of timestamps (see twirssi_timestamp_append) (default is "%Y-%m-%d %H:%M:%S").
  • twirssi_timestamp_truncate - If on, then timestamps (see - twirssi_timestamp_append) are shortened by removing any prefix (terminated - by a non-alphanumeric) that is common with (i.e. also prefixes) - the current time (default is on).
  • + 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. +
  • twirssi_timestamp_truncate_ws_only - 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.
  • twitter_passwords - A comma separated list of passwords to auto-login
  • twitter_poll_interval - How often should new messages be diff --git a/twirssi.pl b/twirssi.pl index ff0a197..adba017 100644 --- a/twirssi.pl +++ b/twirssi.pl @@ -10,6 +10,7 @@ use FileHandle; 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); @@ -46,6 +47,7 @@ my $update_is_running = 0; my $logfile_fh; my %settings; +my @datetime_parser; my %irssi_to_mirc_colors = ( '%k' => '01', @@ -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", @@ -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; @@ -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; @@ -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; @@ -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; } } } @@ -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", @@ -1541,9 +1553,34 @@ sub get_timeline { return 1; } -sub id_to_time { - my $id = shift; - return floor((($id >> 22) + 1288834974657)/1000); +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 { @@ -1575,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; @@ -1630,17 +1667,22 @@ sub monitor_child { } if ( $meta{type} =~ /tweet|reply|search|dm/ - and $settings{timestamp_append} ) { + and $settings{timestamp_append} + and defined $meta{created_at} ) { my $now = strftime($settings{timestamp_format}, localtime()); - my $timestamp = strftime($settings{timestamp_format}, localtime(&id_to_time($meta{id}))); + my $timestamp = strftime($settings{timestamp_format}, localtime(&date_to_epoch($meta{created_at}))); while ($settings{timestamp_truncate} and $timestamp ne '') { - my $non_digit_pos = 0; - while ($non_digit_pos < length($now) and substr($now, $non_digit_pos, 1) =~ /[[:alnum:]]/) { - $non_digit_pos++; + 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, $non_digit_pos+1) ne substr($timestamp, 0, $non_digit_pos+1); - $now = substr($now, $non_digit_pos+1); - $timestamp = substr($timestamp, $non_digit_pos+1); + 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} }; @@ -2039,6 +2081,7 @@ sub event_setup_changed { [ '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] ); @@ -2295,6 +2338,7 @@ sub window_to_account { 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;