def publish_fund_image_async(): ''' 向所有的bot发送基金曲线 通过库中查询后提交 因为基金图片要请求网络,所以通过基金来遍历 ''' res = db.select_data( 'select f.id, f.user_id, u.name, u.bot_id, u.chat_id from funds f, users u where f.user_id = u.id;' ) ''' 构造bot字典:{ bot_id-chat_id : Bot对象 } ''' funds = generate_fund_userlist(res) bots = dict() tasks = [] print(funds) for fund_id, user_list in funds.items(): f = Fund(fund_id) for user in user_list: ''' 查找bots字典 ''' bid, cid = user.bot_id, user.chat_id bot_key = '%s-%s' % (bid, cid) bot = bots.get(bot_key) if bot == None: bot = bots[bot_key] = services.get_bot(bid, cid) tasks.append(services.async_send_image(bot, f)) group = asyncio.gather(*tasks, loop=loop) res = loop.run_until_complete(group) return str(res)
def publish_fund_image(): ''' 向所有的bot发送基金曲线 通过库中查询后提交 因为基金图片要请求网络,所以通过基金来遍历 ''' res = db.select_data( 'select f.id, f.user_id, u.name, u.bot_id, u.chat_id from funds f, users u where f.user_id = u.id;' ) ''' 构造bot字典:{ bot_id-chat_id : Bot对象 } ''' funds = generate_fund_userlist(res) bots = dict() print(funds) for fund_id, user_list in funds.items(): f = Fund(fund_id) for user in user_list: ''' 查找bots字典 ''' bid, cid = user.bot_id, user.chat_id bot_key = '%s-%s' % (bid, cid) bot = bots.get(bot_key) if bot == None: bot = bots[bot_key] = services.get_bot(bid, cid) services.send_fund_image(bot, f) return '成功'
def check_chollos(): logging.debug("Checking chollos") result = [] try: for userConfiguration in repository.get_dbc().get_table('UserConfiguration').all(): user_id = userConfiguration['user_id'] chollos = get_user_chollos(user_id) repository.get_dbc().insert_user_sent_chollos(user_id) sent_chollos = [] for chollo in chollos: if chollo.link: sent_chollos.append(chollo.link) if chollo.link and chollo.link not in repository.get_dbc().get_user_sent_chollos(user_id): result.append(chollo) # repository.get_dbc().add_user_sent_chollo(chollo.link, user_id) notify_new_chollo(get_bot(), user_id, chollo) # print(chollo.titulo+' - '+chollo.comercio) repository.get_dbc().replace_user_sent_chollos(sent_chollos, user_id) except Exception as e: if(str(e) == 'Forbidden: bot was blocked by the user' or str(e) == 'Chat not found'): repository.get_dbc().remove_user(user_id) else: logging.error("Failed checking chollos") logging.error("User ID: "+str(user_id)) logging.error(str(e)) repository.get_dbc().replace_user_sent_chollos(sent_chollos, user_id) # get_bot().send_message(chat_id=user_id, parse_mode="Markdown", text="Something go really bad. You couldn't be notify of news chollos") return result
def async_publish_fund_image_by_name(name): user = None try: user = next( db.select_data('select * from users where name = "%s"' % (name))) except StopIteration: return '没有用户名为%s的用户' % (name) else: if user == None: return '没有用户名为%s的用户' % (name) funds = db.select_data('select * from funds where user_id = %d;' % (user.get('id'))) bot = services.get_bot(user.get('bot_id'), user.get('chat_id')) tasks = [services.async_send_image(bot, Fund(f.get('id'))) for f in funds] group = asyncio.gather(*tasks, loop=loop) res = loop.run_until_complete(group) return str(res)
def publish_fund_image_by_name(name): user = None try: user = next( db.select_data('select * from users where name = "%s"' % (name))) except StopIteration: return '没有用户名为%s的用户' % (name) else: if user == None: return '没有用户名为%s的用户' % (name) funds = db.select_data('select * from funds where user_id = %d;' % (user.get('id'))) bot = services.get_bot(user.get('bot_id'), user.get('chat_id')) for fund in funds: f = Fund(fund.get('id')) res = services.send_fund_image(bot, f) return res.text
def publish_user_funds(user_id): user = None try: user = next( db.select_data('select * from users where id = %d' % (user_id))) except StopIteration: return {'status': 'error', 'message': '没有id为%d的用户' % (user_id)} else: if user == None: return {'status': 'error', 'message': '没有该用户' % (user_id)} funds = db.select_data('select * from funds where user_id = %d;' % (user_id)) bot = services.get_bot(user.get('bot_id'), user.get('chat_id')) for fund in funds: f = Fund(fund.get('id')) res = services.send_fund_image(bot, f) return res.text
def check_chollos_first_time(user_id): logging.debug("Checking chollos") result = [] try: user_id = user_id chollos = get_user_chollos(user_id) if user_id not in old_chollos: old_chollos[user_id] = [] for chollo in chollos: if chollo.link not in old_chollos[user_id]: result.append(chollo) old_chollos[user_id].append(chollo.link) notify_new_chollo(get_bot(), user_id, chollo) # print(chollo.titulo+' - '+chollo.comercio) except Exception as e: logging.error("Failed checking chollos") logging.error(e) # get_bot().send_message(chat_id=user_id, parse_mode="Markdown", text="Something go really bad. You couldn't be notify of news chollos") return result
def check_chollos_first_time(user_id): logging.debug("Checking chollos") result = [] try: user_id = user_id chollos = get_user_chollos(user_id) repository.get_dbc().insert_user_sent_chollos(user_id) for chollo in chollos: if chollo.link not in repository.get_dbc().get_user_sent_chollos(user_id): result.append(chollo) repository.get_dbc().add_user_sent_chollo(chollo.link, user_id) notify_new_chollo(get_bot(), user_id, chollo) # print(chollo.titulo+' - '+chollo.comercio) except Exception as e: logging.error("Failed checking chollos") logging.error(e) logging.error("User ID: "+str(user_id)) # get_bot().send_message(chat_id=user_id, parse_mode="Markdown", text="Something go really bad. You couldn't be notify of news chollos") return result