def search_share(keyword): response = submit_request( 'http://www.tsetmc.com/tsev2/data/search.aspx', params=(('skey', keyword), ), headers=get_headers(None, 'http://www.tsetmc.com/Loader.aspx?ParTree=15'), timeout=25) if len(response.text) == 0: return lables = [ 'ticker', 'description', 'id', '', '', '', 'bazaar type', 'enable', 'bazaar', 'bazaar' ] df = pd.read_csv(StringIO(response.text), sep=',', lineterminator=';', header=None) df = df.where((pd.notnull(df)), None) new_list, update_list = [], [] for index, row in df.iterrows(): id = row[2] try: share = Share.objects.get(id=id) except Share.DoesNotExist: for share in new_list: if share.id == row[2]: break else: share = Share() (update_list if share.id else new_list).append(share) share.ticker = characters.ar_to_fa(str(row[0])).strip() share.description = characters.ar_to_fa(row[1]).strip() share.id = row[2] share.bazaar_type = row[6] share.enable = bool(row[7]) share.strike_date, share.option_strike_price, share.base_share = share.parse_data( ) Share.objects.bulk_create(new_list, batch_size=100) Share.objects.bulk_update(update_list, [ 'ticker', 'description', 'bazaar_type', 'enable', 'option_strike_price', 'strike_date', 'base_share' ], batch_size=100) if new_list: logger.info( f"update share list, {len(new_list)} added ({new_list}), {len(update_list)} updated." )
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." )