aboutsummaryrefslogtreecommitdiffstats
path: root/python/download_daily.py
blob: c204bc7e5ca63a329fbdcc579c80d7cfd0f0e51c (plain)
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
import os
import os.path
import datetime
from datetime import date
from datetime import datetime
from ftplib import FTP
import gnupg
import pdb
import config
import pdb

if os.name =='nt':
    root = "//WDsentinel/share/Daily"
elif os.name == 'posix':
    root = '/home/share/Daily'

ftp = FTP('ftp.globeop.com')
ftp.login('srntsftp', config.ftp_password)
ftp.cwd('outgoing')
files = ftp.nlst()

workdate = str(datetime.today().date())
globeopdate = datetime.strftime(datetime.today(), '%Y%m%d')

pnlfiles = [filename for filename in files if "csv" in filename and \
                globeopdate in filename and \
                "Profit" in filename]
valuationfiles = [filename for filename in files if "csv" in filename and \
                      globeopdate in filename and \
                      "Valuation" in filename]
pnlfile = sorted(pnlfiles, reverse=True)[0]
valuationfile = sorted(valuationfiles, reverse=True)[0]

if pnlfile:
    if not os.path.exists(os.path.join(root, workdate, "Reports")):
        os.makedirs(os.path.join(root, workdate, "Reports"))

for filename in [pnlfile, valuationfile]:
    with open(os.path.join(root, workdate, "Reports", filename), "wb") as fh:
        ftp.retrbinary('RETR ' + filename, fh.write)
    print "downloaded {0}".format(filename)

gpg = gnupg.GPG(gnupghome = '/home/guillaume/.gnupg')
gpg.encoding = 'utf8'
for filename in [pnlfile, valuationfile]:
    if "Profit" in filename:
        newfilename = "Pnl.csv"
    else:
        newfilename = "Valuation_Report.csv"
    with open(os.path.join(root, workdate, "Reports", filename), "rb") as fh:
        gpg.decrypt_file(fh, output = os.path.join(root, workdate, "Reports", newfilename),
                         passphrase=config.key_password)
    os.remove(os.path.join(root, workdate, "Reports", filename))