Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- import requests
- import sys
- import datetime
- import logging
- import smtplib
- from PIL import Image
- from ftplib import FTP
- try:
- import ConfigParser as CP
- except ImportError:
- import configparser as CP
- def getTimeStamp():
- return '{:%Y-%m-%d %H:%M:%S }'.format(datetime.datetime.now())
- config = CP.RawConfigParser()
- config.read('config.ini')
- port = config.getint('FTPSettings', 'port')
- FTPuser = config.get('FTPSettings', 'user')
- FTPpassword = config.get('FTPSettings', 'password')
- host = config.get('FTPSettings', 'host')
- path = config.get('FTPSettings', 'path')
- fileName = config.get('FTPSettings', 'fileName')
- updateInterval = config.getint('Settings', 'updateInterval')
- logFileName = config.get('Settings', 'logFileName')
- url = config.get('HTTPSettings', 'url')
- HTTPUser = config.get('HTTPSettings', 'user')
- HTTPPassword = config.get('HTTPSettings', 'password')
- logging.basicConfig(filename=logFileName, level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
- logging.getLogger("requests").setLevel(logging.WARNING)
- logging.getLogger("urllib3").setLevel(logging.WARNING)
- logging.info("Script started")
- imgDownloaded = False
- while True:
- try:
- r = requests.get(url, auth=(HTTPUser, HTTPPassword), stream=True)
- r.raw.decode_content = True
- im = Image.open(r.raw)
- im.save(fileName, "JPEG")
- imgDownloaded = True
- logging.info("Downloaded image")
- except:
- imgDownloaded = False
- logging.error("Failed to download image")
- if imgDownloaded:
- try:
- ftp = FTP(host)
- ftp.login(FTPuser, FTPpassword)
- ftp.cwd(path)
- file = open(fileName, "rb")
- ftp.storbinary("STOR " + fileName, file)
- ftp.quit()
- file.close()
- logging.info("Uploaded image")
- except:
- logging.error("Failed to upload image to FTP server")
- time.sleep(updateInterval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement