Ejemplo n.º 1
0
def collect_stock_info():
    ebest = EBest("DEMO")
    mongodb = MongoDBHandler()
    ebest.login()

    code_list = mongodb.find_item({}, "stocklab_ace", "code_info")
    target_code = set([item["단축코드"] for item in code_list])
    today = datetime.today().strftime("%Y%m%d")
    print(today)

    collect_list = mongodb.find_item({"날짜": today}, "stocklab_ace", "price_info") \
        .distinct("code")
    for col in collect_list:
        target_code.remove(col)

    for code in target_code:
        time.sleep(1)
        print("code:", code)
        """
        result_price = ebest.get_stock_price_by_code(code, "1")
        if len(result_price) > 0:
            print(result_price)
            mongodb.insert_item_many(result_price, "stocklab_ace", "price_info")
        
        
        result_credit = ebest.get_credit_trend_by_code(code, today)
        if len(result_credit) > 0:
            mongodb.insert_item_many(result_credit, "stocklab_ace", "credit_info")
        """
        result_short = ebest.get_short_trend_by_code(code,
                                                     sdate=today, edate=today)
        if len(result_short) > 0:
            mongodb.insert_item_many(result_short, "stocklab_ace", "short_info")

        result_agent = ebest.get_agent_trend_by_code(code,
                                                     fromdt=today, todt=today)
        if len(result_agent) > 0:
            mongodb.insert_item_many(result_agent, "stocklab_ace", "agent_info")
        #
        ebest.logout()
Ejemplo n.º 2
0
class MongoDBHandlerTestCase(unittest.TestCase):
    def setUp(self):
        self.mongodb = MongoDBHandler()
        self.db_name = "stocklab_ace"
        self.mongodb._client[self.db_name]["corp_info"].drop()
        docs = [{
            "item": "SamSung SDS",
            "related": "SamSung",
            "qty": 25,
            "tags": ["blank", "red"],
            "account": [14, 21]
        }, {
            "item": "LG CNS",
            "related": "LG",
            "qty": 50,
            "tags": ["red", "blank"],
            "account": [14, 21]
        }, {
            "item": "SK Telecom",
            "related": "SK",
            "qty": 100,
            "tags": ["red", "blank", "plain"],
            "account": [14, 21]
        }, {
            "item": "HYUNDAI MOBIS",
            "related": "HYUNDAI",
            "qty": 75,
            "tags": ["blank", "red"],
            "account": [22.85, 30]
        }, {
            "item": "SamSung SDI",
            "related": "SamSung",
            "qty": 25,
            "tags": ["blank", "red"],
            "account": [14, 21]
        }, {
            "item": "LG Telecom",
            "related": "LG",
            "qty": 50,
            "tags": ["red", "blank"],
            "account": [14, 21]
        }, {
            "item": "SK Innovation",
            "related": "SK",
            "qty": 50,
            "tags": ["red", "blank"],
            "account": [14, 21]
        }]
        self.mongodb._client[self.db_name]["corp_info"].insert_many(docs)

    def test_insert_item(self):
        print(inspect.stack()[0][3])
        doc = {
            "item": "SamSung Card",
            "related": "SamSung",
            "qty": 25,
            "tags": ["green", "red"],
            "account": [10, 11]
        }
        _id = self.mongodb.insert_item_one(doc, self.db_name, "corp_info")
        assert _id
        print(_id)

    def test_insert_items(self):
        print(inspect.stack()[0][3])
        docs = [{
            "item": "LG",
            "related": "LG",
            "qty": 25,
            "tags": ["red"],
            "account": [10, 11]
        }, {
            "item": "LG 화학",
            "related": "LG",
            "qty": 25,
            "tags": ["green", "red"],
            "account": [10, 11]
        }]
        ids = self.mongodb.insert_item_many(docs, self.db_name, "corp_info")
        assert ids
        print(ids)

    def test_find_item(self):
        print(inspect.stack()[0][3])
        doc = self.mongodb.find_item_one({"related": "LG"}, self.db_name,
                                         "corp_info")
        pprint(doc)

    def test_find_items(self):
        print(inspect.stack()[0][3])
        cursor = self.mongodb.find_item({"tags.1": "red"}, self.db_name,
                                        "corp_info")
        assert cursor
        for doc in cursor:
            pprint(doc)

    def test_delete_items(self):
        print(inspect.stack()[0][3])
        result = self.mongodb.delete_item_many({"related": "SamSung"},
                                               self.db_name, "corp_info")
        assert result
        print(result.deleted_count)

    def test_update_items(self):
        print(inspect.stack()[0][3])
        result = self.mongodb.update_item_many({"item": "LG Telecom"},
                                               {"$set": {
                                                   "qty": 300
                                               }}, self.db_name, "corp_info")
        assert result
        print("matched_count:" + str(result.matched_count))
        print("modified_count:" + str(result.modified_count))

    def test_aggregate(self):
        print(inspect.stack()[0][3])
        pipeline = [{
            "$match": {
                "tags.1": "red"
            }
        }, {
            "$group": {
                "_id": "$related",
                "sum_val": {
                    "$sum": "$qty"
                }
            }
        }]
        result = self.mongodb.aggregate(pipeline, self.db_name, "corp_info")
        assert result
        for item in result:
            pprint(item)

    def test_text_search(self):
        print(inspect.stack()[0][3])
        index_result = self.mongodb._client[
            self.db_name]["corp_info"].create_index([('item', 'text'),
                                                     ('related', 'text'),
                                                     ('tags', 'text')])
        print(index_result)
        result = self.mongodb.text_search("blank", self.db_name, "corp_info")
        assert result
        for item in result:
            pprint(item)

    def tearDown(self):
        pass