#!/usr/bin/env python2.7
# encoding: utf-8

## Get filings from master list
## Available here: ftp://ftp.sec.gov/edgar/full-index/
## Jasper GInn
## 07/05/2016

## Call function from terminal
## python processMasterFilings.py </path/to/master/list> </path/to/output/csv> <document type>

import sys

# Class

class processFilings:
    
    def __init__(self, file_path_input, file_path_output):
        self.file_path_input = file_path_input
        self.file_path_output = file_path_output
        
    def filterFilings(self, filing):
        # Open input
        with open(self.file_path_input, 'r') as filings:
            for line in filings:
                if "|10-K|" in line:
                    elements = line.split("|")
                    with open(self.file_path_output, 'a') as outfile:
                        outfile.write("{};{};{};{};ftp://ftp.sec.gov/{}".format(elements[0],elements[1].replace(",", ""),elements[2],elements[3],elements[4]))

# Run
if __name__ == "__main__":
    infile = sys.argv[1]
    outfile = sys.argv[2]
    filing = sys.argv[3]
    # Call
    p = processFilings(infile, outfile)
    # Read/write
    p.filterFilings(filing)