Beispiel #1
0
def get_product(self, user):

    product_list = {}
    products = []

    searches = nltk.Nltk.generate_searches(
        Post.query.filter_by(user_id=user.id))

    print('# of searches')
    print(len(searches))
    print('post searches')
    for i in range(3):
        search = random.choice(searches)
        print(i, search)
        try:
            ps = amazon.search(Keywords=search,
                               SearchIndex=AMAZON_CATEGORIES[0])
            for p in ps:
                product_list[p.title] = p
        except Exception as e:
            print('Exception')
            print(e)
            continue

    for p in product_list.keys():
        print(p.title)
        products.append(product_list[p])

    print('Total # of products')
    print(len(products))

    random.shuffle(products)
    products = products[:100]

    print('Sliced # of products')
    print(len(products))

    for index, product in enumerate(products, start=1):  # default is zero
        product_endity = ProductActions.create(product.asin)
        redis.set(product.asin, product.to_string())

        # check category
        product_category = product.get_attribute('ProductGroup')
        category = CategoryActions.find_by_name(product_category)

        if category is None:
            category = CategoryActions.create(product_category)
            UserActions.add_category(user, category)

        UserProductActions.create(user, product_endity, category)
        self.update_state(state='PROGRESS',
                          meta={
                              'current': index,
                              'total': 100,
                              'user_id': user.id
                          })

    return {'user_id': user.id, 'status': 'Task completed!'}
Beispiel #2
0
def get_product(self, user):

    product_list = {}
    products = []

    searches = nltk.Nltk.generate_searches(Post.query.filter_by(user_id=user.id))

    print('# of searches')
    print(len(searches))
    print('post searches')
    for i in range(3):
        search = random.choice(searches)
        print(i, search)
        try:
            ps = amazon.search(Keywords=search, SearchIndex=AMAZON_CATEGORIES[0])
            for p in ps:
                product_list[p.title] = p
        except Exception as e:
            print('Exception')
            print(e)
            continue

    for p in product_list.keys():
        print(p.title)
        products.append(product_list[p])

    print('Total # of products')
    print(len(products))

    random.shuffle(products)
    products = products[:100]

    print('Sliced # of products')
    print(len(products))

    for index, product in enumerate(products, start=1):   # default is zero
        product_endity = ProductActions.create(product.asin)
        redis.set(product.asin, product.to_string())

        # check category
        product_category = product.get_attribute('ProductGroup')
        category = CategoryActions.find_by_name(product_category)

        if category is None:
            category = CategoryActions.create(product_category)
            UserActions.add_category(user, category)

        UserProductActions.create(user, product_endity, category)
        self.update_state(state='PROGRESS',
                          meta={
                              'current': index,
                              'total': 100,
                              'user_id': user.id
                          })

    return {'user_id': user.id, 'status': 'Task completed!'}
Beispiel #3
0
    def setUp(self):
        app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://*****:*****@localhost/giftsmarts_test"
        db.session.close()
        db.drop_all()
        db.create_all()

        FriendRelationShipTypeActions.create("relationship 1")
        FriendRelationShipTypeActions.create("relationship 2")
        FriendRelationShipTypeActions.create("relationship 3")

        self.profile_1 = ['118600698523151', 'Luke Skywalker Alaaaiffajfch Occhinosky', '', '', '1980-01-30']
        profile_2 = {'id': '118600698523152', 'name': 'Han Solo Alaaaiffajfch Occhinosky', 'birthday': '01/30/1979'}
        profile_3 = {'id': '118600698523153', 'name': 'Padme  Alaaaiffajfch Occhinosky', 'birthday': '01/30/1979'}

        self.user_1 = UserActions.create_user_from_csv(self.profile_1)
        user_2 = UserActions.new_facebook_user(profile_2, {'access_token': 'mock access token'})
        user_3 = UserActions.new_facebook_user(profile_3, {'access_token': 'mock access token'})
        FriendRelationshipActions.create(self.user_1, user_2)
        FriendRelationshipActions.create(self.user_1, user_3)

        product_1 = ProductActions.create("1")
        product_2 = ProductActions.create("2")
        category = CategoryActions.create("Book")
        UserProductActions.create(self.user_1, product_1, category)
        UserProductActions.create(self.user_1, product_2, category)
    def setUp(self):
        app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://*****:*****@localhost/giftsmarts_test"
        db.session.close()
        db.drop_all()
        db.create_all()

        self.profile_1 = ['118600698523151', 'Luke Skywalker Alaaaiffajfch Occhinosky', '', '', '1980-01-30']
        profile_2 = {'id': '118600698523152', 'name': 'Han Solo Alaaaiffajfch Occhinosky', 'birthday': '01/30/1979'}
        profile_3 = {'id': '118600698523153', 'name': 'Padme  Alaaaiffajfch Occhinosky', 'birthday': '01/30/1979'}

        family_rel_name = "family"
        friend_rel_name = "friend"

        FriendRelationShipTypeActions.create(family_rel_name)
        FriendRelationShipTypeActions.create(friend_rel_name)
        FriendRelationShipTypeActions.create("acquaintance")

        rel_family = FriendRelationShipTypeActions.find_by_name(family_rel_name)
        rel_friend = FriendRelationShipTypeActions.find_by_name(friend_rel_name)

        self.user_1 = UserActions.create_user_from_csv(self.profile_1)
        user_2 = UserActions.new_facebook_user(profile_2, {'access_token': 'mock access token'})
        user_3 = UserActions.new_facebook_user(profile_3, {'access_token': 'mock access token'})
        FriendRelationshipActions.create(self.user_1, user_2, rel_family)
        FriendRelationshipActions.create(self.user_1, user_3, rel_friend)
        product_1 = ProductActions.create("1")
        product_2 = ProductActions.create("2")
        category = CategoryActions.create("Book")
        UserProductActions.create(self.user_1, product_1, category)
        UserProductActions.create(self.user_1, product_2, category)
Beispiel #5
0
    def run(self):
        print("Drop & Create database")
        db.reflect()
        db.drop_all()
        db.create_all()

        # Load fixtures
        print("Load Relationships type")
        FriendRelationShipTypeActions.create("family")
        FriendRelationShipTypeActions.create("close friend")
        FriendRelationShipTypeActions.create("acquaintance")
        FriendRelationShipTypeActions.create("archived")

        print("Load category")
        for category in AMAZON_CATEGORIES:
            CategoryActions.create(category)

        pass
Beispiel #6
0
    def new_facebook_user(cls, profile, result):
        birthday = datetime.datetime.strptime(profile['birthday'],
                                              '%m/%d/%Y').date()
        new_user = cls.model(id=profile['id'],
                             name=profile['name'],
                             profile_url="",
                             birthday=birthday,
                             access_token=result['access_token'])

        # Add all category by default
        categories = CategoryActions.find_all()
        for category in categories:
            new_user.categories.append(category)

        db.session.add(new_user)
        db.session.commit()
        return new_user
Beispiel #7
0
    def create_user_from_csv(cls, row):
        birthday = datetime.datetime.strptime(row[4], '%Y-%m-%d').date()

        new_user = cls.model(id=row[0],
                             name=row[1],
                             profile_url=row[2],
                             access_token=row[3],
                             birthday=birthday)

        # Add all category by default
        categories = CategoryActions.find_all()
        for category in categories:
            new_user.categories.append(category)

        db.session.add(new_user)
        db.session.add(new_user)
        db.session.commit()
        return new_user
Beispiel #8
0
    def new(cls, user):
        if db.session.query(
                exists().where(User.id == user['id'])).scalar() is not True:
            birthday = datetime.datetime.strptime(user['birthday'],
                                                  '%m/%d/%Y').date()
            new_user = cls.model(id=user['id'],
                                 name=user['name'],
                                 profile_url="",
                                 birthday=birthday,
                                 access_token="")

            # Add all category by default
            categories = CategoryActions.find_all()
            for category in categories:
                new_user.categories.append(category)

            db.session.add(new_user)
            db.session.commit()

            return new_user
        else:
            return UserActions.find_by_id(user['id'])
    def test_user_product(self):
        products = UserProductActions.find_by_user(self.user_1)
        product_1 = ProductActions.create("1")
        product_2 = ProductActions.create("2")
        category = CategoryActions.create("Book")
        UserProductActions.create(self.user_1, product_1, category)
        UserProductActions.create(self.user_1, product_2, category)

        products = UserProductActions.find_by_user(self.user_1)

        user_product_schema = UserProductSchema(many=True)
        user_product_result = user_product_schema.dump(products)
        user_product_json = json.dumps(user_product_result.data)

        expected = '[{"user_id": "118600698523151", "product_id": "1", "category_id": 1, "product": {"id": 1}, ' \
                   '"user": {"id": 118600698523151, "name": "Luke Skywalker Alaaaiffajfch Occhinosky",' \
                   ' "profile_url": "", "birthday": "1980-01-30"}}, ' \
                   '{"user_id": "118600698523151", "product_id": "2", "category_id": 1, "product": {"id": 2}, ' \
                   '"user": {"id": 118600698523151, "name": "Luke Skywalker Alaaaiffajfch Occhinosky", ' \
                   '"profile_url": "", "birthday": "1980-01-30"}}]'

        self.assertEqual(expected, user_product_json)
        pass