Skip to content

Instantly share code, notes, and snippets.

@jacoby
Created April 18, 2019 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jacoby/5151e6d499ff5bc271608eec7fca9c91 to your computer and use it in GitHub Desktop.
Save jacoby/5151e6d499ff5bc271608eec7fca9c91 to your computer and use it in GitHub Desktop.
Perl program (using EVERYTHING) to update your Twitter profile image (avatar)
#!/usr/bin/env perl
use feature qw{ say } ;
use strict ;
use utf8 ;
use Carp ;
use Data::Dumper ;
use Encode qw{decode decode_utf8} ;
use Getopt::Long ;
use IO::Interactive qw{ interactive } ;
use Net::Twitter ;
use WWW::Shorten 'TinyURL' ;
use YAML::XS qw{ DumpFile LoadFile } ;
use open ':std', ':encoding(UTF-8)' ;
binmode STDOUT, ':utf8' ;
@ARGV = map { decode_utf8( $_, 1 ) } @ARGV ;
my $config = config() ;
update_profile_image( $config ) ;
exit ;
# ========= ========= ========= ========= ========= ========= =========
sub update_profile_image {
my $config = shift ;
my $suffix = ( split m{\.}, $config->{ file } )[ -1 ] ;
if ( $suffix =~ m{(?:gif|jpe?g|png)}i ) {
my $twit = Net::Twitter->new(
traits => [ qw/API::RESTv1_1/ ],
consumer_key => $config->{ consumer_key },
consumer_secret => $config->{ consumer_secret },
ssl => 1,
) ;
if ( $config->{ access_token } && $config->{ access_token_secret } ) {
$twit->access_token( $config->{ access_token } ) ;
$twit->access_token_secret( $config->{ access_token_secret } ) ;
}
unless ( $twit->authorized ) {
croak( "Not Authorized" ) ;
}
#unless ( $twit->authorized ) {
#
# # You have no auth token
# # go to the auth website.
# # they'll ask you if you wanna do this, then give you a PIN
# # input it here and it'll register you.
# # then save your token vals.
#
# say "Authorize this app at ", $twit->get_authorization_url,
# ' and enter the PIN#' ;
# my $pin = <STDIN> ; # wait for input
# chomp $pin ;
# my ( $access_token, $access_token_secret, $user_id, $screen_name ) =
# $twit->request_access_token( verifier => $pin ) ;
# save_tokens( $user, $access_token, $access_token_secret ) ;
# }
my $image ;
push @$image, $config->{ file } ;
push @$image, 'icon.' . $suffix ;
if ( $twit->update_profile_image( $image ) ) {
say 'OK' ;
}
}
}
# ========= ========= ========= ========= ========= ========= =========
sub config {
my $config_file = $ENV{ HOME } . '/.twitter_dm.cnf' ;
my $data = LoadFile( $config_file ) ;
my $config ;
GetOptions(
'file=s' => \$config->{ file },
'user=s' => \$config->{ user },
'help' => \$config->{ help },
) ;
if ( $config->{ help }
|| !$config->{ file }
|| !-f $config->{ file }
|| !$config->{ user }
|| !$data->{ tokens }->{ $config->{ user } } ) {
croak qq(nothing) ;
}
for my $k ( qw{ consumer_key consumer_secret } ) {
$config->{ $k } = $data->{ $k } ;
}
my $tokens = $data->{ tokens }->{ $config->{ user } } ;
for my $k ( qw{ access_token access_token_secret } ) {
$config->{ $k } = $tokens->{ $k } ;
}
return $config ;
}
#========= ========= ========= ========= ========= ========= =========
sub restore_tokens {
my ( $user ) = @_ ;
my ( $access_token, $access_token_secret ) ;
if ( $config->{ tokens }{ $user } ) {
$access_token = $config->{ tokens }{ $user }{ access_token } ;
$access_token_secret =
$config->{ tokens }{ $user }{ access_token_secret } ;
}
return $access_token, $access_token_secret ;
}
#========= ========= ========= ========= ========= ========= =========
sub save_tokens {
my ( $user, $access_token, $access_token_secret ) = @_ ;
$config->{ tokens }{ $user }{ access_token } = $access_token ;
$config->{ tokens }{ $user }{ access_token_secret } = $access_token_secret ;
#DumpFile( $config_file, $config ) ;
return 1 ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment