def identify(self, social_key, entity, request): differences = { social_key + '__add': entity[social_key], 'ts_match_' + social_key: int(time.time()) } print 'valid:', social_key, entity[social_key] if entity['match_' + social_key]: del entity['match_' + social_key] entity.partial_save() EntityHistory().delta(entity, differences) league = Entity().get_item(league=entity[keys.entity_league], profile=Entity().league_profile( entity[keys.entity_league])) curator = User().get_curator(entity[keys.entity_league]) tweet_txt = getattr(tweets, 'id_' + social_key)(entity, curator, league) try: print 'tweet message:', tweet_txt except: pass tweet_message = {} tweet_message.update(entity._data) tweet_message[twitter_keys.message_tweet] = tweet_txt request.write(json.dumps(tweet_message, cls=fixed.SetEncoder)) request.finish() return server.NOT_DONE_YET
def scout_delta(self, item, differences, tweet=True): EntityHistory().delta(item, differences) if tweet: message = {} message.update(item._data) message.update(differences) dt = tweets.delta_tweet(item, item, differences, self.curator, self.league) print 'scout delta tweet:', dt message[twitter_keys.message_tweet] = dt TweetQueue().createMessage(message)
def process_drop(self, existing_player, curator, league): ct = tweets.cut_tweet(existing_player._data, curator, league) try: print 'cut tweet:', ct except: pass EntityHistory().cut(league[keys.entity_league], existing_player) message = {} message.update(existing_player._data) message[twitter_keys.message_tweet] = ct return message
def process_add(self, new_player, curator, league): print 'try to find old cut-', curator[user_keys.user_role], league[ keys.entity_league] history_last_cut = EntityHistory().last_cut( new_player[keys.entity_profile], league[keys.entity_league]) if history_last_cut: print 'found cut instance:', fixed.lingo_since( history_last_cut, time_keys.ts_cut) for key in [ k for k in history_last_cut.keys() if k in keys.social_keys ]: if history_last_cut[key] and key != keys.entity_match_twitter: print 'cut player with key:', key, 'adding to new player:', history_last_cut[ key] if key == keys.entity_twitter_id: new_player[key] = str(history_last_cut[key]) else: new_player[key] = history_last_cut[key] else: print 'never been cut' new_player[keys.entity_league] = league[keys.entity_league] new_player[keys.entity_site] = curator[user_keys.user_role] print new_player try: Entity().put_item(new_player) addtweet = tweets.add_tweet(new_player, curator, league) try: print 'add tweet:', addtweet except: pass message = {} message.update(new_player) message[twitter_keys.message_tweet] = addtweet return message except Exception as e: print 'already exists:', e
def process_delta(self, scraped_player, existing_player, differences, curator, league): print 'not dry run-', existing_player[keys.entity_league] scraped_player[keys.entity_league] = existing_player[ keys.entity_league] scraped_player[keys.entity_site] = curator[user_keys.user_role] message = {} message.update(existing_player._data) message.update(scraped_player) message.update(differences) for key in [k for k in existing_player.keys() if k.startswith('ts_') ] + keys.social_keys + [ k for k in existing_player.keys() if k.startswith('ct_') ]: scraped_player[key] = existing_player[key] overwrite = Entity().put_item(scraped_player, overwrite=True) print 'overwrite:', overwrite try: put_result = EntityHistory().delta(existing_player, differences) print 'difference tweet:', put_result except Exception as e: print 'difference exception:', e dt = tweets.delta_tweet(scraped_player, existing_player, differences, curator, league) try: print 'delta tweet:', dt except: pass try: message[twitter_keys.message_tweet] = dt #if not self.filter_tweet(message): # TweetQueue().createMessage(message) return message except Exception as e: print 'delta tweet exception:', e
def create_rankings(league_name, league_profile): return [ e._data for e in EntityHistory().query_2(profile__eq=league_profile, **ranking_query) ]
def render_POST(self, request): shared.SharedPath().response_headers(request, 'application/json') body = json.loads(request.content.read()) print 'match post:', request.prepath, 'uri:', request.uri, 'body:', body try: social_key = [sk for sk in keys_league.social_keys if sk in body][0] print 'social_key:', social_key if 'block' in body and body['block']: blocks = keys_league.add_blocked( shared.SharedPath().path_site(request), social_key, body[social_key]) print 'blocks:', blocks if keys.entity_profile in body: entity = self.find_by_profile(request, body[keys.entity_profile]) del entity[social_key] del entity[getattr(keys, 'entity_match_' + social_key)] entity.partial_save() request.write( json.dumps({ 'match': 'blocked', keys.entity_profile: body[keys.entity_profile], social_key: body[social_key] })) request.finish() return server.NOT_DONE_YET elif 'no' in body and body['no']: entity = self.find_by_profile(request, body[keys.entity_profile]) del entity[getattr(keys, 'entity_match_' + social_key)] entity.partial_save() request.write( json.dumps({ 'match': 'deleted', keys.entity_profile: body[keys.entity_profile], social_key: body[social_key] })) request.finish() return server.NOT_DONE_YET elif 'remove' in body and body['remove']: entity = self.find_by_profile(request, body[keys.entity_profile]) if entity[social_key] == body[social_key]: request.write('removed: ' + entity[social_key].encode('utf-8')) del entity[social_key] differences = { social_key + '__remove': entity[social_key], time_keys.ts_remove: int(time.time()) } EntityHistory().delta(entity, differences) entity.partial_save() request.finish() return server.NOT_DONE_YET else: self.render_ERROR(400, 'Cannot Remove:' + str(body[social_key]), request) return server.NOT_DONE_YET else: entity = self.find_by_profile(request, body[keys.entity_profile]) body[social_key] = body[social_key][1:] if body[ social_key][:1] == '@' else body[social_key] if not entity[social_key] or 'overwrite' in body and body[ 'overwrite']: entity[social_key] = body[social_key] if (social_key == keys.entity_twitter and twitter_keys.validate_twitter(entity)) or ( social_key == keys.entity_instagram and instagram_keys.validate_instagram(entity)): return self.identify(social_key, entity, request) else: print 'league has:', social_key, 'value:', entity[ social_key] self.render_ERROR( 400, 'Invalid:' + entity[social_key].encode('utf-8'), request) return server.NOT_DONE_YET else: print 'already has:', social_key, 'value:', entity[ social_key] self.render_ERROR( 409, 'Already Has:' + entity[social_key].encode('utf-8'), request) return server.NOT_DONE_YET except Exception as e: print 'social key exception:', e