def update_share_list(batch_size=100): text = get_watch_list() df = pd.read_csv(StringIO(text.split("@")[2]), sep=',', lineterminator=';', header=None) df = df.where((pd.notnull(df)), None) new_list, update_list = [], [] for index, row in df.iterrows(): try: share = Share.objects.get(id=row[0]) except Share.DoesNotExist: share = Share() (update_list if share.id else new_list).append(share) share.enable = True share.id = row[0] share.ticker = characters.ar_to_fa(str(row[2])).strip() share.description = characters.ar_to_fa(row[3]).strip() share.eps = row[14] share.base_volume = row[15] share.bazaar_type = row[17] share.group = ShareGroup.objects.get(id=row[18]) share.total_count = row[21] share.bazaar_group = row[22] share.strike_date, share.option_strike_price, share.base_share = share.parse_data( ) Share.objects.bulk_create(new_list, batch_size=batch_size) Share.objects.bulk_update(update_list, [ 'enable', 'ticker', 'description', 'eps', 'base_volume', 'bazaar_type', 'group', 'total_count', 'bazaar_group', 'option_strike_price', 'strike_date', 'base_share' ], batch_size=100) logger.info( f"update share list, {len(new_list)} ({new_list}) added, {len(update_list)} updated." )
def update_stock_list(batch_size=100): headers = HEADERS.copy() headers['Referer'] = 'http://www.tsetmc.com/Loader.aspx?ParTree=15131F' params = ( ('h', '0'), ('r', '0'), ) response = requests.get( 'http://www.tsetmc.com/tsev2/data/MarketWatchInit.aspx', headers=headers, params=params) if response.status_code != 200: raise Exception("Http Error: {}".format(response.status_code)) ''' separated with @ text part 0: ? part 1: general info of bazaar ['date and time of last_transaction', 'boorse_status', 'boorse_index', 'boorse_index_diff', 'boorse_market cap', 'boorse_volume', 'boorse_value', 'boorse_count', 'faraboorse_status', 'faraboorse_volume', 'faraboorse_value', 'faraboorse_count', 'moshtaghe_status', 'moshtaghe_volume', 'moshtaghe_value', 'moshtaghe_count'] part 2: ['id', 'IR', 'ticker', 'description', '?', 'first', 'tomorrow', 'last', 'count', 'volume', 'value', 'low', 'high', 'yesterday', 'eps', 'base volume', '', 'bazaar type', 'group', 'max_price_possible', 'min_price_possible', 'number of stock', 'bazaar group'] part 3; ['id', 'order', 'sell_count', 'buy_count', 'buy_price', 'sell_price', 'buy_volume', 'sell_volume'] part 4: ? ''' df = pd.read_csv(StringIO(response.text.split("@")[2]), sep=',', lineterminator=';', header=None) df = df.where((pd.notnull(df)), None) new_list, update_list = [], [] for index, row in df.iterrows(): try: share = Share.objects.get(id=row[0]) except Share.DoesNotExist: share = Share() (update_list if share.id else new_list).append(share) share.enable = True share.id = row[0] share.ticker = characters.ar_to_fa(str(row[2])) share.description = characters.ar_to_fa(row[3]) share.eps = row[14] share.base_volume = row[15] share.bazaar_type = row[17] share.group = row[18] share.total_count = row[21] share.bazaar_group = row[22] if new_list: logger.info("new stocks: {}".format(new_list)) Share.objects.bulk_create(new_list, batch_size=100) Share.objects.bulk_update(update_list, [ 'enable', 'ticker', 'description', 'eps', 'base_volume', 'bazaar_type', 'group', 'total_count', 'bazaar_group' ], batch_size=100) logger.info("update stock list, {} added, {} updated.".format( len(new_list), len(update_list)))