#!/usr/bin/python

#
#   ftp upload by ftplib
#
#     2016/10/9
#

## 2018 5/21 for New USB Cam


##  2023/11/2 for New VOLCAT
##  2023/11/20 for Logging VOLCAT

##  2024/9/16 configparser

##  2024/9/17 FTP_TLS  ERROR!!

import sys
from ftplib import FTP
from ftplib import FTP_TLS
import time
import os
import shutil
import logging
from datetime import datetime as dt

import configparser

host0="kazan.ec-site.jp"
host="www3.tins.ad.jp"

#id="vash"
#passwd="nied9763"

id="kazanbai"
passwd="kazan2023"

pasiv=1

ftpdir='ash_nied'

# read from config.ini

config = configparser.ConfigParser()

if  os.path.exists('../config.ini'):

	config.read('../config.ini',encoding='utf-8')

	id = config.get('VOLCAT','ftpid')
	passwd = config.get('VOLCAT','ftppass')
	host = config.get('VOLCAT','ftphost')
	ftpdir = config.get('VOLCAT','ftpdir')
	pasiv = int(config.get('VOLCAT','ftppasive'))

argvs = sys.argv
argc = len(argvs)
#print argvs
#print argc

if argc == 1 :
   print ('no image')
   quit()

fn=argvs[1]


now=dt.now()

nowdate = now.strftime('%Y/%m/%d %H:%M:%S ')
print("Start FTP " + nowdate + fn )

ftp = FTP();

#ftp = FTP_TLS(host);

#ftp.set_debuglevel(1)    # for  LOG

ftp.connect(host,21,30)   #time out 30sec


print(ftp.login(id,passwd))  # log

ftp.set_pasv(pasiv)

dir = now.strftime('%Y')


ftp.cwd(ftpdir)
print(ftp.cwd(dir))

for fn in argvs[1:]:
	print(fn)
	fp=open(fn,"rb")
	cmd = "STOR "+fn
	print(ftp.storbinary(cmd,fp))  #log
	fp.close()
	shutil.move(fn,"../image/"+dir)

#debug
#ftp.retrlines('LIST')

ftp.quit()


