Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2016/06/21

Personal Programming Plans: Instagram2Background

I have this code which uses WebService::Instagram to grab my most recent picture from Instagram and set it as background image on my Ubuntu machines. I put it in my crontab and it just works.

  1. #!/usr/bin/env perl  
  2.   
  3. use feature qw'say' ;  
  4. use strict ;  
  5. use warnings ;  
  6.   
  7. use Cwd 'abs_path' ;  
  8. use Data::Dumper ;  
  9. use IO::Interactive qw{ interactive } ;  
  10. use Try::Tiny ;  
  11. use YAML::XS qw{ LoadFile DumpFile } ;  
  12.   
  13. use lib '/home/jacoby/lib' ;  
  14. use Instagram ;  
  15.   
  16. my $config_file = join '/'$ENV{HOME}, '.i2b.yml' ;  
  17. my $config = LoadFile($config_file) ;  
  18.   
  19. my $token    = $config->{user}{access_token} ;  
  20. my $id       = $config->{user}{id} ;  
  21. my $template = 'https://api.instagram.com/v1/users/XX/media/recent' ;  
  22. my $ig       = connect($config) ;  
  23.   
  24. $ig->set_access_token($token) ;  
  25. my $url = $template ;  
  26. $url =~ s/XX/$id/ ;  
  27. my $feed        = $ig->request($url) ;  
  28. my $data        = $feed->{data} ;  
  29. my @data        = grep { $_->{type} eq 'image' } @$data ;  
  30. my $most_recent = $data[0] ;  
  31. my $file        = '/home/jacoby/.i2b/i2b.jpg' ;  
  32.   
  33. my $image_id    = $most_recent->{id} ;  
  34. my $image_url   = $most_recent->{images}->{standard_resolution}->{url} ;  
  35. my $image_text  = $most_recent->{caption}->{text} ;  
  36.   
  37. if ( $config->{done} ne $image_id ) {  
  38.     my $image = imagegrab($image_url) ;  
  39.     imagewrite( $file$image ) ;  
  40.     say {interactive} $image_id ;  
  41.     say {interactive} $image_text ;  
  42.     $config->{done} = $image_id ;  
  43.     }  
  44. imageset($file) ;  
  45.   
  46. DumpFile( $config_file$config ) ;  
  47.   
  48. exit ;  
  49.   
  50. # takes a URL, returns the raw content  
  51. sub imagegrab {  
  52.     my $url      = shift ;  
  53.     my $agent    = new LWP::UserAgent ;  
  54.     my $request  = new HTTP::Request( 'GET'$url ) ;  
  55.     my $response = $agent->request($request) ;  
  56.     if ( $response->is_success ) {  
  57.         return $response->content ;  
  58.         }  
  59.     return undef ;  
  60.     }  
  61.   
  62. # takes an filename and an image, and writes image to filename  
  63. sub imagewrite {  
  64.     my $file  = shift ;  
  65.     my $image = shift ;  
  66.     if ( open my $fh'>'$file ) {  
  67.         print $fh $image ;  
  68.         close $fh ;  
  69.         return 1 ;  
  70.         }  
  71.     return 0 ;  
  72.     }  
  73.   
  74. # takes a filename, sets it as backgroundimages  
  75. sub imageset {  
  76.     my $img = shift ;  
  77.   
  78.     return unless $img ;  
  79.     my $command = join ' 'qw{  
  80.         gsettings set  
  81.         org.gnome.desktop.background  
  82.         picture-uri  
  83.         } ;  
  84.     my $command2 = join ' 'qw{  
  85.         gsettings set  
  86.         org.gnome.desktop.background  
  87.         picture-options  
  88.         } ;  
  89.   
  90.     my $bg = '"file://' . abs_path $img . '"' ;  
  91.     qx{$command $bg} ;  
  92.     qx{$command2 'zoom'} ;  
  93.     }  


With IFTTT, it's even easier on Android.

But I don't spend all my time with just Android and Ubuntu. I spend a fair amount of time in Windows. I have a start with that: I can use C# to set an image to the background. This is the first step. I know, at least a little, about scheduling tasks in Windows, which is the last step.

So, the coming steps:

  • Using C# to get the Instagram JSON
  • Using C# to parse the Instagram JSON and pull URL of newest image
  • Using C# to download said image. Certainly related to getting the JSON.
  • Using C# to write image file to Windows equivalent to /tmp (because this will be released to others).
  • Knowing what Windows for /tmp is.
  • Knowing where to hold my Instagram OAuth token data.
Seems like a small number of things to get done, once I sit down to do them. I just need to sit down, do them, and build the tool again.

1 comment:

  1. Thank you for sharing this plan. I think we would need an editor for our coding stuff right? So download vscode

    ReplyDelete