#! /usr/bin/python2.7

# PROG TO determine the number of highs and lows in a week
# REV 3 RKN 06/01/2015  Index pointer fix for new file.
# REV 2 RKN 15/12/2015  Weekday test included
# REV 1 RKN 05/12/2015
#

import urllib2
import schedule
import time
import shutil
import datetime
from datetime import date
import calendar

print datetime.datetime.now()
print "Waiting for Scheduled Run Time"


indexpointer = 1

base = "SharesData/"
#base = "TestSharesData/"



def job():

    daytorun = str(calendar.day_name[date.today().weekday()])
    print daytorun

    if (daytorun == "Monday") or (daytorun == "Tuesday") or (daytorun == "Wednesday") or (daytorun == "Thursday") or (daytorun == "Friday"):
        print "Job Will Run"

        req = urllib2.Request('http://www.barchart.com/stocks/lsehigh.php')

        response = urllib2.urlopen(req)
        the_page = response.read()

        #print the_page

        posoflargetable = the_page.find("largeTable")
        #print posoflargetable
        posofcolsyb = the_page.find("columns=symbol" , posoflargetable)

        #print the_page[posoflargetable:posofcolsyb]

        myline = the_page[posoflargetable:posofcolsyb]

        x = 1
        highs = 0
        posofls=1
        while x == 1:
            newpos = myline.find(".LS", posofls)
            posofls = newpos + 1
            if posofls > 1:
                highs = highs + 1
                
            else:
                print "Highs   ", highs
                x=0
            

        
        req = urllib2.Request('http://www.barchart.com/stocks/lselow.php')

        response = urllib2.urlopen(req)
        the_page = response.read()

        
        posoflargetable = the_page.find("largeTable")
        #print posoflargetable
        posofcolsyb = the_page.find("columns=symbol" , posoflargetable)

        #print the_page[posoflargetable:posofcolsyb]

        myline = the_page[posoflargetable:posofcolsyb]

        x = 1
        lows = 0
        posofls=1
        while x == 1:
            newpos = myline.find(".LS", posofls)
            posofls = newpos + 1
            if posofls > 1:
                lows = lows + 1
                
            else:
                print "Lows    "  , lows
                x=0           


        savefilename = base + "52s.L" + ".txt"
        result = open(savefilename, 'r')

        recordfound = 0

        for line in result:
            commapos = line.find(",")
            indexpoint = line[0:commapos]
            indexpointer = int(indexpoint) + 1
            recordfound = 1
        result.close()

        if recordfound == 0:
            indexpointer = 1


        savefilename = base + "52s.L" + ".txt"
        result = open(savefilename, 'a')
        #now = time.ctime()
        
        now = str(datetime.datetime.now())
        now = now[0:10]
        datatowrite = str(indexpointer) + ", " + str(highs) + ", " +  str(lows) + ", " + "0" ", " + "0" + ", " + str(now)+ ", " + "52s.L" 
        result.write(datatowrite + "\n")
        result.close()



    else:
        print "Job not run its the weekend"
        

#schedule.every(1).minutes.do(job)
schedule.every().day.at("19:30").do(job)


timecount = 0

while True:
    schedule.run_pending()
    time.sleep(1)
    if timecount > 3600:
        print str(datetime.datetime.now())
        print "waiting"
        timecount = 0
    else:
        timecount = timecount + 1


### END OF LISTING ###

