aboutsummaryrefslogtreecommitdiffstats
path: root/python/download_daily.py
blob: 51cb17ba9e3a84c8a08241b6b90bc27e2cbefa1d (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import os.path
import datetime
from ftplib import FTP
import gnupg
import config
import sys
import re

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

def get_ped(s):
    regex = re.search("PED=(.+?)\.", s)
    if regex:
        PED = datetime.datetime.strptime(regex.group(1), "%Y-%m-%d").date()
    else:
        regex = re.search("(.+?)\.", s)
        PED = datetime.datetime.strptime(regex.group(1), "%Y%m%d").date() - \
              datetime.timedelta(1)
    return PED

def key_fun(s):
    PED = get_ped(s)
    regex = re.search("KD=(.+?)\.", s)
    if regex:
        KD = datetime.datetime.strptime(regex.group(1), "%Y-%m-%d-%H-%M-%S")
    else:
        regex = re.search("(.+?\..+?)\.", s)
        KD = datetime.datetime.strptime(regex.group(1), "%Y%m%d.%H%M%S")
    return (PED, KD)

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

    pnlfiles = [filename for filename in files if "csv" in filename and \
                "Profit" in filename if get_ped(filename) < workdate]
    valuationfiles = [filename for filename in files if "csv" in filename and \
                      "Valuation" in filename if get_ped(filename) < workdate]

    pnlfile = sorted(pnlfiles, key=key_fun, reverse=True)[0]
    valuationfile = sorted(valuationfiles, key=key_fun, reverse=True)[0]

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

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

    if os.name=='nt':
        gpg = gnupg.GPG(gpgbinary = r'"c:\\Program Files (x86)\\GNU\\GnuPG\\gpg2.exe"',
                        gnupghome = os.path.join(os.getenv('APPDATA'), "gnupg"))
    elif os.name == 'posix':
        gpg = gnupg.GPG(gnupghome = os.path.join(os.getenv('HOME'), '.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, str(workdate), "Reports", filename), "rb") as fh:
            gpg.decrypt_file(fh, output = os.path.join(root, str(workdate), "Reports", newfilename),
                             passphrase=config.key_password)
        os.remove(os.path.join(root, str(workdate), "Reports", filename))

if __name__=="__main__":
    if len(sys.argv) > 1:
        workdate = datetime.datetime.strptime(sys.argv[1], "%Y-%m-%d").date()
    else:
        workdate = datetime.date.today()
    download_data(workdate)