-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftp-articles.py
34 lines (27 loc) · 1.01 KB
/
ftp-articles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# This file uploads the feed_articles.csv to the FTP server
from ftplib import FTP
import datetime
# Set current date/time of script run
currentDT = datetime.datetime.now()
cleanDate = currentDT.strftime("%Y-%m-%d %H:%M:%S")
# Store login credentials
ftp = FTP('yourdomain.org')
ftp.login('username@yourdomain.org', 'somepass')
# Change to News folder
ftp.cwd('news')
# Check for yesterday's backup of the articles, and delete it,
# then rename today's to .old to create a backup
oldCsv = 'feed-articles.old'
newCsv = 'feed-articles.csv'
if oldCsv in ftp.nlst('feed-articles.old'):
ftp.delete('feed-articles.old')
ftp.rename('feed-articles.csv', 'feed-articles.old')
# Copy the new file to the ftp server
with open('feed-articles.csv', 'rb') as f:
ftp.storlines('STOR %s' % 'feed-articles.csv', f)
# Verify the both files exist on the server and log the results
if oldCsv and newCsv in ftp.nlst():
log = open('feedlog.txt', 'a')
log.write(cleanDate + " feed-articles.csv has been uploaded\n")
log.close()
ftp.quit()