| 1 | # Aug 27 2012 | ||
| 2 | # blogger interface | ||
| 3 | package WWW::BloggerWeb; | ||
| 4 | our $VERSION = '1.0'; | ||
| 5 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 0 | ▶ | use Net::SSL (); # From Crypt-SSLeay |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use LWP::UserAgent; | ||
| 9 | use HTTP::Request::Common; | ||
| 10 | |||
| 11 | $Net::HTTPS::SSL_SOCKET_CLASS = "Net::SSL"; # Force use of Net::SSL for proxy compatibility | ||
| 12 | |||
| 13 | { | ||
| 14 | has 'username', is => 'rw', isa => 'Str',default => '' ; | ||
| 15 | has 'password', is => 'rw', isa => 'Str',default => '' ; | ||
| 16 | has 'blog_id', is => 'rw', isa => 'Str',default => '' ; | ||
| 17 | has 'blog', is => 'rw', isa => 'Str',default => '' ; | ||
| 18 | |||
| 19 | has proxy_host => ( isa => 'Str', is => 'rw', default => '' ); | ||
| 20 | has proxy_port => ( isa => 'Str', is => 'rw', default => '' ); | ||
| 21 | has proxy_user => ( isa => 'Str', is => 'rw', default => '' ); | ||
| 22 | has proxy_pass => ( isa => 'Str', is => 'rw', default => '' ); | ||
| 23 | has proxy_env => ( isa => 'Str', is => 'rw', default => '' ); | ||
| 24 | |||
| 25 | has browser => ( isa => 'Object', is => 'rw', lazy => 1, builder => '_build_browser' ); | ||
| 26 | |||
| 27 | ###################################### blog functions ################### | ||
| 28 | |||
| 29 | ### login ### | ||
| 30 | sub login | ||
| 31 | { | ||
| 32 | my $self = shift; | ||
| 33 | my %options = @_; | ||
| 34 | my $username = $self->username; | ||
| 35 | my $password = $self->password; | ||
| 36 | |||
| 37 | my $post_data = {Email => $username, | ||
| 38 | Passwd => $password, | ||
| 39 | service => 'blogger'}; | ||
| 40 | |||
| 41 | |||
| 42 | my $response = $self->dispatch(url =>"https://www.google.com/accounts/ClientLogin", method => 'POST',post_data =>$post_data); | ||
| 43 | |||
| 44 | # Check success, parsing Google error message, if available. | ||
| 45 | unless ($response->is_success) | ||
| 46 | { | ||
| 47 | my $error_msg = ($response->content =~ /\bError=(.+)/)[0] || 'Google error message unavailable'; | ||
| 48 | die 'HTTP error when trying to authenticate: ' . $response->status_line . " ($error_msg)"; | ||
| 49 | } | ||
| 50 | |||
| 51 | # Parse authentication token and set it as default header for user agent object. | ||
| 52 | my ($auth_token) = $response->content =~ /\bAuth=(.+)/ | ||
| 53 | or die 'Authentication token not found in the response: ' . $response->content; | ||
| 54 | $self->browser->default_header(Authorization => "GoogleLogin auth=$auth_token"); | ||
| 55 | |||
| 56 | return 1; | ||
| 57 | } | ||
| 58 | |||
| 59 | # post to blog | ||
| 60 | sub post | ||
| 61 | { | ||
| 62 | my $self = shift; | ||
| 63 | my ($title,$content) = @_; | ||
| 64 | #my $post_data = $options{ post_data }; | ||
| 65 | my $blog_id = $self->blog_id; | ||
| 66 | |||
| 67 | my $xml_post = '<entry xmlns="http://www.w3.org/2005/Atom"><title type="text">TITLE</title><content type="html">CONTENT</content></entry>'; | ||
| 68 | $xml_post =~ s/TITLE/$title/; | ||
| 69 | $xml_post =~ s/CONTENT/$content/; | ||
| 70 | |||
| 71 | my $response = $self->dispatch(url =>"https://www.blogger.com/feeds/".$blog_id."/posts/default",method => 'POST_FILE',post_data =>$xml_post); | ||
| 72 | |||
| 73 | my $raw_response = $response->content; | ||
| 74 | |||
| 75 | open (SALIDA,">post.xml") || die "ERROR: No puedo abrir el fichero post.xml\n"; | ||
| 76 | print SALIDA $raw_response; | ||
| 77 | close (SALIDA); | ||
| 78 | |||
| 79 | my $url = `egrep -o 'http://[[:alnum:].\/-]{1,50}blogspot.com[[:alnum:].\/-]{1,100}.html' post.xml | head -1`; | ||
| 80 | $url =~ s/\r|\n//g; # Remove Carrier Return | ||
| 81 | |||
| 82 | return $url; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | ###################################### internal functions ################### | ||
| 87 | sub dispatch { | ||
| 88 | my $self = shift; | ||
| 89 | my %options = @_; | ||
| 90 | |||
| 91 | my $method = $options{ method }; | ||
| 92 | my $url = $options{ url }; | ||
| 93 | my $post_data = $options{ post_data }; | ||
| 94 | |||
| 95 | my $response = ''; | ||
| 96 | if ($method eq 'GET') | ||
| 97 | { $response = $self->browser->get($url);} | ||
| 98 | |||
| 99 | if ($method eq 'POST') | ||
| 100 | { | ||
| 101 | my $post_data = $options{ post_data }; | ||
| 102 | $response = $self->browser->post($url,$post_data); | ||
| 103 | } | ||
| 104 | |||
| 105 | if ($method eq 'POST_FILE') | ||
| 106 | { | ||
| 107 | $response = $self->browser->post( $url, Content_Type => 'application/atom+xml', Content => $post_data ); | ||
| 108 | } | ||
| 109 | |||
| 110 | return $response; | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | sub _build_browser { | ||
| 115 | my $self = shift; | ||
| 116 | |||
| 117 | my $proxy_host = $self->proxy_host; | ||
| 118 | my $proxy_port = $self->proxy_port; | ||
| 119 | my $proxy_user = $self->proxy_user; | ||
| 120 | my $proxy_pass = $self->proxy_pass; | ||
| 121 | my $proxy_env = $self->proxy_env; | ||
| 122 | |||
| 123 | my $browser = LWP::UserAgent->new; | ||
| 124 | $browser->show_progress(1); | ||
| 125 | print "proxy_env $proxy_env \n"; | ||
| 126 | |||
| 127 | if ( $proxy_env eq 'ENV' ) | ||
| 128 | { | ||
| 129 | $Net::HTTPS::SSL_SOCKET_CLASS = "Net::SSL"; # Force use of Net::SSL | ||
| 130 | $ENV{HTTPS_PROXY} = "http://".$proxy_host.":".$proxy_port; | ||
| 131 | } | ||
| 132 | else | ||
| 133 | { | ||
| 134 | if (($proxy_user ne "") && ($proxy_host ne "")) | ||
| 135 | { | ||
| 136 | $browser->proxy(['http', 'https'], 'http://'.$proxy_user.':'.$proxy_pass.'@'.$proxy_host.':'.$proxy_port); # Using a private proxy | ||
| 137 | } | ||
| 138 | elsif ($proxy_host ne "") | ||
| 139 | { $browser->proxy(['http', 'https'], 'http://'.$proxy_host.':'.$proxy_port);} # Using a public proxy | ||
| 140 | else | ||
| 141 | { $browser->env_proxy;} # No proxy | ||
| 142 | } | ||
| 143 | |||
| 144 | return $browser; | ||
| 145 | } | ||
| 146 | |||
| 147 | } | ||
| 148 | |||
| 149 | 1; | ||
| 150 | |||
| 151 | |||
| 152 | __END__ | ||