80 lines
2.5 KiB
Python
Executable File
80 lines
2.5 KiB
Python
Executable File
#!/usr/bin/env python2
|
|
|
|
import os
|
|
import sys
|
|
import argparse
|
|
import ConfigParser
|
|
import datetime
|
|
#import imghdr
|
|
from PIL import Image
|
|
import shutil
|
|
|
|
version = 0.1
|
|
|
|
configFile = '/etc/telegram-notify/telegram-notify.conf'
|
|
if not os.path.isfile(configFile) or not os.access(configFile, os.R_OK):
|
|
configFile = './telegram-notify.conf'
|
|
if not os.path.isfile(configFile) or not os.access(configFile, os.R_OK):
|
|
configFile = ''
|
|
|
|
parser = argparse.ArgumentParser(description='Send notification via Telegram')
|
|
parser.add_argument('-c', '--config', nargs='?',
|
|
help='start with specified config file (default: telegram-notify.conf in /etc/ or in current directory)')
|
|
parser.add_argument('-t', '--text', nargs='?',
|
|
help='send text')
|
|
parser.add_argument('-i', '--image', nargs='?',
|
|
help='send image')
|
|
parser.add_argument('-V', '--version', action='store_true',
|
|
help='show program version and quit')
|
|
args = parser.parse_args()
|
|
|
|
if args.version:
|
|
print 'telegram-notify version %s - Copyright (C) 2018 by Paolo Asperti.' % version
|
|
sys.exit(0)
|
|
|
|
if args.config:
|
|
if not os.path.isfile(args.config):
|
|
print 'specified config file doesn\'t exists or is not a file'
|
|
sys.exit(1)
|
|
if not os.access(args.config, os.R_OK):
|
|
print 'specified config file is not readable'
|
|
sys.exit(1)
|
|
configFile = args.config
|
|
|
|
if configFile == '':
|
|
print 'no config file available'
|
|
sys.exit(1)
|
|
|
|
defaults = {
|
|
'spool_dir': '/var/spool/telegram-notify'
|
|
}
|
|
|
|
configParser = ConfigParser.SafeConfigParser(defaults=defaults)
|
|
configParser.read(configFile)
|
|
|
|
spool_dir = configParser.get('general', 'spool_dir')
|
|
t = datetime.datetime.now()
|
|
curdate = t.strftime('%Y-%m-%d_%H:%M:%S')
|
|
|
|
if args.text:
|
|
outfile = spool_dir + '/' + curdate + '.txt'
|
|
f = open(outfile,"w")
|
|
f.write(args.text)
|
|
f.close()
|
|
|
|
if args.image:
|
|
if not os.path.isfile(args.image):
|
|
print 'specified image file doesn\'t exists or is not a file'
|
|
sys.exit(1)
|
|
if not os.access(args.image, os.R_OK):
|
|
print 'specified image file is not readable'
|
|
sys.exit(1)
|
|
im = Image.open(args.image)
|
|
imgType = im.format
|
|
supportedTypes = {'JPEG':'jpg', 'GIF':'gif', 'PNG':'png'}
|
|
if any(imgType == s for s in supportedTypes):
|
|
outfile = spool_dir + '/' + curdate + '.' + supportedTypes[imgType]
|
|
shutil.copyfile(args.image,outfile)
|
|
else:
|
|
print 'unsupported image type (%s) for file %s' % (imgType, args.image)
|