当前位置: 首页>>代码示例>>Python>>正文


Python Query.update方法代码示例

本文整理汇总了Python中query.Query.update方法的典型用法代码示例。如果您正苦于以下问题:Python Query.update方法的具体用法?Python Query.update怎么用?Python Query.update使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在query.Query的用法示例。


在下文中一共展示了Query.update方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Cron

# 需要导入模块: from query import Query [as 别名]
# 或者: from query.Query import update [as 别名]
class Cron(object):

	def __init__(self, args):
		super(Cron, self).__init__()
		self.args = args
		self.query = Query(args);
		self.action = Action(args);

	def run(self):
		crons = self.query.list({}, 'AND', 'cron')
		output = []
		date = int(time.time())

		for action in crons:
			if( action['enable'] ):
				local = date - (action['tz_offset']*60*60)

				weekday = dt.utcfromtimestamp(local).weekday()
				hour = dt.utcfromtimestamp(local).hour
				minute = dt.utcfromtimestamp(local).minute

				dates = action['date'].split(',')

				for dte in dates:
					action_date = dt.utcfromtimestamp(int(dte) - (action['tz_offset']*60*60))
					cron_minute = action_date.minute
					cron_hour = action_date.hour

					if( action['repeat'] == 'hour' ):
						cron = {}

						# output.append({ str(action_date): (cron_minute < 30 and minute < 30) or (cron_minute > 29 and minute > 29) })

						if( (cron_minute < 30 and minute < 30) or (cron_minute > 29 and minute > 29) ):
							output.append(self.run_cron(action))
							cron['action'] = action

					if( action['repeat'] == 'day' ):
						cron = {}

						if( cron_hour == hour ):
							if( (cron_minute < 30 and minute < 30) or (cron_minute > 29 and minute > 29)  ):
								output.append(self.run_cron(action))
								cron['action'] = action

					if( action['repeat'] == 'week' ):
						cron = {}

						cron_weekday = action_date.weekday()

						if( weekday == cron_weekday and cron_hour == hour ):
							if( abs(cron_minute-minute) < 30 ):
								cron['message'] = self.action.send_message(action['bot_id'], action['chat_id'], action['text'], action['tz_offset'])
								cron['action'] = action

								output.append(cron)

		return output

	def run_cron(self, action):
		cron = {}
		if( len(action['cron']) > 0 and action['cron'] != 'null' ):
			# Cron contains photo or rss configuration
			cron_obj = json.loads(action['cron'])

			if 'photo' in cron_obj:
				cron['photo'] = self.action.send_photo(action['bot_id'], action['chat_id'], action['cron'], action['text'], action['tz_offset'])

				if( cron_obj['loop'] ):
					self.query.update({'cron': self.action.next_file(action['cron'], action['bot_id'])}, 'cron', action['id'])

			if 'rss' in cron_obj:
				rss_msg = self.complete_rss(cron_obj['resource'], cron_obj['rss'])
				if( len(rss_msg) > 0 ):
					cron['message'] = self.action.send_message(action['bot_id'], action['chat_id'], rss_msg, action['tz_offset'])
				else:
					cron['message'] = 'Failed to send rss'

			if 'weather' in cron_obj:
				weather = self.complete_weather(cron_obj['weather'])
				cron['weather'] = self.action.send_message(action['bot_id'], action['chat_id'], weather, action['tz_offset'])

		else:
			# Cron contains only text for message
			cron['message'] = self.action.send_message(action['bot_id'], action['chat_id'], action['text'], action['tz_offset'])

		return cron

	def complete_rss(self, resource, url):
		output = []

		if( resource == 'newsmaker.md' ):
			output.append('*{0}* - <DATE>'.format(resource))

			try:
				rss_url = urllib2.urlopen(url)
				data = rss_url.read()
				rss_url.close()

				rss = xmltodict.parse(data)
#.........这里部分代码省略.........
开发者ID:Ufrutov,项目名称:telebots,代码行数:103,代码来源:cron.py

示例2: __init__

# 需要导入模块: from query import Query [as 别名]
# 或者: from query.Query import update [as 别名]
class Action:

	def __init__(self, args):
		self.telegram = args.telegram
		self.query = Query(args);

	def get_updates(self, bot_id):
		output = {}

		bot = self.query.list({'id': bot_id}, 'AND', 'bots')

		if( len(bot) > 0 ):

			req = urllib2.Request( '{0}{1}/{2}'.format(self.telegram, bot[0]['token'], 'getUpdates') )
			request = urllib2.urlopen(req)
			
			response = json.loads(request.read())
			if( response['ok'] ):
				self.update_chats(response['result'], bot_id)
				output['response'] = response['result']
				output['chats'] = self.query.list({'bot_id': bot_id}, 'AND', 'chats')
				output['ok'] = True			
				output['updates'] = response['result']
			else:
				output['ok'] = False
		else:
			output['ok'] = False

		return output

	def update_chats(self, updates, bot_id):
		chats = {}
		chat_ids = []
		output = []

		for item in updates:
			chat = item['message']['from']['id']
			if( chat not in chat_ids ):
				chats["chat_id"] = item['message']['from']['id']
				chats["username"] = item['message']['from']['first_name']
				chats["first_name"] = item['message']['from']['first_name']
				chats["bot_id"] = bot_id
				chat_ids.append(chats)

		for chat in chat_ids:
			if( len(self.query.list({'chat_id': chat['chat_id'], 'bot_id': bot_id}, 'AND', 'chats')) <= 0 ):
				self.query.insert([chat['chat_id'], chat['username'], chat['first_name'], chat['bot_id']], 'chats')

	def get_message_atr(self, atributes):
		output = {}

		for atr in atributes:
			if( ':' in atr ):
				name = atr.split(':', 1)[0]
				value = atr.split(':', 1)[1]

				output[name] = value

		if( not output ):
			output['error'] = True
			return output
		else:
			output['error'] = False
			return output

	def send_message(self, bot_id, ids, text, tz):
		output = {}
		bot = self.query.list({'id': bot_id}, 'AND', 'bots')

		if( len(bot) > 0 ):
			bot_obj = telebot.TeleBot(str(bot[0]['token']))

			chats = ids.split(',')
			success = []

			for chat in chats:

				try:
					send = bot_obj.send_message(chat, self.convert_text(text, tz, 'request'))
					
					output['send'] = {
						'date': str(send.date),
						'message_id': str(send.message_id)
					}

					output['ok'] = True
					success.append(chat)
				except Exception as e:
					output['status'] = str(e)
					output['ok'] = False

			if( output['ok'] ):
				# message = str(MySQLdb.escape_string( self.convert_text(text, tz, 'save') ))
				message = self.convert_text(text, tz, 'save')
				output['saved'] = self.query.insert([', '.join(success), output['send']['date'], message, output['send']['message_id'], bot_id], 'message')

				output['success'] = success
				output['text'] = text

				return output
#.........这里部分代码省略.........
开发者ID:Ufrutov,项目名称:telebots,代码行数:103,代码来源:action.py


注:本文中的query.Query.update方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。