Skip to content

Instantly share code, notes, and snippets.

@ITotalJustice
Last active December 15, 2024 21:07
Show Gist options
  • Save ITotalJustice/17f1239d645978ddbaa77096455d6788 to your computer and use it in GitHub Desktop.
Save ITotalJustice/17f1239d645978ddbaa77096455d6788 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from ftplib import FTP
from io import BytesIO
# pip install python-slugify
from slugify import slugify
import argparse
import time
parser = argparse.ArgumentParser()
parser.add_argument("--ip", required=True, type=str)
parser.add_argument("--username", required=False, default="", type=str)
parser.add_argument("--password", required=False, default="", type=str)
parser.add_argument("--poll", required=False, default=1, type=int)
args = parser.parse_args()
# replace this with your account id that you want to sync with, do LIST /save:/ to see all IDS
ACC_ID = "1000000057CF2FD9BDF4A201A84F639A"
IP = args.ip
USERNAME = args.username
PASSWORD = args.password
POLLRATE = args.poll
cur_tid = ""
cur_name = ""
def poll_ftp(ftp: FTP):
global cur_tid, cur_name
res = ftp.sendcmd("tid")
sp = res.split(' ', 2)
new_tid = cur_tid
new_name = ""
# code = sp[0], tid = sp[1], name = [2]
new_tid = sp[1]
if len(sp) > 2:
new_name = sp[2]
else:
new_name = ""
if cur_tid != new_tid:
# keep copy of old id / save
prev_tid = cur_tid
prev_name = cur_name
# set new id / name
cur_tid = new_tid
cur_name = new_name
#
if prev_tid != "" and prev_tid != "0100000000001000":
buffer = BytesIO()
cmd = "RETR /save:/" + "[" + ACC_ID + "]/zips/" + "[" + prev_tid + "].zip"
ftp.retrbinary(cmd, buffer.write)
filename = prev_tid
if new_name != "":
filename = prev_name + " " + filename
filename = slugify(filename, allow_unicode=True, separator='_', lowercase=False)
with open(filename + ".zip", 'wb') as f:
f.write(buffer.getbuffer())
print("save written to " + filename)
while True:
try:
with FTP(IP, USERNAME, PASSWORD) as ftp:
while True:
poll_ftp(ftp)
time.sleep(POLLRATE)
except Exception as inst:
print("An exception occurred " + str(inst))
time.sleep(POLLRATE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment