Skip to content

Instantly share code, notes, and snippets.

@GuyKroizman
Forked from DanielBorgesOliveira/telegram.py
Last active September 14, 2021 06:33
Show Gist options
  • Save GuyKroizman/7de7e899db4ddaccf9f819d7b2131db7 to your computer and use it in GitHub Desktop.
Save GuyKroizman/7de7e899db4ddaccf9f819d7b2131db7 to your computer and use it in GitHub Desktop.
skip incomplete mp4
#!/usr/bin/python3
import requests
class telegram():
def __init__(
self,
TelegramToken,
TelegramChatID,
):
self.TelegramToken = TelegramToken
self.TelegramChatID = TelegramChatID
def send_message(self, text):
files = {
'chat_id': (None, self.TelegramChatID),
'text': text,
}
response = requests.get("https://api.telegram.org/bot"+self.TelegramToken+"/sendMessage?chat_id="+self.TelegramChatID+"&text="+text, files=files)
return response
def send_file(self, FileName, FilePath):
files = {
'chat_id': (None, self.TelegramChatID),
'document': (FileName, open(FilePath, 'rb')),
}
response = requests.post("https://api.telegram.org/bot"+self.TelegramToken+"/sendDocument", files=files)
return response
def send_video(self, FileName, FilePath):
files = {
'chat_id': (None, self.TelegramChatID),
'video': (FileName, open(FilePath, 'rb')),
}
response = requests.post("https://api.telegram.org/bot"+self.TelegramToken+"/sendVideo", files=files)
return response
#!/usr/bin/python3
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import datetime
from subprocess import Popen,DEVNULL
# https://realpython.com/python-logging/
import sys
sys.path.append("/root/watchdog/")
from watchdog_logging import *
import telegram
class MyHandler(FileSystemEventHandler):
def __init__(
self,
logger,
telegram,
):
self.AlreadySended = []
self.logger = logger
self.telegram = telegram
def on_modified(self, event):
self.logger.error(f"File modified: {event.src_path}")
def on_created(self, event):
self.logger.error(f"File created: {event.src_path}")
if '.mp4' in event.src_path and \
not event.src_path in self.AlreadySended and \ # This avoid send file than once.
not 'incomplete' in event.src_path:
# Send telegram message about the event
#Text = f'event type: {event.event_type} path : {event.src_path}'
#self.telegram.send_message(Tex)
# Send the file via telegram
self.logger.error(f'Sending file via telegram. Group: {self.telegram.TelegramChatID}')
Time = datetime.datetime.now()
FileName='Video_'+str(Time.day)+"-"+str(Time.month)+\
"-"+str(Time.year)+"_"+str(Time.hour)+"-"+\
str(Time.minute)+"-"+str(Time.second)+".mp4"
# Send file to telegram
attempts=0
while attempts < 10:
try:
self.telegram.send_video(FileName, event.src_path)
self.logger.error('File sended')
self.AlreadySended.append(event.src_path)
break
except:
attempts += 1
self.logger.error('Error trying send file.')
def on_deleted(self, event):
self.logger.error(f"File deleted: {event.src_path}")
def on_moved(self, event):
self.logger.error(f"File moved: {event.src_path} to {event.dest_path}")
path = '/media/pd/zoneminder/events/2/'
if __name__ == "__main__":
tg = telegram.telegram(
TelegramToken = "< Your bot token here>",
TelegramChatID = '< Your chat ID here >')
event_handler = MyHandler(
logger = logger,
telegram = tg)
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
try:
while True:
time.sleep(60)
except KeyboardInterrupt:
observer.stop()
observer.join()
#!/usr/bin/python3
# https://realpython.com/python-logging/
import logging
from logging.handlers import RotatingFileHandler
# Create a custom logger
logger = logging.getLogger(__name__)
# Create handlers
c_handler = logging.StreamHandler()
c_handler.setLevel(logging.NOTSET)
f_handler = logging.FileHandler('/tmp/watchdog_zoneminder.log')
f_handler.setLevel(logging.DEBUG)
# Create formatters and add it to handlers
c_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', '%m/%d/%Y %I:%M:%S')
c_handler.setFormatter(c_format)
f_format = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s', '%m/%d/%Y %I:%M:%S')
f_handler.setFormatter(f_format)
# Rotate the log file.
rotating_handler = RotatingFileHandler('/tmp/watchdog_zoneminder.log', maxBytes=2000, backupCount=5)
logger.addHandler(rotating_handler)
# Add handlers to the logger
logger.addHandler(c_handler)
logger.addHandler(f_handler)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment