Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #-------------------------------------------------------------------------------
- # Name: Stock Watch
- # Purpose: Helps monitoring stocks/equities in BSE.
- # Currently it employs CUI, but updates can be expected.
- # Currently it only allows one profile, but it can be easily
- # extended.
- #
- # Version: 1.0
- #
- # Author: ADMIN
- #
- # Created: 25/01/2013
- # Copyright: (c) ADMIN 2013
- # Licence: <your licence>
- #-------------------------------------------------------------------------------
- #!/usr/bin/env python
- #-------------------------------------------------------------------------------
- from bs4 import BeautifulSoup
- from urllib2 import urlopen
- import os, re, pickle, csv, copy, sys, time
- #-------------------------------------------------------------------------------
- __version__ = '1.0'
- root = os.getcwd()
- codes = []
- baseUrl = "http://in.finance.yahoo.com/q?s={}.BO"
- attributes = ['Code','Name','Scrip Name','Price', 'Change', 'Percentage change']
- record = dict(zip(attributes,[None]*6))
- outFile = root + r"\out.csv"
- profile = root + r"\default.pro"
- #-------------------------------------------------------------------------------
- def updateProfile():
- with open(profile, 'w') as f:
- pickle.dump(codes, f)
- def addToCodes(element):
- if not element in codes:
- codes.append(element)
- codes.sort()
- updateProfile()
- def removeFromCodes(element):
- if element in codes:
- codes.remove(element)
- updateProfile()
- #-------------------------------------------------------------------------------
- def display():
- f = open(outFile, 'w')
- writer = csv.DictWriter(f, ['Code','Name','Scrip Name','Price', 'Change', 'Percentage change'])
- writer.writeheader()
- for code in codes:
- try:
- stock = BeautifulSoup(urlopen(baseUrl.format(code)))
- except URLError:
- print "Failed to retrieve any data from the servers, \nplese check \
- your Internet connection and try again"
- break
- print "Processing code {}".format(code)
- record['Code'] = code
- t = str(stock.find('div', {'class':'title'}).text).replace('-BSE','')
- record['Name'] = t[:t.find(' (')]
- record['Scrip Name'] = t[t.find(' (')+2:t.find(')')].replace('.BO','')
- record['Price'] = str(stock.find('span', {'class':'time_rtq_ticker'}).text)
- t = str(stock.find('span', {'class':'time_rtq_content'}).text)
- e = re.search(r'(\d*\.?\d+)\((\d*\.?\d+%)\)', t)
- record['Change'] = e.group(1)
- record['Percentage change'] = e.group(2)
- writer.writerow(record)
- f.close()
- print "\nPlease wait while your Excel file is being opened"
- print "Thank You for using Stock Watch"
- time.sleep(2)
- os.startfile(outFile)
- sys.exit()
- #-------------------------------------------------------------------------------
- def main():
- choice = ""
- while choice != 5:
- choice = input("""What do you want to do
- Display current rates (1)
- Add to existing stocks (2)
- Remove from existing stocks (3)
- Show existing stocks (4)
- Leave the program(5)\n--> """)
- if choice == 1:
- display()
- elif choice == 2:
- addToCodes(input("\nEnter stock Code you want to add: "))
- elif choice == 3:
- removeFromCodes(input("\nEnter stock Code you want to remove: "))
- elif choice == 4:
- print '\n', codes
- else:
- print "\n\n\nThank You for using Stock Watch v{}.".format(__version__)
- with open(profile) as f:
- codes = pickle.load(f)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment