Committer: ailyin
LJSUP-7346 (Put the tweet to lj_times twitter account)U trunk/bin/upgrading/en_LJ.dat A trunk/bin/worker/twitter-times
Modified: trunk/bin/upgrading/en_LJ.dat =================================================================== --- trunk/bin/upgrading/en_LJ.dat 2010-11-18 09:02:04 UTC (rev 9733) +++ trunk/bin/upgrading/en_LJ.dat 2010-11-18 10:06:15 UTC (rev 9734) @@ -6207,6 +6207,8 @@ twitter_digest.weekday.sun=Sun, +twitter_times.status=#ljtimes #news In the meantime in LjTimes www.livejournal.com/ljtimes/ + twitterconnect.untitled.comment=New comment at LiveJournal: twitterconnect.untitled.entry=New entry at LiveJournal: Added: trunk/bin/worker/twitter-times =================================================================== --- trunk/bin/worker/twitter-times (rev 0) +++ trunk/bin/worker/twitter-times 2010-11-18 10:06:15 UTC (rev 9734) @@ -0,0 +1,119 @@ +#!/usr/bin/perl +use strict; +use warnings; + +use lib "$ENV{'LJHOME'}/cgi-bin"; +require 'ljlib.pl'; + +# twitter-times worker: periodically update the specified Twitter +# account with some status +# +# configuration: +# +# $LJ::TIMES_TWITTER_ACCOUNT (lj-times-conf.pl): the username of the +# LiveJournal account that stores the access token necessary to update +# twitter. +# +# 'twitter_times.status' ML variable, Russian: the text that gets +# posted. Please note that due to caching, worker needs to be +# restarted to pick up any updates of that variable. + +LJ::NewWorker::TwitterTimes->start; + +package LJ::NewWorker::TwitterTimes; +use base qw(LJ::NewWorker::Manual); + +use LJ::Client::Twitter; +use Data::Dumper qw(); + +# given an HH:MM time, return UNIX timestamp in the current day +# corresponding to that time; timezone is Moscow time +sub _hour_to_timestamp { + my ($class, $time) = @_; + + my ($hour, $minute) = split /:/, $time; + + my $dt = DateTime->from_epoch( 'epoch' => time, + 'time_zone' => 'Europe/Moscow', ); + + $dt->set( 'hour' => $hour, + 'minute' => $minute, + 'second' => 0 ); + + return $dt->epoch; +} + +my @POST_TIMES; +BEGIN { + @POST_TIMES = qw( 13:00 19:00 ); # 1PM and 7PM MSK +} + +# return the list of all timestamps in the current day we're supposed +# to update Twitter on; timezone is Moscow time +sub _today_post_timestamps { + my ($class) = @_; + return map { $class->_hour_to_timestamp($_) } @POST_TIMES; +} + +sub work { + my ($class) = @_; + + # check that we're posting at the specified time + # we actually give it a five minute leeway because workers + # can lag + my @timestamps = $class->_today_post_timestamps; + my $time_correct = 0; + foreach my $ts (@timestamps) { + next unless (time - $ts > 0 and time - $ts < 300); + + $time_correct = 1; + last; + } + + # if we're running at the wrong time, let's wait + unless ($time_correct) { + warn "we're not posting until the time comes\n" + if LJ::NewWorker->verbose; + + return 0; + } + + # now, actually post + my $status = LJ::Lang::get_text('ru', 'twitter_times.status'); + $status .= ' ' . rand() if $LJ::IS_DEV_SERVER; + + my $res = LJ::Client::Twitter->call( + 'api_method' => 'statuses/update', + 'user' => LJ::load_user($LJ::TIMES_TWITTER_ACCOUNT), + 'http_method' => 'POST', + 'params' => { 'status' => $status }, + ); + + if (LJ::NewWorker->verbose) { + my $addr = 'http://twitter.com/' . $res->{'user'}->{'screen_name'} + . '/status/' . $res->{'id'}; + warn "posted: $addr\n"; + } + + # and sleep until it's time + return 0; +} + +sub on_idle { + my ($class) = @_; + + my @timestamps = sort { $a <=> $b } $class->_today_post_timestamps; + + # try to find some time to post today + my ($next_post_time) = grep { $_ > time } @timestamps; + + # failing that, let's post at our earliest convenience tomorrow + $next_post_time = $timestamps[0] + 86400 unless $next_post_time; + + warn "sleeping until " . scalar(localtime($next_post_time)) . "\n" + if LJ::NewWorker->verbose; + + sleep $next_post_time - time; +} + +1; Property changes on: trunk/bin/worker/twitter-times ___________________________________________________________________ Added: svn:executable + *