Exemplo n.º 1
0
def get_key_uin(account_biz):
    key_uin = _get_key_uin(account_biz)
    if not key_uin:
        raise NoneKeyUinError("NoneKeyUinError")
    print("get_key_uin: ", key_uin)
    key_uin_dict = json.loads(key_uin, encoding="utf-8")
    if not key_uin_dict.get("key", None) or not key_uin_dict.get("uin", None):
        raise NoneKeyUinError("NoneKeyUinError")
    return key_uin_dict
Exemplo n.º 2
0
def check_key_uin(account_biz):
    try:
        key_uin_dict = get_key_uin(account_biz)
        get_history_api(**{
            "key": key_uin_dict.get("key", ""),
            "uin": key_uin_dict.get("uin", ""),
            "biz": account_biz,
            "offset": 0,
        })
    except KeyExpireError:
        delete_key_uin(account_biz)
        raise NoneKeyUinError(f"key: 已过期")
Exemplo n.º 3
0
    def account_run(self, account_id):
        account = models.Account.query.get(account_id)
        account_biz = account.account_biz
        account_offset = account.offset
        key_uin_dict = get_key_uin(account_biz)
        offset = 0
        one_add = False
        while 1:
            if not self.check_account_status(account_id, 2):
                break
            s_time = time.time()
            try:
                histories = get_history_api(
                    **{
                        "key": key_uin_dict.get("key", ""),
                        "uin": key_uin_dict.get("uin", ""),
                        "biz": account_biz,
                        "offset": offset,
                    })
                ending = histories['ending']
                next_offset = histories["next_offset"]
                print(
                    f"biz: {account_biz} offset: {offset} next_offset: {next_offset}"
                )
                article_items = histories["results"]["article_infos"]
                new_article = False
                for article_item in article_items:
                    # print(article_item)
                    if not article_item["article_content_url"]:
                        continue
                    if new_article:
                        self.save_article(account_id, article_item)
                    else:
                        new_article = self.save_article(
                            account_id, article_item)

                account.counts = models.Article.query.filter_by(
                    account_id=account.id).count()
                if account_offset == 0:
                    account.offset = offset
                    offset = next_offset
                elif new_article:
                    if one_add:
                        account.offset = offset
                    offset = next_offset
                else:
                    print(
                        f"biz: {account_biz} 当前offset: {offset}文章都存在 next_offset: {next_offset}"
                    )
                    if one_add or account_offset == 0:
                        account.offset = offset
                        offset = next_offset
                    else:
                        offset += account_offset
                        account.offset = offset
                        one_add = True
                if ending:
                    account.offset = offset
                    account.end = True
                self.update_obj(account)
                if ending:
                    break
            except KeyExpireError:
                delete_key_uin(account_biz)
                raise NoneKeyUinError(f"key: 已过期 offset: {offset}")
            # 控制访问频次,以免被禁
            while time.time() - s_time < SLEEP_TIME:
                time.sleep(0.1)
Exemplo n.º 4
0
def get_key_uin(account_biz):
    key_uin = _get_key_uin(account_biz)
    if not key_uin:
        raise NoneKeyUinError("NoneKeyUinError")
    return json.loads(key_uin, encoding="utf-8")