Advertisement
creamygoat

Multishot

Apr 17th, 2014
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.39 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. '''Takes a series of numbered screenshots.
  4.  
  5. NAME
  6.  multishot
  7.  
  8. SYNOPSIS
  9.  multishot [-h] [-d DIR] [-f FMT] [-q] [-v] [-V] num-screenshots
  10.  
  11. DESCRIPTION
  12.  This script saves a number of screenshots in quick succession, which is
  13.  handy for quickly saving a series of screenshots of a Facebook thread to
  14.  be stitched together using the stitch script. The external commands scrot
  15.  and bell are used to record each screenshot and prompt the user to advance
  16.  the screen by one page. The filenames are indicated by means of a C format
  17.  string. The defult format string is "%02d.png" so that the image filenames
  18.  are 01.png, 02.png, 03.png and so on.
  19.  
  20. Author:
  21.  Daniel Neville (Blancmange), creamygoat@gmail.com
  22.  
  23. Copyright:
  24.  None
  25.  
  26. Licence:
  27.  Public domain
  28.  
  29.  
  30. INDEX
  31.  
  32.  
  33. Imports
  34.  
  35. Constants
  36.  
  37. Exceptions:
  38.  
  39.  Error
  40.  ArgError
  41.  FileError
  42.  CmdError
  43.  
  44. Main:
  45.  
  46.  Main()
  47.  
  48. '''
  49.  
  50.  
  51. #-------------------------------------------------------------------------------
  52. # Imports
  53. #-------------------------------------------------------------------------------
  54.  
  55.  
  56. import sys
  57. import traceback
  58. import os
  59. import time
  60. import re
  61. import argparse
  62.  
  63.  
  64. #-------------------------------------------------------------------------------
  65. # Constants
  66. #-------------------------------------------------------------------------------
  67.  
  68.  
  69. VersionStr = '1.0.0.0'
  70.  
  71.  
  72. #-------------------------------------------------------------------------------
  73. # Exceptions
  74. #-------------------------------------------------------------------------------
  75.  
  76.  
  77. class Error (Exception):
  78.   pass
  79.  
  80. class ArgError (Error):
  81.   pass
  82.  
  83. class FileError(Error):
  84.   pass
  85.  
  86. class CmdError(Error):
  87.   pass
  88.  
  89.  
  90. #-------------------------------------------------------------------------------
  91. # Main
  92. #-------------------------------------------------------------------------------
  93.  
  94.  
  95. def Main():
  96.  
  97.   #-----------------------------------------------------------------------------
  98.  
  99.   def CheckArgRange(Name, Value, MinValue, MaxValue):
  100.     if not MinValue <= Value <= MaxValue:
  101.       raise ArgError('argument ' + Name + ': out of range ' + #<<<<<<<<<
  102.           str(MinValue) + '..' + str(MaxValue) +'.')
  103.  
  104.   #-----------------------------------------------------------------------------
  105.  
  106.   def GetArguments():
  107.  
  108.     cn = os.path.basename(sys.argv[0])
  109.  
  110.     Parser = argparse.ArgumentParser(
  111.       prog=cn,
  112.       add_help=False,
  113.       description='Saves a series of numbered screenshots.'
  114.     )
  115.  
  116.     Parser.add_argument(
  117.         '-h', '--help',
  118.         dest='Help', action='store_true',
  119.         help='Display this message and exit.')
  120.     Parser.add_argument(
  121.         '-d', '--dir', metavar='DIR',
  122.         dest='Dir', default='',
  123.         help=('Specify the directory where the screenshot image files are ' +
  124.             'to be saved. The default is the current directory.'))
  125.     Parser.add_argument(
  126.         '-f', '--name-format', metavar='FMT',
  127.         dest='NameFormat', default='%02d.png',
  128.         help=('Specify the C format string for the numbered image ' +
  129.             'file names (starting at 1). The default is "%%02d.png".'))
  130.     Parser.add_argument(
  131.         '-q', '--quiet',
  132.         dest='Quiet', action='store_true',
  133.         help='Suppress all output.')
  134.     Parser.add_argument(
  135.         '-v', '--verbose',
  136.         dest='Verbose', action='store_true',
  137.         help='Display detailed progress information.')
  138.     Parser.add_argument(
  139.         '-V', '--version',
  140.         dest='Version', action='store_true',
  141.         help='Display version and exit.')
  142.     Parser.add_argument(
  143.         'NumScreenshots', metavar='num-screenshots',
  144.         type=int,
  145.         help=('Specify the number of screenshot files to load. If the ' +
  146.             'default format string is used, the files will be named ' +
  147.             '01.png, 02.png, 03.png and so on.'))
  148.  
  149.     if '-h' in sys.argv or '--help' in sys.argv:
  150.       Parser.print_help()
  151.       print (
  152.         '\nExamples:\n' +
  153.         '  ' + cn + ' 29\n' +
  154.         '  ' + cn + ' -d shots/ -f "Screenshot_%03d.png" 110'
  155.       )
  156.       sys.exit(0)
  157.  
  158.     if '-V' in sys.argv or '--version' in sys.argv:
  159.       print(VersionStr)
  160.       sys.exit(0)
  161.  
  162.     Args = Parser.parse_args()
  163.  
  164.     if Args.Version:
  165.       print(VersionStr)
  166.       sys.exit(0)
  167.  
  168.     setattr(Args, 'Verbosity', 0 if Args.Quiet else (2 if Args.Verbose else 1))
  169.  
  170.     CheckArgRange('num-screenshots', Args.NumScreenshots, 1, 999)
  171.  
  172.     try:
  173.       S = Args.NameFormat % (1)
  174.     except (TypeError, ValueError):
  175.       raise ArgError('argument -f/--name-format: Invalid format string.')
  176.  
  177.     return Args
  178.  
  179.   #-----------------------------------------------------------------------------
  180.  
  181.   Result = 0
  182.   ErrMsg = ''
  183.  
  184.   CmdName = os.path.basename(sys.argv[0])
  185.  
  186.   try:
  187.  
  188.     Args = GetArguments()
  189.  
  190.     Verbosity = Args.Verbosity
  191.  
  192.     n = Args.NumScreenshots
  193.  
  194.     if Verbosity >= 1:
  195.       print('Get ready to save ' + str(n) + ' screenshots.')
  196.     time.sleep(5)
  197.  
  198.     for i in range(1, n + 1):
  199.  
  200.       ImgFileName =  os.path.join(Args.Dir, Args.NameFormat % (i))
  201.  
  202.       CLIImgFileName = re.sub(r'"', r'\"', ImgFileName)
  203.       if ' ' in CLIImgFileName or "'" in CLIImgFileName:
  204.         CLIImgFileName = '"' + CLIImgFileName + '"'
  205.  
  206.       Result = os.system('scrot ' + CLIImgFileName)
  207.  
  208.       if Result != 0:
  209.         raise CmdError('scrot failed with error code ' + str(Result) + '.')
  210.  
  211.       if Verbosity >= 2:
  212.         print('Saved: ' + CLIImgFileName)
  213.  
  214.       os.system('qbell')
  215.  
  216.       if i < n:
  217.         time.sleep(0.500)
  218.  
  219.     if Verbosity >= 2:
  220.       print('Done!')
  221.  
  222.   except (ArgError) as E:
  223.  
  224.     ErrMsg = 'error: ' + str(E)
  225.     Result = 2
  226.  
  227.   except (FileError) as E:
  228.  
  229.     ErrMsg = str(E)
  230.     Result = 3
  231.  
  232.   except (CmdError) as E:
  233.  
  234.     ErrMsg = str(E)
  235.     Result = 4
  236.  
  237.   except (Exception) as E:
  238.  
  239.     exc_type, exc_value, exc_traceback = sys.exc_info()
  240.     ErrLines = traceback.format_exc().splitlines()
  241.     ErrMsg = 'Unhandled exception:\n' + '\n'.join(ErrLines)
  242.     Result = 1
  243.  
  244.   if ErrMsg != '':
  245.     print(CmdName + ': ' + ErrMsg, file=sys.stderr)
  246.  
  247.   return Result
  248.  
  249.  
  250. #-------------------------------------------------------------------------------
  251. # Command line trigger
  252. #-------------------------------------------------------------------------------
  253.  
  254.  
  255. if __name__ == '__main__':
  256.   sys.exit(Main())
  257.  
  258.  
  259. #-------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement