def nse_index_watch(symbol): from nsetools import Nse nse = Nse() ''' q = nse.get_quote('infy') # it's ok to use both upper or lower case for codes. from pprint import pprint # just for neatness of display pprint(q) ''' ''' help(nse) get_top_fno_losers nse.get_top_fno_gainers() ''' #fno_gainer = json_normalize(nse.get_fno_lot_sizes()).T fno_gainer = json_normalize(nse.get_advances_declines()) fno_gainer['adratio']=fno_gainer['advances']/fno_gainer['declines'] return round(float(fno_gainer[fno_gainer.indice==symbol]['adratio']),2)
class TestCoreAPIs(unittest.TestCase): def setUp(self): self.nse = Nse() def test_string_representation(self): self.assertEqual(str(self.nse) , "Driver Class for National Stock Exchange (NSE)") def test_instantiate_abs_class(self): class Exchange(AbstractBaseExchange): pass with self.assertRaises(TypeError): exc = Exchange() def test_nse_headers(self): ret = self.nse.nse_headers() self.assertIsInstance(ret, dict) def test_nse_opener(self): ''' should not raise any exception ''' opener = self.nse.nse_opener() def test_build_url_for_quote(self): test_code = 'infy' url = self.nse.build_url_for_quote(test_code) # 'test_code' should be present in the url self.assertIsNotNone(re.search(test_code, url)) def test_negative_build_url_for_quote(self): negative_codes = [1, None] with self.assertRaises(Exception): for test_code in negative_codes: url = self.nse.build_url_for_quote(test_code) def test_response_cleaner(self): test_dict = { 'a': '10', 'b': '10.0', 'c': '1,000.10', 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': u'10', 'k': u'10.0', 'l': u'1,000.10' } expected_dict = { 'a': 10, 'b': 10.0, 'c': 1000.10, 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': 10, 'k': 10.0, 'l': 1000.10 } ret_dict = self.nse.clean_server_response(test_dict) self.assertDictEqual(ret_dict, expected_dict) def test_get_stock_codes(self): sc = self.nse.get_stock_codes() self.assertIsNotNone(sc) self.assertIsInstance(sc, dict) # test the json format return sc_json = self.nse.get_stock_codes(as_json=True) self.assertIsInstance(sc_json, str) # reconstruct the dict from json and compare six.assertCountEqual(self, sc, json.loads(sc_json)) # TODO: use mock and create one test where response contains a blank line # TODO: use mock and create one test where response doesnt contain a csv # TODO: use mock and create one test where return is null # TODO: test the cache feature def test_negative_get_quote(self): wrong_code = 'inf' self.assertIsNone(self.nse.get_quote(wrong_code)) def test_get_quote(self): code = 'infy' resp = self.nse.get_quote(code) self.assertIsInstance(resp, dict) # test json response json_resp = self.nse.get_quote(code, as_json=True) self.assertIsInstance(json_resp, str) # reconstruct the original dict from json # this test may raise false alarms in case the # the price changed in that very moment. self.assertDictEqual(resp, json.loads(json_resp)) def test_is_valid_code(self): code = 'infy' self.assertTrue(self.nse.is_valid_code(code)) def test_negative_is_valid_code(self): wrong_code = 'in' self.assertFalse(self.nse.is_valid_code(wrong_code)) def test_get_top_gainers(self): res = self.nse.get_top_gainers() self.assertIsInstance(res, list) # test json response res = self.nse.get_top_gainers(as_json=True) self.assertIsInstance(res, str) def test_get_top_losers(self): res = self.nse.get_top_losers() self.assertIsInstance(res, list) def test_render_response(self): d = {'fname':'vivek', 'lname':'jha'} resp_dict = self.nse.render_response(d) resp_json = self.nse.render_response(d, as_json=True) # in case of dict, response should be a python dict self.assertIsInstance(resp_dict, dict) # in case of json, response should be a json string self.assertIsInstance(resp_json, str) # and on reconstruction it should become same as original dict self.assertDictEqual(d, json.loads(resp_json)) def test_advances_declines(self): resp = self.nse.get_advances_declines() # it should be a list of dictionaries self.assertIsInstance(resp, list) # get the json version resp_json = self.nse.get_advances_declines(as_json=True) self.assertIsInstance(resp_json, str) # load the json response and it should have same number of # elements as in case of first response self.assertEqual(len(resp), len(json.loads(resp_json))) def test_is_valid_index(self): code = 'CNX NIFTY' self.assertTrue(self.nse.is_valid_index(code)) # test with invalid string code = 'some junk stuff' self.assertFalse(self.nse.is_valid_index(code)) # test with lower case code = 'cnx nifty' self.assertTrue(self.nse.is_valid_index(code)) def test_get_index_quote(self): code = 'CNX NIFTY' self.assertIsInstance(self.nse.get_index_quote(code), dict) # with json response self.assertIsInstance(self.nse.get_index_quote(code, as_json=True), str) # with wrong code code = 'wrong code' self.assertIsNone(self.nse.get_index_quote(code)) # with lower case code code = 'cnx nifty' self.assertIsInstance(self.nse.get_index_quote(code), dict) def test_get_index_list(self): index_list = self.nse.get_index_list() index_list_json = self.nse.get_index_list(as_json=True) self.assertIsInstance(index_list, list) # test json response type self.assertIsInstance(index_list_json, str) # reconstruct list from json and match self.assertListEqual(index_list, json.loads(index_list_json)) def test_jsadptor(self): buffer = 'abc:true, def:false, ghi:NaN, jkl:none' expected_buffer = 'abc:True, def:False, ghi:"NaN", jkl:None' ret = js_adaptor(buffer) self.assertEqual(ret, expected_buffer) def test_byte_adaptor(self): if six.PY2: from StringIO import StringIO buffer = 'nsetools' fbuffer = StringIO(buffer) else: from io import BytesIO buffer = b'nsetools' fbuffer = BytesIO(buffer) ret_file_buffer = byte_adaptor(fbuffer) self.assertIsInstance(ret_file_buffer, six.StringIO)
class ManagerLib(object): _instance = None def __new__(self): if not self._instance: self._instance = super(ManagerLib, self).__new__(self) self.nse = Nse() self.userList = [] return self._instance #def __init__(self): #self.nse = Nse() #self.userList = [] def GetLiveData(self, key, id, stock): ret = self.GetUser(key, id) if isinstance(ret, UserDetails): ret = self.nse.get_quote(str(stock)) return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def CheckStockCode(self, key, id, stock): ret = self.GetUser(key, id) if isinstance(ret, UserDetails): ret = self.nse.is_valid_code(str(stock)) return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def GetListOfStocks(self, key, id): ret = self.GetUser(key, id) if isinstance(ret, UserDetails): ret = self.nse.get_stock_codes() return json.dumps(ret, ensure_ascii=False, default=lambda o: o.__dict__, sort_keys=True, indent=4) def AddUser(self, key, firebase_url, firebase_pass): firebaseauth = { 'FIREBASE_URL': firebase_url, 'FIREBASE_PWD': firebase_pass } userdetailobj = UserDetails(key, firebaseauth) self.userList.append(userdetailobj) ret = { 'message': 'User added successfully.', 'id': userdetailobj.userid } return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def GetUsers(self): return json.dumps(self.userList, default=lambda o: o.__dict__, sort_keys=True, indent=4) def GetUser(self, key, id): userobj = list(x for x in self.userList if x.userid == id) if userobj: if userobj[0].userkey == key: return userobj[0] else: return {'error': 'Id-key mismatch'} else: return {'error': 'Id not found'} def GetUserDetails(self, key, id): ret = self.GetUser(key, id) return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def AddConditionFieldList(self, key, id, indice, attrnames, attrtypes, attrvalues, conjunctions, operations): ret = self.GetUser(key, id) if isinstance(ret, UserDetails): ret = ret.SetConditionList(indice, attrnames, attrtypes, attrvalues, conjunctions, operations) return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def AddSubscibeFieldList(self, key, id, conditionid, subscribeFieldlist, triggermessage): ret = self.GetUser(key, id) if isinstance(ret, UserDetails): conditionobjlist = list(condition for condition in ret.conditionList if condition.id == conditionid) if conditionobjlist: if not any(subscription for subscription in ret.subscribeFieldList if subscription.id == conditionid): ret = ret.SetSubscibeFieldList(conditionobjlist[0].id, conditionobjlist[0].indice, subscribeFieldlist, triggermessage) else: ret = { 'error': 'Subscription for the condition already present.Remove and then try.' } else: ret = {'error': 'Condition id not found.'} return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def RemoveFromConditionList(self, key, id, conditionid): ret = self.GetUser(key, id) if isinstance(ret, UserDetails): ret = ret.RemoveFromConditionList(conditionid) return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def RemoveFromSubscriptionList(self, key, id, subscriptionid): ret = self.GetUser(key, id) if isinstance(ret, UserDetails): ret = ret.RemoveFromSubscriptionList(subscriptionid) return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def GetConditionList(self, key, userid): ret = self.GetUser(key, id) if isinstance(ret, UserDetails): ret = ret.conditionList return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def GetSubscriptionList(self, key, userid): ret = self.GetUser(key, id) if isinstance(ret, UserDetails): ret = ret.subscribeFieldList return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def RemoveUser(self, key, id): ret = self.GetUser(key, id) if isinstance(ret, UserDetails): self.userList.remove(ret) ret = {'message': 'User removed successfully'} return json.dumps(ret, default=lambda o: o.__dict__, sort_keys=True, indent=4) def GetADByStockCode(self, stocklist): AllAD = self.nse.get_advances_declines() ret = list(x for x in AllAD if x['indice'] in stocklist) return ret def GetAllADStockCode(self): AllAD = self.nse.get_advances_declines() return list(x['indice'] for x in AllAD) def CheckAttrNameOfStock(self, stock, attrname): ret = GetAllDetailsOfStock(self, stock) if attrname in ret: return True else: return False def CheckAttrTypeOfStock(self, stock, attrname, attrtype): ret = GetAllDetailsOfStock(self, stock) if attrtype == 'date': try: parse(ret[attrname]) return True except: return False elif attrtype == 'number': try: float(ret[attrname]) return True except: return False else: return True def CheckOperation(self, attrtype, operation): if attrtype == 'string' and operation != 'equals': return False else: return True def SetStockList(self, stocklist): self.stocklist = stocklist def GetStockList(self): return self.stocklist def AddStock(self, stock): self.stocklist.append(stock) def RemoveStock(self, stock): if len(self.stocklist) > 0 and stock in self.stocklist: self.stocklist.remove(stock)
[itlz, df_banklist.index[ind], df_banklist.iloc[ind][3]]) if ((float(df_banklist.iloc[ind][3]) <= 0.99) and (float(df_banklist.iloc[ind][3]) > 0.0)): itgz = "Price change are in zeros 0 " bankinzero.append( [itgz, df_banklist.index[ind], df_banklist.iloc[ind][3]]) print( "NSE Bank stocks whre {} are greater than one, {} are negative , {} are in zeros" .format(len(bankgrtz), len(banknegt), len(bankinzero))) ######### Advances Declines ##It containes the number of rising stocks, falling stocks and unchanged stocks in a given trading day, per index. adv_dec = nse.get_advances_declines() #pprint(adv_dec) ##################################### End of dat pull from NSE ############################### ## End of data pull from NSE endtime = datetime.now().strftime("%H:%M") #program data pull end time bot_endtime = datetime.now().strftime("%Y-%m-%d %H:%M:%S") print('End of data pull from NSE at time ', endtime) ###################################### Main Logic ########################################## ###### Advances Declines
class TestCoreAPIs(unittest.TestCase): def setUp(self): self.nse = Nse() def test_string_representation(self): self.assertEqual(str(self.nse) , "Driver Class for National Stock Exchange (NSE)") def test_instantiate_abs_class(self): class Exchange(AbstractBaseExchange): pass with self.assertRaises(TypeError): exc = Exchange() def test_nse_headers(self): ret = self.nse.nse_headers() self.assertIsInstance(ret, dict) def test_nse_opener(self): ''' should not raise any exception ''' opener = self.nse.nse_opener() def test_build_url_for_quote(self): test_code = 'infy' url = self.nse.build_url_for_quote(test_code) # 'test_code' should be present in the url self.assertIsNotNone(re.search(test_code, url)) def test_negative_build_url_for_quote(self): negative_codes = [1, None] with self.assertRaises(Exception): for test_code in negative_codes: url = self.nse.build_url_for_quote(test_code) def test_response_cleaner(self): test_dict = { 'a': '10', 'b': '10.0', 'c': '1,000.10', 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': u'10', 'k': u'10.0', 'l': u'1,000.10' } expected_dict = { 'a': 10, 'b': 10.0, 'c': 1000.10, 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': 10, 'k': 10.0, 'l': 1000.10 } ret_dict = self.nse.clean_server_response(test_dict) self.assertDictEqual(ret_dict, expected_dict) def test_get_stock_codes(self): sc = self.nse.get_stock_codes() self.assertIsNotNone(sc) self.assertIsInstance(sc, dict) # test the json format return sc_json = self.nse.get_stock_codes(as_json=True) self.assertIsInstance(sc_json, str) # reconstruct the dict from json and compare self.assertItemsEqual(sc, json.loads(sc_json)) # TODO: use mock and create one test where response contains a blank line # TODO: use mock and create one test where response doesnt contain a csv # TODO: use mock and create one test where return is null # TODO: test the cache feature def test_negative_get_quote(self): wrong_code = 'inf' self.assertIsNone(self.nse.get_quote(wrong_code)) def test_get_quote(self): code = 'infy' resp = self.nse.get_quote(code) self.assertIsInstance(resp, dict) # test json response json_resp = self.nse.get_quote(code, as_json=True) self.assertIsInstance(json_resp, str) # reconstruct the original dict from json # this test may raise false alarms in case the # the price changed in that very moment. self.assertDictEqual(resp, json.loads(json_resp)) def test_is_valid_code(self): code = 'infy' self.assertTrue(self.nse.is_valid_code(code)) def test_negative_is_valid_code(self): wrong_code = 'in' self.assertFalse(self.nse.is_valid_code(wrong_code)) def test_get_top_gainers(self): res = self.nse.get_top_gainers() self.assertIsInstance(res, list) # test json response res = self.nse.get_top_gainers(as_json=True) self.assertIsInstance(res, str) def test_get_top_losers(self): res = self.nse.get_top_losers() self.assertIsInstance(res, list) def test_render_response(self): d = {'fname':'vivek', 'lname':'jha'} resp_dict = self.nse.render_response(d) resp_json = self.nse.render_response(d, as_json=True) # in case of dict, response should be a python dict self.assertIsInstance(resp_dict, dict) # in case of json, response should be a json string self.assertIsInstance(resp_json, str) # and on reconstruction it should become same as original dict self.assertDictEqual(d, json.loads(resp_json)) def test_advances_declines(self): resp = self.nse.get_advances_declines() # it should be a list of dictionaries self.assertIsInstance(resp, list) # get the json version resp_json = self.nse.get_advances_declines(as_json=True) self.assertIsInstance(resp_json, str) # load the json response and it should have same number of # elements as in case of first response self.assertEqual(len(resp), len(json.loads(resp_json))) def test_is_valid_index(self): code = 'CNX NIFTY' self.assertTrue(self.nse.is_valid_index(code)) # test with invalid string code = 'some junk stuff' self.assertFalse(self.nse.is_valid_index(code)) # test with lower case code = 'cnx nifty' self.assertTrue(self.nse.is_valid_index(code)) def test_get_index_quote(self): code = 'CNX NIFTY' self.assertIsInstance(self.nse.get_index_quote(code), dict) # with json response self.assertIsInstance(self.nse.get_index_quote(code, as_json=True), str) # with wrong code code = 'wrong code' self.assertIsNone(self.nse.get_index_quote(code)) # with lower case code code = 'cnx nifty' self.assertIsInstance(self.nse.get_index_quote(code), dict) def test_get_index_list(self): index_list = self.nse.get_index_list() index_list_json = self.nse.get_index_list(as_json=True) self.assertIsInstance(index_list, list) # test json response type self.assertIsInstance(index_list_json, str) # reconstruct list from json and match self.assertListEqual(index_list, json.loads(index_list_json))
def webhook(): nse = Nse() with open('symbols.json') as json_file: symbol_data = json.load(json_file) with open('inv_symbols.json') as json_file: inv_symbol_data = json.load(json_file) newsapi = NewsApiClient(api_key='cc0446450bcc4e46a91abd02e33d5f85') req = request.get_json(silent=True, force=True) query_result = req.get('queryResult') if query_result.get('action') == 'get_stock_price': # query_result = req.get('queryResult') price_type = query_result.get('parameters').get('price_type') company_name = query_result.get('parameters').get('company_name') date_time = query_result.get('parameters').get('date-time') if isinstance(date_time, str): s = date_time.split("T")[0] unix_time = int( time.mktime( datetime.datetime.strptime(s, "%Y-%m-%d").timetuple())) else: s = date_time['date_time'].split("T")[0] unix_time = int( time.mktime( datetime.datetime.strptime(s, "%Y-%m-%d").timetuple())) start_date = unix_time end_date = unix_time + 86399 try: company_symbol = symbol_data[company_name] except: return { "fulfillmentText": "Sorry! You need to enter a NSE belonging company.", "displayText": '25', "source": "webhookdata" } q = nse.get_quote(company_symbol) prices = { "opening": q['open'], "closing": q['lastPrice'], "high": q['dayHigh'], "low": q['dayLow'] } human_readable_date = datetime.datetime.utcfromtimestamp( unix_time).strftime("%d %B, %Y") op_string = "The {} price of {} on {} was Rs.{}.".format( price_type, company_name, human_readable_date, prices[price_type]) return { "fulfillmentText": op_string, "displayText": '25', "source": "webhookdata" } elif query_result.get('action') == 'get_gainer': gainer_type = query_result.get('parameters').get('gainer_type') if query_result.get('parameters').get('number') == '': number = 1 else: number = int(query_result.get('parameters').get('number')) top_gainers = nse.get_top_gainers() top_losers = nse.get_top_losers() if gainer_type == 'gainer': if number == 1: c_name = inv_symbol_data[top_gainers[0].get('symbol')] op_string = "The top gainer for the last trading session is {}.".format( c_name) else: company_list = [] for i in range(number - 1): company_list.append( inv_symbol_data[top_gainers[i].get('symbol')]) company_string = ", ".join(company_list) company_string += " and {}".format( inv_symbol_data[top_gainers[number - 1].get('symbol')]) op_string = "The top {} {}s are {}.".format( number, gainer_type, company_string) else: if number == 1: c_name = inv_symbol_data[top_losers[0].get('symbol')] op_string = "The top loser for the last trading session is {}.".format( c_name) else: company_list = [] for i in range(number - 1): company_list.append( inv_symbol_data[top_losers[i].get('symbol')]) company_string = ", ".join(company_list) company_string += " and {}".format( inv_symbol_data[top_losers[number - 1].get('symbol')]) op_string = "The top {} {}s are {}.".format( number, gainer_type, company_string) return { "fulfillmentText": op_string, "displayText": '25', "source": "webhookdata" } elif query_result.get('action') == 'get_news': company_name = query_result.get('parameters').get('company_name') all_articles = newsapi.get_everything( qintitle=company_name, sources='bbc-news,the-verge,the-times-of-india', language='en', sort_by='relevancy') articles = all_articles.get('articles') if len(articles) == 0: return { "fulfillmentText": "Sorry! Could not find any relevant news.", "displayText": '25', "source": "webhookdata" } article = articles[random.randint(0, len(articles) - 1)] # pprint(article) title = article.get('title') url = article.get('url') url_img = article.get('urlToImage') subtitle = article.get('description') response = [{ "card": { "title": title, "subtitle": subtitle, "imageUri": url_img, "buttons": [{ "text": "Read Full Story", "postback": url }, { "text": "Get more news", "postback": "Get more news for {}".format(company_name) }] }, "platform": "FACEBOOK" }] return jsonify({"fulfillmentMessages": response}) elif query_result.get('action') == 'get_index_quote': index_code = query_result.get('parameters').get('index_codes') if index_code == "": op_string = 'Try again using a valid Index code.' else: index_quote = nse.get_index_quote(index_code).get('lastPrice') op_string = "The last updated price of {} is Rs.{}.".format( index_code.upper(), index_quote) return { "fulfillmentText": op_string, "displayText": '25', "source": "webhookdata" } elif query_result.get('action') == 'get_advances': trade_index = query_result.get('parameters').get('index_codes') advance_type = query_result.get('parameters').get('advance_type') print(trade_index) if trade_index == '': op_string = 'Try again using a valid Index code.' else: adv_dec = nse.get_advances_declines() flag = 0 for i in adv_dec: if i.get('indice') == trade_index: advances = i.get('advances') declines = i.get('declines') flag = 1 break if flag == 0: op_string = "No data of advances/declines for this index was found." else: if advance_type == 'advance': op_string = "The advances of {} are {}.".format( trade_index, advances) else: op_string = "The declines of {} are {}.".format( trade_index, declines) print(op_string) return { "fulfillmentText": op_string, "displayText": '25', "source": "webhookdata" }
class TestCoreAPIs(unittest.TestCase): def setUp(self): self.nse = Nse() def test_string_representation(self): self.assertEqual(str(self.nse), "Driver Class for National Stock Exchange (NSE)") def test_instantiate_abs_class(self): class Exchange(AbstractBaseExchange): pass with self.assertRaises(TypeError): exc = Exchange() def test_nse_headers(self): ret = self.nse.nse_headers() self.assertIsInstance(ret, dict) def test_nse_opener(self): ''' should not raise any exception ''' opener = self.nse.nse_opener() def test_build_url_for_quote(self): test_code = 'infy' url = self.nse.build_url_for_quote(test_code) # 'test_code' should be present in the url self.assertIsNotNone(re.search(test_code, url)) def test_negative_build_url_for_quote(self): negative_codes = [1, None] with self.assertRaises(Exception): for test_code in negative_codes: url = self.nse.build_url_for_quote(test_code) def test_response_cleaner(self): test_dict = { 'a': '10', 'b': '10.0', 'c': '1,000.10', 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': u'10', 'k': u'10.0', 'l': u'1,000.10' } expected_dict = { 'a': 10, 'b': 10.0, 'c': 1000.10, 'd': 'vsjha18', 'e': 10, 'f': 10.0, 'g': 1000.10, 'h': True, 'i': None, 'j': 10, 'k': 10.0, 'l': 1000.10 } ret_dict = self.nse.clean_server_response(test_dict) self.assertDictEqual(ret_dict, expected_dict) def test_get_stock_codes(self): sc = self.nse.get_stock_codes() self.assertIsNotNone(sc) self.assertIsInstance(sc, dict) # test the json format return sc_json = self.nse.get_stock_codes(as_json=True) self.assertIsInstance(sc_json, str) # reconstruct the dict from json and compare six.assertCountEqual(self, sc, json.loads(sc_json)) # TODO: use mock and create one test where response contains a blank line # TODO: use mock and create one test where response doesnt contain a csv # TODO: use mock and create one test where return is null # TODO: test the cache feature def test_negative_get_quote(self): wrong_code = 'inf' self.assertIsNone(self.nse.get_quote(wrong_code)) def test_get_quote(self): code = 'infy' resp = self.nse.get_quote(code) self.assertIsInstance(resp, dict) # test json response json_resp = self.nse.get_quote(code, as_json=True) self.assertIsInstance(json_resp, str) # reconstruct the original dict from json # this test may raise false alarms in case the # the price changed in that very moment. self.assertDictEqual(resp, json.loads(json_resp)) def test_is_valid_code(self): code = 'infy' self.assertTrue(self.nse.is_valid_code(code)) def test_negative_is_valid_code(self): wrong_code = 'in' self.assertFalse(self.nse.is_valid_code(wrong_code)) def test_get_top_gainers(self): res = self.nse.get_top_gainers() self.assertIsInstance(res, list) # test json response res = self.nse.get_top_gainers(as_json=True) self.assertIsInstance(res, str) def test_get_top_losers(self): res = self.nse.get_top_losers() self.assertIsInstance(res, list) def test_render_response(self): d = {'fname':'vivek', 'lname':'jha'} resp_dict = self.nse.render_response(d) resp_json = self.nse.render_response(d, as_json=True) # in case of dict, response should be a python dict self.assertIsInstance(resp_dict, dict) # in case of json, response should be a json string self.assertIsInstance(resp_json, str) # and on reconstruction it should become same as original dict self.assertDictEqual(d, json.loads(resp_json)) def test_advances_declines(self): resp = self.nse.get_advances_declines() # it should be a list of dictionaries self.assertIsInstance(resp, list) # get the json version resp_json = self.nse.get_advances_declines(as_json=True) self.assertIsInstance(resp_json, str) # load the json response and it should have same number of # elements as in case of first response self.assertEqual(len(resp), len(json.loads(resp_json))) def test_is_valid_index(self): code = 'NIFTY BANK' self.assertTrue(self.nse.is_valid_index(code)) # test with invalid string code = 'some junk stuff' self.assertFalse(self.nse.is_valid_index(code)) # test with lower case code = 'nifty bank' self.assertTrue(self.nse.is_valid_index(code)) def test_get_index_quote(self): code = 'NIFTY BANK' self.assertIsInstance(self.nse.get_index_quote(code), dict) # with json response self.assertIsInstance(self.nse.get_index_quote(code, as_json=True), str) # with wrong code code = 'wrong code' self.assertIsNone(self.nse.get_index_quote(code)) # with lower case code code = 'nifty bank' self.assertIsInstance(self.nse.get_index_quote(code), dict) def test_get_index_list(self): index_list = self.nse.get_index_list() index_list_json = self.nse.get_index_list(as_json=True) self.assertIsInstance(index_list, list) # test json response type self.assertIsInstance(index_list_json, str) # reconstruct list from json and match self.assertListEqual(index_list, json.loads(index_list_json)) def test_jsadptor(self): buffer = 'abc:true, def:false, ghi:NaN, jkl:none' expected_buffer = 'abc:True, def:False, ghi:"NaN", jkl:None' ret = js_adaptor(buffer) self.assertEqual(ret, expected_buffer) def test_byte_adaptor(self): if six.PY2: from StringIO import StringIO buffer = 'nsetools' fbuffer = StringIO(buffer) else: from io import BytesIO buffer = b'nsetools' fbuffer = BytesIO(buffer) ret_file_buffer = byte_adaptor(fbuffer) self.assertIsInstance(ret_file_buffer, six.StringIO) def test_nse_lot_sizes(self): data = self.nse.get_fno_lot_sizes() self.assertIsInstance(data, dict) def test_6th_Dec_1994(self): data = self.nse.download_bhavcopy('1994-12-06') self.assertIsInstance(self, data, bytes) def test_top_fno_gainers_losers(self): fno_gainer = self.nse.get_top_fno_gainers() self.assertIsInstance(fno_gainer, list) fno_gainer_json = self.nse.get_top_fno_gainers(as_json=True) self.assertIsInstance(fno_gainer_json, str) fno_loser = self.nse.get_top_fno_losers() self.assertIsInstance(fno_loser, list) fno_loser_json = self.nse.get_top_fno_losers(as_json=True) self.assertIsInstance(fno_loser_json, str) def test_statistics(self): active = self.nse.get_active_monthly() self.assertIsInstance(active, list) active_json = self.nse.get_active_monthly(as_json=True) self.assertIsInstance(active_json, str) yr_high = self.nse.get_year_high() self.assertIsInstance(yr_high, list) yr_high_json = self.nse.get_year_high(as_json=True) self.assertIsInstance(yr_high_json, str) yr_low = self.nse.get_year_low() self.assertIsInstance(yr_low, list) yr_low_json = self.nse.get_year_low(as_json=True) self.assertIsInstance(yr_low_json, str) preopen = self.nse.get_preopen_nifty() self.assertIsInstance(preopen, list) preopen_json = self.nse.get_preopen_nifty(as_json=True) self.assertIsInstance(preopen_json, str) preopen_nb = self.nse.get_preopen_niftybank() self.assertIsInstance(preopen_nb, list) preopen_nb_json = self.nse.get_preopen_niftybank(as_json=True) self.assertIsInstance(preopen_nb_json, str) preopen_fno = self.nse.get_preopen_fno() self.assertIsInstance(preopen_fno, list) preopen_fno_json = self.nse.get_preopen_fno(as_json=True) self.assertIsInstance(preopen_fno_json, str)
class TestCoreAPIs(unittest.TestCase): def setUp(self): self.nse = Nse() def test_string_representation(self): self.assertEqual(str(self.nse), "Driver Class for National Stock Exchange (NSE)") def test_instantiate_abs_class(self): class Exchange(AbstractBaseExchange): pass with self.assertRaises(TypeError): exc = Exchange() def test_nse_headers(self): ret = self.nse.nse_headers() self.assertIsInstance(ret, dict) def test_nse_opener(self): """ should not raise any exception """ opener = self.nse.nse_opener() def test_build_url_for_quote(self): test_code = "infy" url = self.nse.build_url_for_quote(test_code) # 'test_code' should be present in the url self.assertIsNotNone(re.search(test_code, url)) def test_negative_build_url_for_quote(self): negative_codes = [1, None] with self.assertRaises(Exception): for test_code in negative_codes: url = self.nse.build_url_for_quote(test_code) def test_response_cleaner(self): test_dict = { "a": "10", "b": "10.0", "c": "1,000.10", "d": "vsjha18", "e": 10, "f": 10.0, "g": 1000.10, "h": True, "i": None, "j": u"10", "k": u"10.0", "l": u"1,000.10", } expected_dict = { "a": 10, "b": 10.0, "c": 1000.10, "d": "vsjha18", "e": 10, "f": 10.0, "g": 1000.10, "h": True, "i": None, "j": 10, "k": 10.0, "l": 1000.10, } ret_dict = self.nse.clean_server_response(test_dict) self.assertDictEqual(ret_dict, expected_dict) def test_get_stock_codes(self): sc = self.nse.get_stock_codes() self.assertIsNotNone(sc) self.assertIsInstance(sc, dict) # test the json format return sc_json = self.nse.get_stock_codes(as_json=True) self.assertIsInstance(sc_json, str) # reconstruct the dict from json and compare six.assertCountEqual(self, sc, json.loads(sc_json)) # TODO: use mock and create one test where response contains a blank line # TODO: use mock and create one test where response doesnt contain a csv # TODO: use mock and create one test where return is null # TODO: test the cache feature def test_negative_get_quote(self): wrong_code = "inf" self.assertIsNone(self.nse.get_quote(wrong_code)) def test_get_quote(self): code = "infy" resp = self.nse.get_quote(code) self.assertIsInstance(resp, dict) # test json response json_resp = self.nse.get_quote(code, as_json=True) self.assertIsInstance(json_resp, str) # reconstruct the original dict from json # this test may raise false alarms in case the # the price changed in that very moment. self.assertDictEqual(resp, json.loads(json_resp)) def test_is_valid_code(self): code = "infy" self.assertTrue(self.nse.is_valid_code(code)) def test_negative_is_valid_code(self): wrong_code = "in" self.assertFalse(self.nse.is_valid_code(wrong_code)) def test_get_top_gainers(self): res = self.nse.get_top_gainers() self.assertIsInstance(res, list) # test json response res = self.nse.get_top_gainers(as_json=True) self.assertIsInstance(res, str) def test_get_top_losers(self): res = self.nse.get_top_losers() self.assertIsInstance(res, list) def test_render_response(self): d = {"fname": "vivek", "lname": "jha"} resp_dict = self.nse.render_response(d) resp_json = self.nse.render_response(d, as_json=True) # in case of dict, response should be a python dict self.assertIsInstance(resp_dict, dict) # in case of json, response should be a json string self.assertIsInstance(resp_json, str) # and on reconstruction it should become same as original dict self.assertDictEqual(d, json.loads(resp_json)) def test_advances_declines(self): resp = self.nse.get_advances_declines() # it should be a list of dictionaries self.assertIsInstance(resp, list) # get the json version resp_json = self.nse.get_advances_declines(as_json=True) self.assertIsInstance(resp_json, str) # load the json response and it should have same number of # elements as in case of first response self.assertEqual(len(resp), len(json.loads(resp_json))) def test_is_valid_index(self): code = "CNX NIFTY" self.assertTrue(self.nse.is_valid_index(code)) # test with invalid string code = "some junk stuff" self.assertFalse(self.nse.is_valid_index(code)) # test with lower case code = "cnx nifty" self.assertTrue(self.nse.is_valid_index(code)) def test_get_index_quote(self): code = "CNX NIFTY" self.assertIsInstance(self.nse.get_index_quote(code), dict) # with json response self.assertIsInstance(self.nse.get_index_quote(code, as_json=True), str) # with wrong code code = "wrong code" self.assertIsNone(self.nse.get_index_quote(code)) # with lower case code code = "cnx nifty" self.assertIsInstance(self.nse.get_index_quote(code), dict) def test_get_index_list(self): index_list = self.nse.get_index_list() index_list_json = self.nse.get_index_list(as_json=True) self.assertIsInstance(index_list, list) # test json response type self.assertIsInstance(index_list_json, str) # reconstruct list from json and match self.assertListEqual(index_list, json.loads(index_list_json)) def test_jsadptor(self): buffer = "abc:true, def:false, ghi:NaN, jkl:none" expected_buffer = 'abc:True, def:False, ghi:"NaN", jkl:None' ret = js_adaptor(buffer) self.assertEqual(ret, expected_buffer) def test_byte_adaptor(self): if six.PY2: from StringIO import StringIO buffer = "nsetools" fbuffer = StringIO(buffer) else: from io import BytesIO buffer = b"nsetools" fbuffer = BytesIO(buffer) ret_file_buffer = byte_adaptor(fbuffer) self.assertIsInstance(ret_file_buffer, six.StringIO)
cred = credentials.Certificate("./ServiceAccountKey.json") app = firebase_admin.initialize_app(cred) # Instantiate Firestore class db = firestore.client() # Instantiate batch class to update data in single batch batch = db.batch() # Set the data structure doc_ref_top_gainers = db.collection(u'nse').document(u'top_gainers') doc_ref_top_losers = db.collection(u'nse').document(u'top_losers') doc_ref_advances_declines = db.collection(u'nse').document( u'advances_declines') # write to database while True: if (current_time[0][:3].lower() in ['mon', 'tue', 'wed', 'thu', 'fri']) \ and \ (9 < int(current_time[0][:2]) < 17): batch.update(doc_ref_top_gainers, {u'top_gainers': nse.get_top_gainers()}) batch.update(doc_ref_top_losers, {u'top_losers': nse.get_top_losers()}) batch.update(doc_ref_advances_declines, {u'advances_declines': nse.get_advances_declines()}) # commit batch batch.commit() print(nse) sleep(600) current_time = ctime(time()).split()
pprint('Getting Index Quote for Infosys') pprint(nse.get_quote('infy')) pprint('Getting the entire Index List') indices = nse.get_index_list() pprint(indices) pprint('Getting Index Quote for a particular index') pprint(nse.get_index_quote(indices[1])) pprint('Getting Index Quote for all indices') for x in indices: pprint(nse.get_index_quote(x)) pprint('Getting all Stock Codes') all_stocks = nse.get_stock_codes() pprint(all_stocks) pprint('Getting Quotes for each stock') for x in all_stocks: pprint(nse.get_quote(x)) pprint('Getting Advances & Declines') pprint(nse.get_advances_declines()) pprint('Getting all Top Gainers') pprint(nse.get_top_gainers()) pprint('Getting all Top Losers') pprint(nse.get_top_losers())