# install BeautifulSoup4 before running
#
# prints out historical data in csv format:
#
# [date, open, high, low, close, volume]
#
import re, csv, sys, urllib2
from bs4 import BeautifulSoup

# If start date and end date is the same only one value will be returned and
# if not the multiple values which can be used to make calculations
#
# ticker (company symbol)
# interval (d (daily), m (monthly), q (quarterly), y (yearly))
# start_date (YYYYMMDD)
# end_date (YYYYMMDD)
def get_historical_data(ticker, interval, start_date, end_date):

    #pathToCSV = '/Users/Michal/Downloads/dialogflow-java-client-master2/samples/clients/VirtualTradingAssistant/src/main/java/ai/api/examples/fileStore/file.csv'
    #pathToCSV = 'C:\\Users\\ojwoo\\Documents\\Warwick\\CS261\\Coursework\\dialogflow-java-client-master\\samples\\clients\\VirtualTradingAssistant\\src\\main\\java\\ai\\api\\examples\\fileStore\\file.csv'
    #pathToCSV = '/Users/Michal/Desktop/apache-tomcat-8.5.28/bin/misc/file.csv'
    pathToCSV = 'C:\\apache-tomcat-8.5.28\\bin\\misc\\file.csv'

    url_builder = []
    url_builder.append('https://stooq.com/q/d/?s=')
    url_builder.append(ticker)
    url_builder.append('&c=0&d1=')
    url_builder.append(start_date)
    url_builder.append('&d2=')
    url_builder.append(end_date)
    url_builder.append('&i=')
    url_builder.append(interval)

    url = ''.join(url_builder)

    page = urllib2.urlopen(url)

    soup = BeautifulSoup(page, 'html.parser')

    link = soup.findAll('a', href=re.compile('^q/d/l/'))

    link = re.search('"(.*)"', str(link))

    try:
        link = link.group(1)
    except AttributeError:
        with open(pathToCSV, 'w') as csvfile:
            wr = csv.writer(csvfile, delimiter='@', quotechar='#')
            wr.writerow('')
            exit()

    link = link.replace('amp;', '')

    arr = []

    arr.append('https://stooq.com/')

    arr.append(link)

    link = ''.join(arr)

    response = urllib2.urlopen(link)

    cr = csv.reader(response)
    with open(pathToCSV, 'w') as csvfile:
        wr = csv.writer(csvfile, delimiter='@', quotechar='#')
        wr.writerows(cr)

def main():
    args = sys.argv

    get_historical_data(args[1], args[2], args[3], args[4])

if __name__ == '__main__':
    main()

[((36, 11, 36, 31), 'urllib2.urlopen', 'urllib2.urlopen', ({(36, 27, 36, 30): 'url'}, {}), '(url)', False, 'import re, csv, sys, urllib2\n'), ((38, 11, 38, 45), 'bs4.BeautifulSoup', 'BeautifulSoup', ({(38, 25, 38, 29): 'page', (38, 31, 38, 44): '"""html.parser"""'}, {}), "(page, 'html.parser')", False, 'from bs4 import BeautifulSoup\n'), ((62, 15, 62, 36), 'urllib2.urlopen', 'urllib2.urlopen', ({(62, 31, 62, 35): 'link'}, {}), '(link)', False, 'import re, csv, sys, urllib2\n'), ((64, 9, 64, 29), 'csv.reader', 'csv.reader', ({(64, 20, 64, 28): 'response'}, {}), '(response)', False, 'import re, csv, sys, urllib2\n'), ((66, 13, 66, 62), 'csv.writer', 'csv.writer', (), '', False, 'import re, csv, sys, urllib2\n'), ((40, 34, 40, 55), 're.compile', 're.compile', ({(40, 45, 40, 54): '"""^q/d/l/"""'}, {}), "('^q/d/l/')", False, 'import re, csv, sys, urllib2\n'), ((48, 17, 48, 66), 'csv.writer', 'csv.writer', (), '', False, 'import re, csv, sys, urllib2\n')]