Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Get the latest Index from BSE and NSE stock market. Python script using google finance open API
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from collections import namedtuple
import json
from urllib.request import Request, urlopen
except ImportError: # python 2
from urllib2 import Request, urlopen
__author__ = 'arulraj'
Stock = namedtuple("Stock", ["Index", "Current", "Change_pts", "Change_percent", "Updated_on"])
def build_url():
symbol_list = ','.join([stock for stock in STOCK_CODE_LIST])
return '' \
+ symbol_list
def get_content(url):
req = Request(url)
resp = urlopen(req)
content ='ascii', 'ignore').strip()
content = content[3:]
return content
def parse_content(content):
:param content:
stock_resp_list = json.loads(content)
list_stock = list()
for stock_resp in stock_resp_list:
isPositive = False if stock_resp["cp"] != None and len(stock_resp["cp"]) > 0 and stock_resp["cp"][0] == '-' else True
colored_cp = stock_resp["cp"]
if isPositive:
colored_cp =["cp"])
colored_cp =["cp"])
list_stock.append(Stock(stock_resp["t"], stock_resp["l"], stock_resp["c"], colored_cp, stock_resp["lt"]))
return list_stock
def pprinttable(rows):
From and modified bit to support UTF-8
:param rows:
if len(rows) > 1:
headers = rows[0]._fields
lens = []
for i in range(len(rows[0])):
lens.append(len(max([x[i] for x in rows] + [headers[i]], key=lambda x: len(str(x)))))
formats = []
hformats = []
for i in range(len(rows[0])):
if isinstance(rows[0][i], int):
formats.append("%%%dd" % lens[i])
formats.append("%%-%ds" % lens[i])
hformats.append("%%-%ds" % lens[i])
pattern = " | ".join(formats)
hpattern = " | ".join(hformats)
separator = "-+-".join(['-' * n for n in lens])
print hpattern % tuple(headers)
print separator
_u = lambda t: t.decode('UTF-8', 'replace') if isinstance(t, str) else t
for line in rows:
print pattern % tuple(_u(t) for t in line)
elif len(rows) == 1:
row = rows[0]
hwidth = len(max(row._fields, key=lambda x: len(x)))
for i in range(len(row)):
print "%*s = %s" % (hwidth, row._fields[i], row[i])
def preety_print_stock(stock_list, preety=True):
:param stock_list:
if not preety:
print """"%s", "%s", "%s", "%s", "%s" """ % (Stock._fields)
for stock in stock_list:
print """"%s", "%s", "%s", "%s", "%s" """ % (stock[0], stock[1], stock[2], stock[3], stock[4])
class Color(object):
reference from and modified bit.
def __init__(self, text, **user_styles):
styles = {
# styles
'reset': '\033[0m',
'bold': '\033[01m',
'disabled': '\033[02m',
'underline': '\033[04m',
'reverse': '\033[07m',
'strike_through': '\033[09m',
'invisible': '\033[08m',
# text colors
'fg_black': '\033[30m',
'fg_red': '\033[31m',
'fg_green': '\033[32m',
'fg_orange': '\033[33m',
'fg_blue': '\033[34m',
'fg_purple': '\033[35m',
'fg_cyan': '\033[36m',
'fg_light_grey': '\033[37m',
'fg_dark_grey': '\033[90m',
'fg_light_red': '\033[91m',
'fg_light_green': '\033[92m',
'fg_yellow': '\033[93m',
'fg_light_blue': '\033[94m',
'fg_pink': '\033[95m',
'fg_light_cyan': '\033[96m',
# background colors
'bg_black': '\033[40m',
'bg_red': '\033[41m',
'bg_green': '\033[42m',
'bg_orange': '\033[43m',
'bg_blue': '\033[44m',
'bg_purple': '\033[45m',
'bg_cyan': '\033[46m',
'bg_light_grey': '\033[47m'
self.color_text = ''
for style in user_styles:
self.color_text += styles[style]
except KeyError:
raise KeyError('def color: parameter `{}` does not exist'.format(style))
self.color_text += text
def __format__(self):
return '{}\033[0m\033[0m'.format(self.color_text)
def red(clazz, text):
cls = clazz(text, bold=True, fg_red=True)
return cls.__format__()
def orange(clazz, text):
cls = clazz(text, bold=True, fg_orange=True)
return cls.__format__()
def green(clazz, text):
cls = clazz(text, bold=True, fg_green=True)
return cls.__format__()
def custom(clazz, text, **custom_styles):
cls = clazz(text, **custom_styles)
return cls.__format__()
if __name__ == "__main__":
json_content = get_content(build_url())
stock_list = parse_content(json_content)
preety_print_stock(stock_list, True)

arulrajnet commented May 3, 2015

wget --no-check-certificate
chmod +x
sudo cp /usr/bin/stockmarketindia

Then run stockmarketindia from your terminal. the output will be



arulrajnet commented Aug 17, 2015

New revision have colored output


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment