O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
Linux Desktop Hacks
By Nicholas Petreley, Jono Bacon
March 2005
More Info

HACK
#65
Put Screenshots Automatically on the Web
Show off to the world what you're up to
The Code
[Discuss (0) | Link to this hack]

The Code

This is where all the magic is. Just write each line into your favorite text editor, whether it is emacs or vim or something else, and save it as autoscreenshot.pl in $localfolder:

#!/usr/bin/perl -w 
use Net::FTP; # Start FTP 

## Define your variables 
$delay = "60"; # Set the screen captures in seconds 
$quality = "50"; # Set the quality of the screenshot
$thumb = "25"; # Set percentage of the thumbnail produced 
$server = "your.server.com"; # Hostname of the server 
$username = "username"; # Put your username for the server here 
$password = "password"; # Put your password for the server here 
$serverfolder = "/home/me/www"; # This is the folder that you want the pictures to 
end up in 
$localfolder = "/home/me/autoscreensnap"; # This is the folder in which you are 
going to locally save the screenshots 

while( ) 
 { 
  system("scrot $localfolder/currentscreen.jpg --thumb $thumb --quality $quality"); 
# Let's take the screenshot
  
  $ftp = Net::FTP->new($server, Debug => 0); # Connect to FTP server 
  $ftp->login($username, $password); # Let's log in 
  print "OK Connected \n"; 
  $ftp->cwd($serverfolder); # Change to the directory you want uploaded image to 
 be in 
  print "OK Changed directories \n"; 
  $ftp->binary( ); # Set binary mode so the picture works 
  $ftp->delete("$serverfolder/currentscreen.jpg"); # Delete old screenshot
  $ftp->delete("$serverfolder/currentscreen-thumb.jpg"); # Delete old thumbnail 
  print "Deleted old screenshots\n"; 
  $ftp->put("$localfolder/currentscreen.jpg"); # Uploading... 
  $ftp->put("$localfolder/currentscreen-thumb.jpg"); #Upload some more.. 
  print "OK Finished uploading files\n"; 
  $ftp->quit; # Close session 
  print "Sleeping for $delay seconds\n\n"; 
  system("sleep $delay"); 
 }

The first half of the script is just to define all your variables. It is safe to change these and it is recommended that you do change them to suit your needs. These options include the following:

$delay

Determines how long the script will wait, in seconds, until it repeats the loop.

$quality

Controls the quality of the screenshot that will be taken. I suggest you use a low number so that it doesn't take long to upload or download the screenshot.

$thumbnail

Defines what percentage of your resolution the thumbnail should be.

$server

The host of your web server; hopefully it has FTP running on it.

$serverfolder

The folder on your web server that will be accessible by the World Wide Web.

$localfolder

Represents the folder where you are going to store the screenshots. I recommend you put it in your home folder just to make everything easy.

The second half of the script uses a loop that repeats itself over and over again. The first command tells the script to use scrot to take a screenshot and create a thumbnail. Then it connects to the web server and changes to a directory visible on the Internet. Once the script has completed, it deletes any old screenshots and thumbnails and uploads the new ones. After it completes this job, it sleeps for 60 seconds by default (this is adjustable) and then continues the loop once again.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.