Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. import time
  2. import requests
  3. import sys
  4. import datetime
  5. import logging
  6. import smtplib
  7.  
  8. from PIL import Image
  9. from ftplib import FTP
  10.  
  11. try:
  12.     import ConfigParser as CP
  13. except ImportError:
  14.     import configparser as CP
  15.  
  16.  
  17. def getTimeStamp():
  18.     return '{:%Y-%m-%d %H:%M:%S  }'.format(datetime.datetime.now())
  19.  
  20. config = CP.RawConfigParser()
  21. config.read('config.ini')
  22.  
  23.  
  24. port = config.getint('FTPSettings', 'port')
  25. FTPuser = config.get('FTPSettings', 'user')
  26. FTPpassword = config.get('FTPSettings', 'password')
  27. host = config.get('FTPSettings', 'host')
  28. path = config.get('FTPSettings', 'path')
  29. fileName = config.get('FTPSettings', 'fileName')
  30.  
  31.  
  32. updateInterval = config.getint('Settings', 'updateInterval')
  33. logFileName = config.get('Settings', 'logFileName')
  34.  
  35. url = config.get('HTTPSettings', 'url')
  36. HTTPUser = config.get('HTTPSettings', 'user')
  37. HTTPPassword = config.get('HTTPSettings', 'password')
  38.  
  39. logging.basicConfig(filename=logFileName, level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
  40. logging.getLogger("requests").setLevel(logging.WARNING)
  41. logging.getLogger("urllib3").setLevel(logging.WARNING)
  42.  
  43. logging.info("Script started")
  44.  
  45. imgDownloaded = False
  46. while True:
  47.     try:
  48.         r = requests.get(url, auth=(HTTPUser, HTTPPassword), stream=True)
  49.         r.raw.decode_content = True
  50.         im = Image.open(r.raw)
  51.         im.save(fileName, "JPEG")
  52.         imgDownloaded = True
  53.         logging.info("Downloaded image")
  54.     except:
  55.         imgDownloaded = False
  56.         logging.error("Failed to download image")
  57.  
  58.     if imgDownloaded:
  59.         try:
  60.             ftp = FTP(host)
  61.             ftp.login(FTPuser, FTPpassword)
  62.             ftp.cwd(path)  
  63.             file = open(fileName, "rb")
  64.             ftp.storbinary("STOR " + fileName, file)
  65.             ftp.quit()
  66.             file.close()
  67.             logging.info("Uploaded image")
  68.         except:
  69.             logging.error("Failed to upload image to FTP server")
  70.     time.sleep(updateInterval)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement