Difference between revisions of "Twitter"

From EPrints Documentation
Jump to: navigation, search
(Updated instructions to use HTTPS and version 1.1 of the API, as required by Twitter.)
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Adding a twitter channel to eprints is dead easy. ==
+
[[Category:EPrints 3 Plugins]]
  
==== First set up a twitter account for your repository. ====
+
This recipe shows how to add tweets to your repository that occur whenever a new item is deposited.
  
Add the following to a new .pl file in cfg.d/
+
== Installation ==
  
You could name this file ''twitter.pl'', for example
+
Install [http://search.cpan.org/search?query=LWP%3A%3AAuthen%3A%3AOAuth LWP::Authen::OAuth] and [http://search.cpan.org/search?query=LWP%3A%3AProtocol%3A%3Ahttps LWP::Protocol::https]
  
Add the username and password for the account.
+
Set up a new [http://twitter.com Twitter] account for your repository if not already done so.
 +
 
 +
Sign into your Twitter account on [https://dev.twitter.com/] and then click "Register an app". Fill in the form:
 +
 
 +
* Name: enter your repository URL
 +
* Description: enter anything
 +
* Home page: enter your repository URL
 +
* Organization: enter anything
 +
* Application Type: Client
 +
* Default Access Type: Read & Write
 +
 
 +
In your repository add to a new configuration file in cfg.d/ (e.g. ''twitter.pl''), substituting the values given below:
 +
 
 +
* Change ''oauth_consumer_key'' and ''oauth_consumer_secret'' to the values found on the Twitter application page.
 +
* Change ''oauth_token'' and ''oauth_token_secret'' to the values found on the "My Access Token" page linked from the Twitter application page.
  
 
<pre><nowiki>
 
<pre><nowiki>
$c->{eprint_status_change} = sub
+
use LWP::Authen::OAuth;
{
+
 
 +
$c->{twitter}->{authen} = {
 +
        oauth_consumer_key => 'c2nOQSd2asDF9nhqFKCWg',
 +
        oauth_consumer_secret => '9hz7hoNIpk29slqtJ46yOg7HXAqOPrETnueNGrTXu4',
 +
        oauth_token => '15203291-1qVbfWHh9Sa8sjdkmqyP8eB9XyxhouZhPnRrKnwQj',
 +
        oauth_token_secret => 'SZmj2zKsVGrEpPDDaZBrE92ksLwf0oBJ8X4medYoX8',
 +
};
 +
 
 +
$c->{eprint_status_change} = sub
 +
{
 
         my( $eprint, $old_status, $new_status ) = @_;
 
         my( $eprint, $old_status, $new_status ) = @_;
  
 
         if( $new_status eq "archive" )
 
         if( $new_status eq "archive" )
 
         {
 
         {
                 my $twitter_username="username";
+
                 my $ua = LWP::UserAgent->new;
                 my $twitter_password="password";
+
                 my $url = URI->new( 'http://is.gd/api.php' );
                my $cmd1 = "curl -s 'http://is.gd/api.php?longurl=".$eprint->get_url."'";
+
                $url->query_form(
                 my $shorturl = `$cmd1`;
+
                        longurl => $eprint->get_url
 +
                );
 +
                 my $shorturl = $ua->get( $url )->content;
 
                 my $title = $eprint->get_value( "title" );
 
                 my $title = $eprint->get_value( "title" );
                 $title =~ s/'/\\'/g;
+
                 {
                 my $cmd2 = "curl --basic -s --user $twitter_username:$twitter_password --data status='$title - $shorturl' http://twitter.com/statuses/update.xml";
+
                        use bytes;
                 `$cmd2 &`;
+
                        $title =~ s/^(.{0,100})/$1/;
 +
                }
 +
                 my $tweet = "$title - $shorturl";
 +
 
 +
                my $oua = LWP::Authen::OAuth->new(
 +
                        %{$eprint->{session}->config( "twitter", "authen" )},
 +
                );
 +
                my $r = $oua->post( 'https://api.twitter.com/1.1/statuses/update.json', [
 +
                        status => $tweet,
 +
                ] );
 +
                 if( $r->is_error )
 +
                {
 +
                        $eprint->{session}->log( "Tweet failed: ".$r->as_string );
 +
                }
 
         }
 
         }
};
+
 
 +
};
 
</nowiki></pre>
 
</nowiki></pre>
  
==== Couple of things to look out for: ====
 
* Make sure you have CURL installed, as the script invokes this from the command line
 
 
* Restart Apache ( /etc/init.d/apache2 force-reload , or similar)
 
* Restart Apache ( /etc/init.d/apache2 force-reload , or similar)
* Then log in and deposit an item to test
+
* Log in and deposit an item to test
  
 +
=== Examples ===
  
==== Add your twitter to the list: ====
 
 
* http://twitter.com/eprintsecs - ECS EPrints, Southampton
 
* http://twitter.com/eprintsecs - ECS EPrints, Southampton

Latest revision as of 11:25, 1 June 2016


This recipe shows how to add tweets to your repository that occur whenever a new item is deposited.

Installation

Install LWP::Authen::OAuth and LWP::Protocol::https

Set up a new Twitter account for your repository if not already done so.

Sign into your Twitter account on [1] and then click "Register an app". Fill in the form:

  • Name: enter your repository URL
  • Description: enter anything
  • Home page: enter your repository URL
  • Organization: enter anything
  • Application Type: Client
  • Default Access Type: Read & Write

In your repository add to a new configuration file in cfg.d/ (e.g. twitter.pl), substituting the values given below:

  • Change oauth_consumer_key and oauth_consumer_secret to the values found on the Twitter application page.
  • Change oauth_token and oauth_token_secret to the values found on the "My Access Token" page linked from the Twitter application page.
use LWP::Authen::OAuth;

$c->{twitter}->{authen} = {
        oauth_consumer_key => 'c2nOQSd2asDF9nhqFKCWg',
        oauth_consumer_secret => '9hz7hoNIpk29slqtJ46yOg7HXAqOPrETnueNGrTXu4',
        oauth_token => '15203291-1qVbfWHh9Sa8sjdkmqyP8eB9XyxhouZhPnRrKnwQj',
        oauth_token_secret => 'SZmj2zKsVGrEpPDDaZBrE92ksLwf0oBJ8X4medYoX8',
};

$c->{eprint_status_change} = sub
{
        my( $eprint, $old_status, $new_status ) = @_;

        if( $new_status eq "archive" )
        {
                my $ua = LWP::UserAgent->new;
                my $url = URI->new( 'http://is.gd/api.php' );
                $url->query_form(
                        longurl => $eprint->get_url
                );
                my $shorturl = $ua->get( $url )->content;
                my $title = $eprint->get_value( "title" );
                {
                        use bytes;
                        $title =~ s/^(.{0,100})/$1/;
                }
                my $tweet = "$title - $shorturl";

                my $oua = LWP::Authen::OAuth->new(
                        %{$eprint->{session}->config( "twitter", "authen" )},
                );
                my $r = $oua->post( 'https://api.twitter.com/1.1/statuses/update.json', [
                        status => $tweet,
                ] );
                if( $r->is_error )
                {
                        $eprint->{session}->log( "Tweet failed: ".$r->as_string );
                }
        }

};
  • Restart Apache ( /etc/init.d/apache2 force-reload , or similar)
  • Log in and deposit an item to test

Examples