def get_mentions_and_send_notification(comment_id): dynamo_client = get_dynamo_client() comment = dynamo_client.get_item(TableName='Comment', Key={'id': {'S': comment_id}}).get('Item') comment_text = strip_tags(comment.get('comment').get('S')) mention_tag = [mention for mention in comment_text.split() if mention.startswith("@")] user_ids = [] for mention in mention_tag: user = dynamo_client.get_item( TableName='User', IndexName='username_index', Key={'username': {'S': mention.strip('@')}} ).get('Item') if user.get('id').get('S') == comment.get('user_id').get('S'): continue dynamo_client.put_item( TableName='Notification', Item={ 'for_user_id': {'S': comment.get('user_id').get('S')}, 'notification_type': {'S': '10'}, 'user_id': {'S': user.get('id').get('S')}, 'video_byte_id': comment_id } ) user_ids.append(user.get('id').get('S')) return user_ids
def sync_contacts_with_user(user_id): dynamo_client = get_dynamo_client() user_contacts = dynamo_query_till_end( TableName='UserPhoneBook', ExpressionAttributeValues={':u': {'S': user_id}}, KeyConditionExpression=' user_id = :u ' ) user_contact_map = {contact.get('mobile_number'): contact for contact in user_contacts} for user in dynamo_query_till_end( TableName='User', IndexName='mobile_no_index', ExpressionAttributeValues={':ml': [contact.get('contact_number') for contact in user_contacts]}, KeyConditionExpression=' mobile_no IN :ml ', AttributesToGet=['id', 'mobile_no'] ): if not user.get('is_active'): continue contact = user_contact_map.get(user.get('mobile_no').get('S')) dynamo_client.update_item( TableName='Contact', Key={'user_id': {'S': contact.get('user_id').get('S')}, 'contact_id': {contact.get('id').get('S')}}, ExpressionAttributeValues={':iru': {'BOOL': True}}, UpdateExpression='SET is_user_registered = :iru' )
def user_ip_to_state_task(user_id, ip): response = requests.get("http://ip-api.com/json/%s"%ip) if not response.ok: return data = json.loads(response.text) get_dynamo_client().update_item( TableName='User', Key={'id': {'S': user_id}}, ExpressionAttributeValues={ ':region': {'S': data.get('regionName')}, ':city': {'S': data.get('city')} }, KeyConditionExpression='SET state_name = :region, city_name = :city' )
def add_to_history(user_id, score, action, action_object_type, action_object_id, is_removed): dynamo_client = get_dynamo_client() last_evaluated_key = None history = None while True: result = dynamo_client.query( TableName='BoloActionHistory', IndexName='user_action_index', ExpressionAttributeValues={':u': {'S': user_id}, ':a': {'S': action}}, KeyConditionExpression='user_id = :u and action = :a', LastEvaluatedKey=last_evaluated_key, AttributesToGet=['action_object_type', 'action_object_id', 'id'] ) if result.get('Count') == 0: break history = filter(lambda x: x.get('action_object_type').get('S') == action_object_type and \ x.get('action_object_id').get('S') == action_object_id, result.get('Items')) if len(history): history = history[0] break last_evaluated_key = result.get('LastEvaluatedKey') if history: dynamo_client.update_item( TableName='BoloActionHistory', ExpressionAttributeNames={'#R': 'is_removed'}, ExpressionAttributeValues={':r': {'N': 1 if is_removed else 0}}, Key={'id': {'S': history.get('id').get('S')}}, UpdateExpression='SET #R = :r', ReturnValues='NONE' ) else: dynamo_client.put_item( TableName='BoloActionHistory', Item={ 'id': str(uuid.uuid4()), 'created_at': str(datetime.now()), 'last_modified_at': str(datetime.now()), 'user_id': user_id, 'score': score, 'action': action, 'action_object_type': action_object_type, 'action_object_id': action_object_id, 'is_removed': 1 if is_removed else 0, 'is_encashed': 0, 'is_eligible_for_encash': 1 } )
def default_boloindya_follow(user_id, language): dynamo_client = get_dynamo_client() user = dynamo_client.get_item(TableName='User', Key={'id': {'S': user_id}}).get('Item') language_name = settings.LANGUAGE_OPTIONS_DICT.get(language) boloindya_username = '******'%language_name.lower() if language_name else 'boloindya' boloindya_user = dynamo_client.query( TableName='User', IndexName='username_index', AttributesToGet=['id', 'username'], ExpressionAttributeValues={':username': {'S': boloindya_username}}, KeyConditionExpression='username = :username' ).get('Items')[0] try: follow = dynamo_client.update_item( TableName='Follow', ExpressionAttributeNames={'#A': 'is_active'}, ExpressionAttributeValues={':a': {'N': 1}}, Key={ 'user_id': {'S': boloindya_user.get('id').get('S')}, 'follower_id': {'S': user_id} }, UpdateExpression='SET #A = :a', ReturnValues='UPDATED_NEW' ) except Exception as e: follow = dynamo_client.put_item( TableName='Follow', Item={ 'user_id': {'S': boloindya_user.get('id').get('S')}, 'follower_id': {'S': user_id}, 'is_active': {'N', 1}, 'created_at': {'S', str(datetime.now())}, 'last_modified_at': {'S', str(datetime.now())} }, ReturnValues='UPDATED_NEW' ).get('Attributes') add_bolo_score(user_id, 'follow', 'Follow', '%s:%s'%(follow.get('user_id').get('S'), follow.get('follower_id').get('S'))) update_dynamo_entry_count('User', 'follow_count', 1, {'id': {'S': user_id}}, dynamo_client) update_dynamo_entry_count('User', 'follower_count', 1, {'id': {'S': follow.get('follower_id').get('S')}}, dynamo_client) #To do: Update these functions too update_redis_following(user.id, bolo_indya_user.id,True) update_redis_follower(bolo_indya_user.id,user.id,True) update_profile_counter(user.id,'follower_count',1, True) update_profile_counter(bolo_indya_user.id,'follow_count',1, True)
def send_report_mail(report_id): dynamo_client = get_dynamo_client() report = dynamo_client.get_item(TableName='Report', Key={'id': {'S': report_id}}).get('Item') reporter = dynamo_client.get_item(TableName='User', Key={'id': {'S': report.get('reported_by_id').get('S')}}).get('Item') video_byte = dynamo_client.get_item( TableName='VideoByte', Key={'id': {'S': report.get('topic_id').get('S')}}).get('Item') if report.get('target_type').get('S') == 'User': target_user_id = report.get('target_id').get('S') else: target_user_id = dynamo_client.get_item(TableName=report.get('target_type'), Key={'id': {'S': report.get('target_id').get('S')}}).get('Item').get('user_id').get('S') target_user = dynamo_client.get_item(TableName='User', Key={'id': {'S': target_user_id}}).get('Item') mail_message = render_to_string('report_email.html', context={ 'reported_by_username': reporter.get('username').get('S'), 'reported_by_name': reporter.get('name').get('S'), 'video_title': video_byte.get('title').get('S'), 'video_id': video_byte.get('id').get('S'), 'report_type': report.get('report_type').get('S'), 'target_mobile': target_user.get('mobile_no').get('S'), 'target_email': target_user.get('email').get('S') }) requests.post( settings.MAILGUN_CONFIG.get('host'), auth={'api': settings.MAILGUN_CONFIG.get('token')}, data={ 'from': settings.MAILGUN_CONFIG.get('from'), 'to': settings.MAILGUN_CONFIG.get('to'), 'cc': settings.MAILGUN_CONFIG.get('cc'), 'bcc': settings.MAILGUN_CONFIG.get('bcc'), 'subject': settings.MAILGUN_CONFIG.get('subject').format(target=report.get('target_type'), reporter_username=reporter.get('username').get('S')), 'html': mail_message } ) return True
def create_downloaded_url(vb_id): dynamo_client = get_dynamo_client() video_byte = dynamo_client.get_item(TableName='VideoByte', Key={'id': {'S': vb_id}}).get('Item') vb_user = dynamo_client.get_item(TableName='User', Key={'id': {'S': video_byte.get('user_id').get('S')}}).get('Item') backup_url = video_byte.get('backup_url').get('S') filename_temp = 'temp_' + backup_url.split('/')[-1] filename = backup_url.split('/')[-1] cmd = ['ffmpeg', '-i', backup_url, '-vf', "[in]scale=540:-1,drawtext=text='@" + vb_user.get("username").get("S") +"':x=10:y=H-th-20:fontsize=18:fontcolor=white[out]", settings.PROJECT_PATH+"/boloindya/scripts/watermark/"+filename_temp] ps = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE) (output, stderr) = ps.communicate() cmd = ''.join(['ffmpeg -i ', settings.PROJECT_PATH, '/boloindya/scripts/watermark/', filename_temp, ' -ignore_loop 0 -i ', settings.PROJECT_PATH, '/boloindya/media/img/boloindya_white.gif', ' -filter_complex "[1:v]format=yuva444p,scale=140:140,setsar=1,rotate=0:c=white@0:ow=rotw(0):oh=roth(0) [rotate];[0:v][rotate] overlay=10:(main_h-overlay_h+10):shortest=1" -codec:a copy -y ', settings.PROJECT_PATH, '/boloindya/scripts/watermark/', filename ]) subprocess.call(cmd,shell=True) downloaded_url = upload_media(open(os.path.join(settings.PROJECT_PATH, 'boloindya/scripts/watermark', filename)),filename) dynamo_client.update_item( TableName='VideoByte', ExpressionAttributeValues={':du': {'S': downloaded_url}, ':idu': {'BOOL': True}}, Key={'id': {'S': vb_id}} ) main_file_path = os.path.join(settings.PROJECT_PATH, 'boloindya/scripts/watermark', filename) temp_file_path = os.path.join(settings.PROJECT_PATH, 'boloindya/scripts/watermark', filename_temp) if os.path.exists(main_file_path): os.remove(main_file_path) os.remove(temp_file_path)
def send_upload_video_notification(data): headers = { 'Authorization': 'Bearer ' + _get_access_token(), 'Content-Type': 'application/json; UTF-8' } devices = get_dynamo_client().query( TableName='FCMDevice', IndexName='user_install_index', AttributesToGet=['user_id', 'is_uninstalled', 'reg_id'], ExpressionAttributeValues={ ':user_id': {'S': data.get('particular_user_id', None)}, ':is_uninstalled': {'N': 0} }, KeyConditionExpression='user_id = :user_id and is_uninstalled = :is_uninstalled' ) for device in devices.get('Items'): fcm_message = { 'message': { 'token': device.get('reg_id'), 'data': { 'title_upper': data.get('upper_title', ''), 'title': data.get('title', ''), 'id': data.get('id', ''), 'type': data.get('notification_type', ''), 'notification_id': '-1', 'image_url': data.get('image_url', '') } } } response = requests.post( settings.FCM_CONFIG.get('message_url'), data=json.dumps(fcm_message), headers=headers )
def add_bolo_score(user_id, feature, action_object_type, action_object_id): score = get_weight(feature) dynamo_client = get_dynamo_client() if not score: return update_dynamo_entry_count('User', bolo_score, int(score), {'id': {'S': user_id}}, dynamo_client) add_to_history(user_id, score, feature, action_object_type, action_object_id, False) if feature in ['create_topic', 'create_topic_en']: dynamo_client.put_item( TableName='Notification', Item={ 'id': str(uuid.uuid4()), 'created_at': str(datetime.now()), 'last_modified_at': str(datetime.now()), 'for_user_id': user_id, 'video_byte_type': action_object_type, 'video_byte_id': action_object_id, 'notification_type': '8', 'user_id': user_id } )