slartyblartfast (slartyblartfast) wrote in changelog,
slartyblartfast
slartyblartfast
changelog

[livejournal] r16154: LJSUP-5451: Publication Writer's block t...

Committer: mchernyshev
LJSUP-5451: Publication Writer's block to RU- and US-Journals
- autopost script refactored.
U   trunk/bin/misc/autopost_writersblock.pl
Modified: trunk/bin/misc/autopost_writersblock.pl
===================================================================
--- trunk/bin/misc/autopost_writersblock.pl	2010-01-27 11:34:48 UTC (rev 16153)
+++ trunk/bin/misc/autopost_writersblock.pl	2010-01-28 04:54:02 UTC (rev 16154)
@@ -12,56 +12,114 @@
 require 'ljprotocol.pl';
 require 'ljfeed.pl';
 
-my $comm = LJ::want_user(LJ::get_userid('writersblock'));
+my %comms = (
+    'writersblock'      => { country => 'US', },
+    'writersblock_ru'   => { country => 'RU', },
+);
+
 my $u = LJ::want_user(LJ::get_userid('lj_bot'));
+
 my $now = time();
-my $ago = $now - (3600 * 13); # X hours = 13
 
-die "Community doesn't exist" unless LJ::isu($comm);
-die "User doesn't exist" unless LJ::isu($u);
+# Get information from entries in communities
+# Check for all errors, then print all error messages.
+{
+    my @errors;
+    foreach my $comm (keys %comms) {
+        if (LJ::isu($comms{$comm}->{object} = LJ::want_user(LJ::get_userid($comm)))) {
 
-# check if community has been posted to in the last X hours.
-# If so, don't do anything
-if ($comm->timeupdate > $ago) {
-    print "Community updated recently, don't do anything\n";
-    exit;
+                # get last 50 entries (getevents request)
+                my $req = {
+                    'usejournal' => $comm,
+                    'ver' => $LJ::PROTOCOL_VER,
+                    'username' => $comm,
+                    'selecttype' => 'lastn',
+                    'howmany' => 50,
+                    'noprop' => 1,
+                };
+
+                my $err;
+                my $evts = LJ::Protocol::do_request("getevents", $req, \$err, { 'noauth' => 1 });
+                if ($err) {
+                    push @errors, "getevents from community '$comm' returns error $err";
+                    next;
+                }
+
+                # get qids from it
+                # 'event' => '<lj-template name="qotd" id="42" />',
+
+                if (@{$evts->{events}}) {
+                    $comms{$comm}->{qids} = {
+                        map { $_ => $_ }
+                        map { $_->{event} =~ m#<lj-template name="qotd" id="(\d+)" />#; $1 }
+                            @{$evts->{events}}
+                    };
+                }
+        } else {
+            push @errors, "community '$comm' doesn't exist";
+        }
+    }
+
+    push @errors, "user 'lj_bot' doesn't exist" unless LJ::isu($u);
+
+    die
+        "There was an error(s):\n" .
+        join(";\n", map {'  - ' . $_} @errors) . ".\n" .
+        "Execution cancelled\n"
+            if @errors;
 }
 
-# Find QotDs that started in the last X hours
-my $dbh = LJ::get_db_reader()
-    or die "Error: no database";
-my $sth = $dbh->prepare("SELECT * FROM qotd WHERE time_start > ? AND " .
-                        "time_start < ? AND active='Y' ORDER BY time_start");
-$sth->execute($ago, $now);
+# Find QotDs
 my @rows = ();
-while (my $row = $sth->fetchrow_hashref) {
-    push @rows, $row;
-}
+{
+    my $dbh = LJ::get_db_reader() or die "Error: no database";
 
-# No QotDs, exit
-unless (@rows) {
-    print "No new QotDs found, exiting...\n";
-    exit;
+    my $sth = $dbh->prepare("SELECT * FROM qotd WHERE active='Y' " .
+            "AND time_start < ? AND time_end > ? ORDER BY time_start");
+    $sth->execute($now, $now);
+    while (my $row = $sth->fetchrow_hashref) {
+        push @rows, $row;
+    }
 }
 
-foreach my $row (@rows) {
-    print "Posting [" . $row->{qid} . "] " . $row->{subject} . "\n";
-    my %req = (
-        mode => 'postevent',
-        ver => $LJ::PROTOCOL_VER,
-        user => $u->user,
-        usejournal => $comm->user,
-        tz => 'guess',
-        subject => $row->{subject},
-        event => '<lj-template name="qotd" id="' . $row->{qid} . '" />',
-        prop_taglist => $row->{tags},
-        prop_opt_noemail => 1,
-        prop_qotdid => $row->{qid},
-    );
+# Combine information, filter it and post to communities
+{
+    foreach my $row (@rows) {
+        foreach my $comm (keys %comms) {
 
-    my %res;
-    my $flags = { noauth => 1, u => $u };
-    LJ::do_request(\%req, \%res, $flags);
+            # filter by country
+            my $country = $comms{$comm}->{country}; # Community's country
+            if ($country) {
+                my $qotd_countries = $row->{countries}; # QotD's countries
+                $qotd_countries =~ s/^ *//; $qotd_countries =~ s/ *$//;
+                next if $qotd_countries && ($qotd_countries !~ m/$country/i);
+            }
+
+            # filter already posted
+            next if $comms{$comm}->{qids}->{$row->{qid}};
+
+            print "Posting [$row->{qid}] $row->{subject} to $comm\n";
+
+            my %req = (
+                mode => 'postevent',
+                ver => $LJ::PROTOCOL_VER,
+                user => $u->user,
+                usejournal => $comms{$comm}->{object}->user(),
+                tz => 'guess',
+                subject => $row->{subject},
+                event => '<lj-template name="qotd" id="' . $row->{qid} . '" />',
+                prop_taglist => $row->{tags},
+                prop_opt_noemail => 1,
+                prop_qotdid => $row->{qid},
+            );
+
+            my %res;
+            my $flags = { noauth => 1, u => $u };
+
+            LJ::do_request(\%req, \%res, $flags);
+        }
+    }
 }
 
 print "ALL DONE\n";
+

Tags: livejournal, pl, slartyblartfast
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments