Esempio n. 1
0
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."
        )
Esempio n. 2
0
def update_share_groups():
    response = requests.get(
        'http://www.tsetmc.com/Loader.aspx?ParTree=111C1213')

    if response.status_code != 200:
        raise Exception(f"Http Error: {response.status_code}")

    for group_data in BeautifulSoup(
            response.text, features='html.parser').body.select('tr[id]'):
        id = group_data.select('td')[0].contents[0].strip()
        name = group_data.select('td')[1].contents[0].strip()

        if not id.isdigit():
            continue

        try:
            group = ShareGroup.objects.get(id=id)
        except ShareGroup.DoesNotExist:
            group = ShareGroup()

        group.id = int(id)
        group.name = characters.ar_to_fa(name)
        group.save()

    logger.info(
        f"Share group info updated. number of groups: {ShareGroup.objects.count()}"
    )
Esempio n. 3
0
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 test_ar_to_fa(self):
        self.assertEqual(characters.ar_to_fa("السلام عليكم"), "السلام علیکم")
        self.assertEqual(characters.ar_to_fa("HI ي"), "HI ی")
        self.assertEqual(characters.ar_to_fa("دِ بِ زِ ذِ شِ سِ ى ي ك"),
                         "د ب ز ذ ش س ی ی ک")
        self.assertEqual(characters.ar_to_fa("دِ بِ زِ ذِ شِ سِ ى ي ك"),
                         "د ب ز ذ ش س ی ی ک")
        self.assertEqual(
            characters.ar_to_fa(
                "ظ ط ذ د ز ر و ، . ش س ي ب ل ا ت ن م ك ض ص ث ق ف غ ع ه خ ح ؟"),
            "ظ ط ذ د ز ر و ، . ش س ی ب ل ا ت ن م ک ض ص ث ق ف غ ع ه خ ح ؟",
        )

        with pytest.raises(TypeError):
            characters.ar_to_fa(12345)

        orig = "السلام عليكم ٠١٢٣٤٥٦٧٨٩"
        converted = characters.ar_to_fa(orig)
        converted = digits.ar_to_fa(converted)
        self.assertEqual(converted, "السلام علیکم ۰۱۲۳۴۵۶۷۸۹")
Esempio n. 5
0
 def test_int_arg_fa_to_ar(self):
     characters.ar_to_fa(12345)
Esempio n. 6
0
 def test_special_ar_chars_to_fa(self):
     self.assertEqual(
         characters.ar_to_fa("ظ ط ذ د ز ر و ، . ش س ي ب ل ا ت ن م ك ض ص ث ق ف غ ع ه خ ح ؟"),
         "ظ ط ذ د ز ر و ، . ش س ی ب ل ا ت ن م ک ض ص ث ق ف غ ع ه خ ح ؟")
Esempio n. 7
0
    def test_long_ar_to_fa(self):
        orig = "السلام عليكم ٠١٢٣٤٥٦٧٨٩"
        converted = characters.ar_to_fa(orig)
        converted = digits.ar_to_fa(converted)

        self.assertEqual(converted, "السلام علیکم ۰۱۲۳۴۵۶۷۸۹")
Esempio n. 8
0
 def test_fa_to_fa(self):
     self.assertEqual(characters.ar_to_fa("السلام علیکم"), "السلام علیکم")
Esempio n. 9
0
 def test_ar_to_fa(self):
     self.assertEqual(characters.ar_to_fa("السلام عليكم"), "السلام علیکم")
     self.assertEqual(characters.ar_to_fa("HI ي"), "HI ی")
Esempio n. 10
0
def character(values):
    return characters.ar_to_fa(values)
Esempio n. 11
0
    def test_fa_to_ar(self):
        self.assertEqual(characters.fa_to_ar("کیک"), "كيك")

        with pytest.raises(TypeError):
            characters.ar_to_fa(12345)
Esempio n. 12
0
 def test_fa_to_fa(self):
     self.assertEqual(characters.ar_to_fa("السلام علیکم"), "السلام علیکم")
     self.assertEqual(characters.ar_to_fa("السلام علیکم"), "السلام علیکم")
Esempio n. 13
0
def search_stock(keyword):
    try:
        headers = {
            'User-Agent':
            'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0',
            'Accept': '*/*',
            'Accept-Language': 'en-US,en;q=0.5',
            'X-Requested-With': 'XMLHttpRequest',
            'DNT': '1',
            'Connection': 'keep-alive',
            'Referer': 'http://www.tsetmc.com/Loader.aspx?ParTree=15',
        }

        response = requests.get(
            'http://www.tsetmc.com/tsev2/data/search.aspx?skey={}'.format(
                keyword),
            headers=headers)

        if response.status_code != 200:
            raise Exception("Http Error: {}".format(response.status_code))

        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:
                share = Share()

            (update_list if share.id else new_list).append(share)

            share.ticker = characters.ar_to_fa(str(row[0]))
            share.description = characters.ar_to_fa(row[1])
            share.id = row[2]
            share.bazaar_type = row[6]
            share.enable = row[7]

        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, ['ticker', 'description', 'bazaar_type', 'enable'],
            batch_size=100)
        if new_list:
            logger.info("update stock list, {} added, {} updated.".format(
                len(new_list), len(update_list)))
    except Exception as e:
        logger.exception(e)
        raise e
Esempio n. 14
0
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)))