Beispiel #1
0
def create_bindhosts(argvs):
    '''
    create bind hosts
    :param argvs:
    :return:
    '''
    if '-f' in argvs:
        bindhosts_file = argvs[argvs.index("-f") + 1]
    else:
        print_err(
            "invalid usage, should be:\ncreate_hosts -f <the new bindhosts file>",
            quit=True)
    source = yaml_parser(bindhosts_file)
    if source:
        for key, val in source.items():
            # print(key,val)
            host_obj = session.query(models.Host).filter(
                models.Host.hostname == val.get('hostname')).first()
            assert host_obj
            for item in val['remote_users']:
                print(item)
                assert item.get('auth_type')
                if item.get('auth_type') == 'ssh-passwd':
                    remoteuser_obj = session.query(models.RemoteUser).filter(
                        models.RemoteUser.username == item.get('username'),
                        models.RemoteUser.password == item.get(
                            'password')).first()
                else:
                    remoteuser_obj = session.query(models.RemoteUser).filter(
                        models.RemoteUser.username == item.get('username'),
                        models.RemoteUser.auth_type == item.get('auth_type'),
                    ).first()
                if not remoteuser_obj:
                    print_err("RemoteUser obj %s does not exist." % item,
                              quit=True)
                bindhost_obj = models.BindHost(host_id=host_obj.id,
                                               remoteuser_id=remoteuser_obj.id)
                session.add(bindhost_obj)
                # for groups this host binds to
                if source[key].get('groups'):
                    group_objs = session.query(models.Group).filter(
                        models.Group.name.in_(
                            source[key].get('groups'))).all()
                    assert group_objs
                    print('groups:', group_objs)
                    bindhost_obj.groups = group_objs
                # for user_profiles this host binds to
                if source[key].get('user_profiles'):
                    userprofile_objs = session.query(
                        models.UserProfile).filter(
                            models.UserProfile.username.in_(
                                source[key].get('user_profiles'))).all()
                    assert userprofile_objs
                    print("userprofiles:", userprofile_objs)
                    bindhost_obj.user_profiles = userprofile_objs
                # print(bindhost_obj)
        session.commit()
Beispiel #2
0
def create_users(argvs):
    """
    create little_finger access user
    :param argvs:
    :return:
    """
    if "-f" in argvs:
        user_file = argvs[argvs.index("-f") + 1]
    else:
        print_err("invalid usage, should be:\ncreateusers -f <the new users file>", quit=True)

    source = yaml_parser(user_file)
    if source:
        for key, val in source.items():
            print(key, val)
            obj = models.UserProfile(username=key, password=val.get("password"))
            if val.get("groups"):
                groups = session.query(models.Group).filter(models.Group.name.in_(val.get("groups"))).all()
                if not groups:
                    print_err("none of [%s] exist in group table." % val.get("groups"), quit=True)
                obj.groups = groups
            if val.get("bind_hosts"):
                bind_hosts = common_filters.bind_hosts_filter(val)
                obj.bind_hosts = bind_hosts
            # print(obj)
            session.add(obj)
        session.commit()
Beispiel #3
0
def auth():
    """
    do the user login authentication
    :return:
    """
    count = 0
    while count < 3:
        username = raw_input("\033[32;1mUsername:\033[0m").strip()
        if len(username) == 0:
            continue
        password = raw_input("\033[32;1mPassword:\033[0m").strip()
        if len(password) == 0:
            continue
        user_obj = (
            session.query(models.UserProfile)
            .filter(models.UserProfile.username == username, models.UserProfile.password == password)
            .first()
        )
        if user_obj:
            return user_obj
        else:
            print("wrong username or password, you have %s more chances." % (3 - count - 1))
            count += 1
    else:
        print_err("too many attempts.")
Beispiel #4
0
def create_users(argvs):
    '''
    create little_finger access user
    :param argvs:
    :return:
    '''
    if '-f' in argvs:
        user_file  = argvs[argvs.index("-f") +1 ]
    else:
        print_err("invalid usage, should be:\ncreateusers -f <the new users file>",quit=True)

    source = yaml_parser(user_file)
    if source:
        for key,val in source.items():
            print(key,val)
            obj = models.UserProfile(username=key,password=val.get('password'))
            if val.get('groups'):
                groups = session.query(models.Group).filter(models.Group.name.in_(val.get('groups'))).all()
                if not groups:
                    print_err("none of [%s] exist in group table." % val.get('groups'),quit=True)
                obj.groups = groups
            if val.get('bind_hosts'):
                bind_hosts = common_filters.bind_hosts_filter(val)
                obj.bind_hosts = bind_hosts
            #print(obj)
            session.add(obj)
        session.commit()
Beispiel #5
0
    def get_page(self, url, page_number):
        # print url
        html_text = self.requests_page(url)
        if not html_text:
            return
        soup = BeautifulSoup(html_text)
        product_list = soup.select("li.item")
        for index , p in enumerate(product_list):
            rank = (page_number - 1) * 96 + index + 1
            # print rank
            # print index
            # print p
            # data_url = p.select()
            # print "+++++++++++++++++++++++++++++++++++++++++++++++++++++"
            data_url = p.select("a.plp_text_link")[0].attrs.get('href')
            true_data_url = 'http://www.revolve.com/'+data_url
            print true_data_url

            find_queue = session.query(CollectionQueue).filter(CollectionQueue.site_name == 'Revolve') \
                .filter(CollectionQueue.url == true_data_url).first()
            print find_queue
            if not find_queue:
                cq = CollectionQueue()
                cq.site_name = self.site_name
                cq.add_time = int(time.time())
                cq.category = self.category
                cq.secondary_category = self.second_category
                cq.rank = rank
                cq.url = true_data_url
                # cq.img_url = img_url
                session.add(cq)
                session.commit()
                print("added....")
def user_profiles_filter(vals):
    user_profiles = session.query(models.UserProfile).filter(
        models.UserProfile.username.in_(vals.get('user_profiles'))).all()
    if not user_profiles:
        print_err("none of [%s] exist in user_profile table." %
                  vals.get('user_profiles'),
                  quit=True)
    return user_profiles
def bind_hosts_filter(vals):
    print('**>', vals.get('bind_hosts'))
    bind_hosts = session.query(models.BindHost).filter(
        models.Host.hostname.in_(vals.get('bind_hosts'))).all()
    if not bind_hosts:
        print_err("none of [%s] exist in bind_host table." %
                  vals.get('bind_hosts'),
                  quit=True)
    return bind_hosts
Beispiel #8
0
def get_queue():
    cq_list = session.query(CollectionQueue).filter(CollectionQueue.is_collected == 0).filter(CollectionQueue.goods_id == 0) \
       .filter(CollectionQueue.site_name == "Lulus").limit(20000).all()

    for cq in cq_list:
        dic = {
            'url': cq.url,
            'img_url': cq.img_url,
            'rank': cq.rank,
            'site_name': cq.site_name,
            'category': cq.category,
            'secondary_category': cq.secondary_category,
            'cq_id': cq.cq_id
        }
        # print cq
        url_list.append(dic)
        # print url_list
    session.close()
Beispiel #9
0
def get_queue():
    cq_list = session.query(CollectionQueue).filter(CollectionQueue.is_collected == 0).filter(CollectionQueue.goods_id == 0) \
       .filter(CollectionQueue.site_name == "Lulus").limit(20000).all()

    for cq in cq_list:
        dic = {
            'url': cq.url,
            'img_url': cq.img_url,
            'rank': cq.rank,
            'site_name': cq.site_name,
            'category': cq.category,
            'secondary_category': cq.secondary_category,
            'cq_id': cq.cq_id
        }
        # print cq
        url_list.append(dic)
        # print url_list
    session.close()
Beispiel #10
0
def auth():
    '''
    do the user login authentication
    :return:
    '''
    count = 0
    while count <3:
        username = raw_input("\033[32;1mUsername:\033[0m").strip()
        if len(username) ==0:continue
        password = raw_input("\033[32;1mPassword:\033[0m").strip()
        if len(password) ==0:continue
        user_obj = session.query(models.UserProfile).filter(models.UserProfile.username==username,
                                                            models.UserProfile.password==password).first()
        if user_obj:
            return user_obj
        else:
            print("wrong username or password, you have %s more chances." %(3-count-1))
            count +=1
    else:
        print_err("too many attempts.")
Beispiel #11
0
    def get_page(self, url, page_number):
        # print url
        html_text = self.requests_page(url)
        if not html_text:
            return
        soup = BeautifulSoup(html_text)
        product_list = soup.select("div.category-image")
        # print product_list
        for index, p in enumerate(product_list):
            rank = (page_number - 1) * 24 + index + 1
            # print rank
            data_price = p.select("span.price")[-1]
            data_url = p.select("a")[0].attrs.get('href')
            true_data_url = 'http://www.lulus.com' + data_url
            # print true_data_url
            data_name = p.select("img")[0].attrs.get('alt')
            # print data_name
            img_url = p.select("img")[1].attrs.get('data-src')
            # print img_url

            if not data_price or not data_name or not true_data_url:
                print("[Not Found] url:%s" % url)
                return

            find_queue = session.query(CollectionQueue).filter(CollectionQueue.site_name == 'Lulus') \
                .filter(CollectionQueue.url == true_data_url).first()
            print find_queue
            if not find_queue:
                cq = CollectionQueue()
                cq.site_name = self.site_name
                cq.add_time = int(time.time())
                cq.category = self.category
                cq.secondary_category = self.second_category
                cq.rank = rank
                cq.url = true_data_url
                cq.img_url = img_url
                session.add(cq)
                session.commit()
                print("added....")
Beispiel #12
0
    def get_page(self, url, page_number):
        # print url
        html_text = self.requests_page(url)
        if not html_text:
            return
        soup = BeautifulSoup(html_text)
        product_list = soup.select("div.category-image")
        # print product_list
        for index, p in enumerate(product_list):
            rank = (page_number - 1) * 60 + index + 1
            print rank
            data_price = p.select("span.price")[-1]
            data_url = p.select("a")[0].attrs.get('href')
            true_data_url = 'http://www.lulus.com' + data_url
            # print true_data_url
            data_name = p.select("img")[0].attrs.get('alt')
            # print data_name
            img_url = p.select("img")[1].attrs.get('data-src')
            print img_url

            if not data_price or not data_name or not true_data_url:
                print("[Not Found] url:%s" % url)
                return

            find_queue = session.query(CollectionQueue).filter(CollectionQueue.site_name == 'Lulus') \
                .filter(CollectionQueue.url == true_data_url).first()
            print find_queue
            if not find_queue:
                cq = CollectionQueue()
                cq.site_name = self.site_name
                cq.add_time = int(time.time())
                cq.category = self.category
                cq.secondary_category = self.second_category
                cq.rank = rank
                cq.url = true_data_url
                cq.img_url = img_url
                session.add(cq)
                session.commit()
                print("added....")
Beispiel #13
0
def user_profiles_filter(vals):
    user_profiles = session.query(models.UserProfile).filter(models.UserProfile.username.in_(vals.get('user_profiles'))
                                                             ).all()
    if not user_profiles:
        print_err("none of [%s] exist in user_profile table." % vals.get('user_profiles'),quit=True)
    return  user_profiles
Beispiel #14
0
        goods.brands_name = site_name
        goods.img_url = down_img_url
        goods.rank = rank
        goods.status = 1
        goods.goods_url = url_dic.get('url', '')
        goods.details_img1 = img_1
        # goods.details_img2 = img2
        # goods.details_img3 = img3
        # goods.details_img4 = img4

        session.add(goods)
        session.commit()

        filter_color = None
        if color:
            filter_color = session.query(Filter).filter(Filter.name == color) \
                .filter(Filter.fg_id == filter_group_dic.get('color')).first()
        print filter_color

        if filter_color:
            color_goods_filter = GoodsFilter()
            color_goods_filter.goods_id = goods.goods_id
            color_goods_filter.filter_id = filter_color.filter_id
            color_goods_filter.filter_name = filter_color.name
            color_goods_filter.fg_id = filter_color.fg_id
            color_goods_filter.filter_group_name = 'color'
            session.add(color_goods_filter)

        curr_time = int(time.time())
        cq_info = session.query(CollectionQueue).filter(CollectionQueue.cq_id == cq_id).first()
        if cq_info:
            cq_info.goods_id = goods.goods_id
Beispiel #15
0
    def get_list(self):
        r = s.get(self.fav_url, headers=self.headers)
        # print r.content
        # print r.headers
        # print r.url
        # print r.status_code
        res_json = r.json()
        paging = res_json.get('paging', {})
        next_url = paging.get('next', '')
        data_list = res_json.get('data', [])

        for answer in data_list:
            question = answer.get('question', {})
            title = question.get('title', '')
            id = int(answer.get('id', ''))
            url = answer.get('url', '')
            if title and id > 0 and url:
                find_queue = session.query(CollectionQueue).filter(CollectionQueue.answer_id == id).first()
                if not find_queue:
                    self.check_num += 1
                    web_url = 'http://www.zhihu.com/question/20070065/answer/%s' % id
                    cq = CollectionQueue()
                    cq.fav_id = self.fav_id
                    cq.title = title
                    cq.api_url = url
                    cq.web_url = web_url
                    cq.answer_id = id
                    cq.add_time = int(time.time())
                    session.add(cq)
                    session.commit()
                    session.close()

                    sqs_body = {
                        'api_url':url,
                        'parent_note':self.parent_note,
                        'answer_id':cq.answer_id
                    }
                    m = Message()
                    m.set_body(json.dumps(sqs_body))
                    zhihufav_sqs.write(m)

                else:
                    if find_queue.fav_id == 0:
                        find_queue.fav_id = self.fav_id
                        session.commit()
                    print("[Find Queue] %s" % url)


        if len(data_list):
            self.check_page = self.check_page - 1
            self.fav_url = next_url
            if self.force:
                print("force next url %s" % next_url)
                return self.get_list()

            elif self.check_page > 0:
                print("start next url %s" % next_url)
                return self.get_list()


        title = self.title
        num = self.check_num
        if num > 0:
            InstaPushNotify.notify(title, str(num))
Beispiel #16
0
        goods.brands_name = site_name
        goods.img_url = down_img_url
        goods.rank = rank
        goods.status = 1
        goods.goods_url = url_dic.get('url', '')
        goods.details_img1 = img_1
        # goods.details_img2 = img2
        # goods.details_img3 = img3
        # goods.details_img4 = img4

        session.add(goods)
        session.commit()

        filter_color = None
        if color:
            filter_color = session.query(Filter).filter(Filter.name == color) \
                .filter(Filter.fg_id == filter_group_dic.get('color')).first()
        print filter_color

        if filter_color:
            color_goods_filter = GoodsFilter()
            color_goods_filter.goods_id = goods.goods_id
            color_goods_filter.filter_id = filter_color.filter_id
            color_goods_filter.filter_name = filter_color.name
            color_goods_filter.fg_id = filter_color.fg_id
            color_goods_filter.filter_group_name = 'color'
            session.add(color_goods_filter)

        curr_time = int(time.time())
        cq_info = session.query(CollectionQueue).filter(
            CollectionQueue.cq_id == cq_id).first()
        if cq_info:
Beispiel #17
0
def bind_hosts_filter(vals):
    print('**>',vals.get('bind_hosts') )
    bind_hosts = session.query(models.BindHost).filter(models.Host.hostname.in_(vals.get('bind_hosts'))).all()
    if not bind_hosts:
        print_err("none of [%s] exist in bind_host table." % vals.get('bind_hosts'),quit=True)
    return bind_hosts
Beispiel #18
0
def create_bindhosts(argvs):
    """
    create bind hosts
    :param argvs:
    :return:
    """
    if "-f" in argvs:
        bindhosts_file = argvs[argvs.index("-f") + 1]
    else:
        print_err("invalid usage, should be:\ncreate_hosts -f <the new bindhosts file>", quit=True)
    source = yaml_parser(bindhosts_file)
    if source:
        for key, val in source.items():
            # print(key,val)
            host_obj = session.query(models.Host).filter(models.Host.hostname == val.get("hostname")).first()
            assert host_obj
            for item in val["remote_users"]:
                print(item)
                assert item.get("auth_type")
                if item.get("auth_type") == "ssh-passwd":
                    remoteuser_obj = (
                        session.query(models.RemoteUser)
                        .filter(
                            models.RemoteUser.username == item.get("username"),
                            models.RemoteUser.password == item.get("password"),
                        )
                        .first()
                    )
                else:
                    remoteuser_obj = (
                        session.query(models.RemoteUser)
                        .filter(
                            models.RemoteUser.username == item.get("username"),
                            models.RemoteUser.auth_type == item.get("auth_type"),
                        )
                        .first()
                    )
                if not remoteuser_obj:
                    print_err("RemoteUser obj %s does not exist." % item, quit=True)
                bindhost_obj = models.BindHost(host_id=host_obj.id, remoteuser_id=remoteuser_obj.id)
                session.add(bindhost_obj)
                # for groups this host binds to
                if source[key].get("groups"):
                    group_objs = (
                        session.query(models.Group).filter(models.Group.name.in_(source[key].get("groups"))).all()
                    )
                    assert group_objs
                    print("groups:", group_objs)
                    bindhost_obj.groups = group_objs
                # for user_profiles this host binds to
                if source[key].get("user_profiles"):
                    userprofile_objs = (
                        session.query(models.UserProfile)
                        .filter(models.UserProfile.username.in_(source[key].get("user_profiles")))
                        .all()
                    )
                    assert userprofile_objs
                    print("userprofiles:", userprofile_objs)
                    bindhost_obj.user_profiles = userprofile_objs
                # print(bindhost_obj)
        session.commit()
Beispiel #19
0

        goods = Goods()
        goods.goods_name = name
        goods.goods_price = price
        goods.add_time = int(time.time())
        goods.brands_name = site_name
        goods.rank = rank
        goods.status = 1
        goods.goods_url = url_dic.get('url', '')

        session.add(goods)
        session.commit()

        curr_time = int(time.time())
        cq_info = session.query(CollectionQueue).filter(CollectionQueue.cq_id == cq_id).first()
        if cq_info:
            cq_info.goods_id = goods.goods_id
            cq_info.is_collected = 1
            cq_info.collected_time = curr_time

        ch = CollectionHistory()
        ch.goods_id =goods.goods_id
        ch.goods_price = price
        ch.rank = rank
        ch.collected_time = curr_time
        session.add(ch)

        session.commit()
        session.close()