This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch_and_process_oclc.py
54 lines (48 loc) · 2 KB
/
fetch_and_process_oclc.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/python
from ftplib import FTP
import os, time
import oclc_credentials
import windows_paths as paths #windows_paths or linux_paths
from datetime import date, datetime, timedelta
ftp = FTP(oclc_credentials.server)
ftp.login(oclc_credentials.username, oclc_credentials.password)
print("OCLC login successful")
#New files
ftp.cwd('metacoll/out/ongoing/new')
files = []
ftp.dir(files.append)
for line in files:
col_list = line.split()
date_str = ' '.join(line.split()[5:8])
file_date = datetime.strptime(date_str, '%b %d %H:%M').date().replace(date.today().year)
if ((date.today() - file_date) < timedelta(weeks=1)):
filename = col_list[8]
ftp.retrbinary('RETR %s' % filename, open(paths.new + filename, 'a+').write)
print("New record file " + filename + " downloaded")
os.system('python ' + paths.scripts_dir + 'prep_oclc.py ' + paths.new + filename)
#Update files
ftp.cwd('../updates')
files = []
ftp.dir(files.append)
for line in files:
col_list = line.split()
date_str = ' '.join(line.split()[5:8])
file_date = datetime.strptime(date_str, '%b %d %H:%M').date().replace(date.today().year)
if ((date.today() - file_date) < timedelta(weeks=1)):
filename = col_list[8]
ftp.retrbinary('RETR %s' % filename, open(paths.update + filename, 'a+').write)
print("Update record file " + filename + " downloaded")
os.system('python ' + paths.scripts_dir + 'prep_oclc.py ' + paths.update + filename)
#Delete files
ftp.cwd('../deletes')
files = []
ftp.dir(files.append)
for line in files:
col_list = line.split()
date_str = ' '.join(line.split()[5:8])
file_date = datetime.strptime(date_str, '%b %d %H:%M').date().replace(date.today().year)
if ((date.today() - file_date) < timedelta(weeks=1)):
filename = col_list[8]
ftp.retrbinary('RETR %s' % filename, open(paths.delete + filename, 'a+').write)
print("Delete record file " + filename + " downloaded")
os.system('python ' + paths.scripts_dir + 'delete_oclc.py ' + paths.delete + filename)