Commits

Anonymous committed c70d6a3 Draft

Send email notifications when user appear online

  • Participants
  • Parent commits 6010c10

Comments (0)

Files changed (1)

 
 from google.appengine.ext import db
 from google.appengine.api import taskqueue
+from google.appengine.api import mail
 
 import vkontakte
 
 USER_ID = 
 APP_ID = 
 APP_SECRET = 
+EMAIL = 
 
 def get_status(user_id=5594):
 	vk = vkontakte.API(APP_ID, APP_SECRET)
 
 date_format = '%y-%m-%d-%H'
 
+def _notify(user_id):
+	msg = ''' Пользователь в сети http://vkontakte.ru/id%d
+
+	--
+	VK-online
+	''' % user_id
+
+	mail.send_mail(EMAIL, EMAIL, 'VK-online notify', msg)
+
 class GetStatus(RequestHandler):
 	def get(self):
 		user_id = USER_ID
 		online = get_status(user_id)
 		log.info('Online: %d' , online)
 
-		status = Status(user_id=user_id, online=online)
-		status.put()
-
 		if online > 0:
-			timeParam = status.time.strftime(date_format)
+			timeParam = datetime.datetime.now().strftime(date_format)
 			try:
 				taskqueue.add(url='/task/status/hour', name='aggregate-hour-'+timeParam, params={'time': timeParam})
 			except taskqueue.TaskAlreadyExistsError:
 				pass
 
+			# if status changed
+			prev = Status.all().filter('user_id', user_id).order('-time').fetch(1)[0]
+			if prev.online == 0:
+				_notify(user_id)
+
+		status = Status(user_id=user_id, online=online)
+		status.put()
+
+
 	def post(self):
 		self.get()
 
 	return m.hexdigest()
 
 class GetStatusHour(RequestHandler):
+	""" Aggregate online status by hour """
 
 	def get(self):
 		user_id = USER_ID