def do_task_post_curry(username): user = CurryUser.get_by_key_name(username) display_string = effects.display_effect(user) # post the tweet try: status = api.PostUpdate( u'@%s の カレーが できあがった! 今日の材料は %s。%s %s' % ( username, user.curry_material, display_string, user.state_string ) ) user.curry_count = 0 user.curry_material = "" #TODO: move to instance method user.spicy = 1.0 user.price = 0 user.kal = 0 user.color = [128, 128, 0] user.state_string = "" user.put() logging.debug("posted '%s'" % status.GetText()) except twitter.TwitterError, e: logging.debug('duplicated user %s with material %s', (username, material_list))
def do_fetch_and_post(): user = (CurryUser.all().order('last_fetch').fetch(limit=1))[0] if user.key().name() == config.MY_NAME: # skip bot itself user.last_fetch = datetime.now() user.put() return 'ok' logging.info("fetching for user '%s'" % user.key().name()) taskqueue.add( queue_name='fetch-queue', url='/task/fetch_material/%s/0' % user.key().name(), ) user.last_fetch = datetime.now() user.put() return 'ok'
def do_fetch_and_post(): user = (CurryUser.all() .order('last_fetch') .fetch(limit=1) )[0] if user.key().name() == config.MY_NAME: # skip bot itself user.last_fetch = datetime.now() user.put() return 'ok' logging.info("fetching for user '%s'" % user.key().name()) taskqueue.add( queue_name='fetch-queue', url='/task/fetch_material/%s/0' % user.key().name(), ) user.last_fetch = datetime.now() user.put() return 'ok'
def do_task_post_curry(username): user = CurryUser.get_by_key_name(username) display_string = effects.display_effect(user) # post the tweet try: status = api.PostUpdate( u'@%s の カレーが できあがった! 今日の材料は %s。%s %s' % (username, user.curry_material, display_string, user.state_string)) user.curry_count = 0 user.curry_material = "" #TODO: move to instance method user.spicy = 1.0 user.price = 0 user.kal = 0 user.color = [128, 128, 0] user.state_string = "" user.put() logging.debug("posted '%s'" % status.GetText()) except twitter.TwitterError, e: logging.debug('duplicated user %s with material %s', (username, material_list))
def do_task_fetch_material(username, force_top): user = CurryUser.get_by_key_name(username) force_top = bool(int(force_top)) if not user: logging.error("no such user '%s'" % username) return 'bad' tweet_list = api.GetUserTimeline(screen_name=username, count=config.FETCH_COUNT) tweet = None material_list = None success = False if force_top: tweet_list = tweet_list[0:1] else: shuffle(tweet_list) # # select material # for tweet in tweet_list: # check history if not force_top and is_duplicated(tweet): continue text = tweet.GetText().encode('utf-8') material_list = analyze(text, count=config.TWEET_MATERIAL_MAX) if len(material_list) > 0: # found material success = True break if success: # record to history # TODO: trim history chronically History(key_name=str(tweet.id), timestamp=datetime.now()).put() else: logging.info("material not found for user '%s'" % username) return 'bad' # # select receivers # link_list = (UserLink.all().filter( 'sender = ', user).order('timestamp').fetch(limit=config.RECEIVER_MAX)) for link in link_list: # randomize material per receiver shuffle(material_list) count = 1 + int(random() * len(material_list)) receive_material = material_list[:count] taskqueue.add(queue_name='post-queue', url='/task/post_material/%s/%s' % (username, link.receiver.key().name()), params={'material': receive_material}) link.timestamp = datetime.now() logging.debug( "sending from user '%s' to '%s' with material '%s'" % (username, link.receiver.key().name(), repr(receive_material))) # update timestamp db.put(link_list) # send to karei_bot if no receivers if len(link_list) == 0: shuffle(material_list) count = 1 + int(random() * len(material_list)) receive_material = material_list[:count] taskqueue.add(queue_name='post-queue', url='/task/post_material/%s/%s' % (username, config.MY_NAME), params={'material': receive_material}) logging.debug("sending from user '%s' to '%s' with material '%s'" % (username, config.MY_NAME, repr(receive_material))) return 'ok'
# # post the tweet # try: status = api.PostUpdate( u'@%s は @%s の カレーに %sを 入れた。 %s' % (sender_name, receiver_name, material_str, effect_string)) logging.debug("posted '%s'" % status.GetText()) except twitter.TwitterError, e: logging.debug('duplicated user %s with material %s', (sender_name, material_list)) # # update the curry # receiver = CurryUser.get_by_key_name(receiver_name) if receiver.curry_count > 0: receiver.curry_material += u'、' + material_str else: receiver.curry_material = material_str tmp_color = [0, 0, 0] for effect in effect_list: receiver.spicy *= effect.spicy receiver.kal += effect.kal receiver.price += effect.price tmp_color[0] += effect.color[0] tmp_color[1] += effect.color[1] tmp_color[2] += effect.color[2] total = receiver.curry_count + len(material_list)
def do_task_fetch_material(username, force_top): user = CurryUser.get_by_key_name(username) force_top = bool(int(force_top)) if not user: logging.error("no such user '%s'" % username) return 'bad' tweet_list = api.GetUserTimeline(screen_name=username, count=config.FETCH_COUNT) tweet = None material_list = None success = False if force_top: tweet_list = tweet_list[0:1] else: shuffle(tweet_list) # # select material # for tweet in tweet_list: # check history if not force_top and is_duplicated(tweet): continue text = tweet.GetText().encode('utf-8') material_list = analyze( text, count=config.TWEET_MATERIAL_MAX ) if len(material_list) > 0: # found material success = True break if success: # record to history # TODO: trim history chronically History( key_name=str(tweet.id), timestamp=datetime.now() ).put() else: logging.info("material not found for user '%s'" % username) return 'bad' # # select receivers # link_list = (UserLink .all() .filter('sender = ', user) .order('timestamp') .fetch(limit=config.RECEIVER_MAX) ) for link in link_list: # randomize material per receiver shuffle(material_list) count = 1 + int(random() * len(material_list)) receive_material = material_list[:count] taskqueue.add( queue_name='post-queue', url='/task/post_material/%s/%s' % (username, link.receiver.key().name()), params={'material': receive_material} ) link.timestamp=datetime.now() logging.debug("sending from user '%s' to '%s' with material '%s'" % (username, link.receiver.key().name(), repr(receive_material))) # update timestamp db.put(link_list) # send to karei_bot if no receivers if len(link_list) == 0: shuffle(material_list) count = 1 + int(random() * len(material_list)) receive_material = material_list[:count] taskqueue.add( queue_name='post-queue', url='/task/post_material/%s/%s' % (username, config.MY_NAME), params={'material': receive_material} ) logging.debug("sending from user '%s' to '%s' with material '%s'" % (username, config.MY_NAME, repr(receive_material))) return 'ok'
status = api.PostUpdate( u'@%s は @%s の カレーに %sを 入れた。 %s' % ( sender_name, receiver_name, material_str, effect_string ) ) logging.debug("posted '%s'" % status.GetText()) except twitter.TwitterError, e: logging.debug('duplicated user %s with material %s', (sender_name, material_list)) # # update the curry # receiver = CurryUser.get_by_key_name(receiver_name) if receiver.curry_count > 0: receiver.curry_material += u'、' + material_str else: receiver.curry_material = material_str tmp_color = [0, 0, 0] for effect in effect_list: receiver.spicy *= effect.spicy receiver.kal += effect.kal receiver.price += effect.price tmp_color[0] += effect.color[0] tmp_color[1] += effect.color[1] tmp_color[2] += effect.color[2] total = receiver.curry_count + len(material_list)