Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- '''Takes a series of numbered screenshots.
- NAME
- multishot
- SYNOPSIS
- multishot [-h] [-d DIR] [-f FMT] [-q] [-v] [-V] num-screenshots
- DESCRIPTION
- This script saves a number of screenshots in quick succession, which is
- handy for quickly saving a series of screenshots of a Facebook thread to
- be stitched together using the stitch script. The external commands scrot
- and bell are used to record each screenshot and prompt the user to advance
- the screen by one page. The filenames are indicated by means of a C format
- string. The defult format string is "%02d.png" so that the image filenames
- are 01.png, 02.png, 03.png and so on.
- Author:
- Daniel Neville (Blancmange), creamygoat@gmail.com
- Copyright:
- None
- Licence:
- Public domain
- INDEX
- Imports
- Constants
- Exceptions:
- Error
- ArgError
- FileError
- CmdError
- Main:
- Main()
- '''
- #-------------------------------------------------------------------------------
- # Imports
- #-------------------------------------------------------------------------------
- import sys
- import traceback
- import os
- import time
- import re
- import argparse
- #-------------------------------------------------------------------------------
- # Constants
- #-------------------------------------------------------------------------------
- VersionStr = '1.0.0.0'
- #-------------------------------------------------------------------------------
- # Exceptions
- #-------------------------------------------------------------------------------
- class Error (Exception):
- pass
- class ArgError (Error):
- pass
- class FileError(Error):
- pass
- class CmdError(Error):
- pass
- #-------------------------------------------------------------------------------
- # Main
- #-------------------------------------------------------------------------------
- def Main():
- #-----------------------------------------------------------------------------
- def CheckArgRange(Name, Value, MinValue, MaxValue):
- if not MinValue <= Value <= MaxValue:
- raise ArgError('argument ' + Name + ': out of range ' + #<<<<<<<<<
- str(MinValue) + '..' + str(MaxValue) +'.')
- #-----------------------------------------------------------------------------
- def GetArguments():
- cn = os.path.basename(sys.argv[0])
- Parser = argparse.ArgumentParser(
- prog=cn,
- add_help=False,
- description='Saves a series of numbered screenshots.'
- )
- Parser.add_argument(
- '-h', '--help',
- dest='Help', action='store_true',
- help='Display this message and exit.')
- Parser.add_argument(
- '-d', '--dir', metavar='DIR',
- dest='Dir', default='',
- help=('Specify the directory where the screenshot image files are ' +
- 'to be saved. The default is the current directory.'))
- Parser.add_argument(
- '-f', '--name-format', metavar='FMT',
- dest='NameFormat', default='%02d.png',
- help=('Specify the C format string for the numbered image ' +
- 'file names (starting at 1). The default is "%%02d.png".'))
- Parser.add_argument(
- '-q', '--quiet',
- dest='Quiet', action='store_true',
- help='Suppress all output.')
- Parser.add_argument(
- '-v', '--verbose',
- dest='Verbose', action='store_true',
- help='Display detailed progress information.')
- Parser.add_argument(
- '-V', '--version',
- dest='Version', action='store_true',
- help='Display version and exit.')
- Parser.add_argument(
- 'NumScreenshots', metavar='num-screenshots',
- type=int,
- help=('Specify the number of screenshot files to load. If the ' +
- 'default format string is used, the files will be named ' +
- '01.png, 02.png, 03.png and so on.'))
- if '-h' in sys.argv or '--help' in sys.argv:
- Parser.print_help()
- print (
- '\nExamples:\n' +
- ' ' + cn + ' 29\n' +
- ' ' + cn + ' -d shots/ -f "Screenshot_%03d.png" 110'
- )
- sys.exit(0)
- if '-V' in sys.argv or '--version' in sys.argv:
- print(VersionStr)
- sys.exit(0)
- Args = Parser.parse_args()
- if Args.Version:
- print(VersionStr)
- sys.exit(0)
- setattr(Args, 'Verbosity', 0 if Args.Quiet else (2 if Args.Verbose else 1))
- CheckArgRange('num-screenshots', Args.NumScreenshots, 1, 999)
- try:
- S = Args.NameFormat % (1)
- except (TypeError, ValueError):
- raise ArgError('argument -f/--name-format: Invalid format string.')
- return Args
- #-----------------------------------------------------------------------------
- Result = 0
- ErrMsg = ''
- CmdName = os.path.basename(sys.argv[0])
- try:
- Args = GetArguments()
- Verbosity = Args.Verbosity
- n = Args.NumScreenshots
- if Verbosity >= 1:
- print('Get ready to save ' + str(n) + ' screenshots.')
- time.sleep(5)
- for i in range(1, n + 1):
- ImgFileName = os.path.join(Args.Dir, Args.NameFormat % (i))
- CLIImgFileName = re.sub(r'"', r'\"', ImgFileName)
- if ' ' in CLIImgFileName or "'" in CLIImgFileName:
- CLIImgFileName = '"' + CLIImgFileName + '"'
- Result = os.system('scrot ' + CLIImgFileName)
- if Result != 0:
- raise CmdError('scrot failed with error code ' + str(Result) + '.')
- if Verbosity >= 2:
- print('Saved: ' + CLIImgFileName)
- os.system('qbell')
- if i < n:
- time.sleep(0.500)
- if Verbosity >= 2:
- print('Done!')
- except (ArgError) as E:
- ErrMsg = 'error: ' + str(E)
- Result = 2
- except (FileError) as E:
- ErrMsg = str(E)
- Result = 3
- except (CmdError) as E:
- ErrMsg = str(E)
- Result = 4
- except (Exception) as E:
- exc_type, exc_value, exc_traceback = sys.exc_info()
- ErrLines = traceback.format_exc().splitlines()
- ErrMsg = 'Unhandled exception:\n' + '\n'.join(ErrLines)
- Result = 1
- if ErrMsg != '':
- print(CmdName + ': ' + ErrMsg, file=sys.stderr)
- return Result
- #-------------------------------------------------------------------------------
- # Command line trigger
- #-------------------------------------------------------------------------------
- if __name__ == '__main__':
- sys.exit(Main())
- #-------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement