def top_liked_posts(request, page_number=0): user_data, token, graph, page_id = utils.get_graph_api_inf( request.user.id, page_number) user_data = UserData.objects.get(user_id=request.user.id) data = graph.get_object(id=page_id, fields='name, picture') profile_image_url = data['picture']['data']['url'] page_name = data['name'] context = { 'page_number': page_number, 'image_url': profile_image_url, 'name': page_name } if request.method == 'POST' and 'refresh_data' in request.POST: utils.refresh_top_likes(graph, page_id, request.user.id, page_number) return HttpResponseRedirect(request.path_info) context['top_5_posts'] = user_data.pages[ page_number].statistics.top_liked_posts context['last_refresh'] = user_data.pages[ page_number].statistics.top_liked_posts_refresh_date context['post_data'] = utils.get_post_data(graph, context['top_5_posts']) context['type'] = "likes" return render(request, 'home/statistics/top_x_posts.html', context)
def add_post(request, page_number=0): if request.method == 'POST': form = InsertPost(request.POST, request.FILES) if form.is_valid(): message = form.cleaned_data['message'] image = form.cleaned_data.get('image') _, _, graph, page_id = utils.get_graph_api_inf( request.user.id, page_number) if image is None: graph.put_object(parent_object='me', message=message, page_number=page_id, connection_name='feed') else: img = Image.open(image, mode='r') img_byte_array = io.BytesIO() img.save(img_byte_array, format=img.format) img_byte_array = img_byte_array.getvalue() graph.put_photo(image=img_byte_array, message=message) return render(request, 'home/management/add_post.html', { 'page_number': page_number, 'form': InsertPost(), 'isValid': True }) else: return render(request, 'home/management/add_post.html', { 'page_number': page_number, 'form': InsertPost(), 'isValid': False })
def single_post(request, page_number=0, post_id=None): user_data, token, graph, page_id = utils.get_graph_api_inf( request.user.id, page_number) # checking if post with that id exists, allowing singular post id number of required by facebook id prefixed by # page id # ex. 12345 works as well as 9876_12345 where 12345 is post id and 9876 is page id try: xd = StatPost(position=1, post_id=post_id) post = utils.get_post_data(graph, [xd])[0] except: try: post_id = page_id + "_" + post_id xd = StatPost(position=1, post_id=post_id) post = utils.get_post_data(graph, [xd])[0] except: return render(request, 'home/management/single_post.html', {}) page_data = graph.get_object(id=page_id, fields='name, picture') profile_image_url = page_data['picture']['data']['url'] page_name = page_data['name'] context = { 'page_number': page_number, 'post': post, 'image_url': profile_image_url, 'name': page_name, 'post_id': post_id } # liking all comments in given post if request.method == 'POST' and 'like_comments' in request.POST: utils.like_comments_in_post( request.POST.dict()['post_id_where_comments_to_like'], graph, page_id) # deleting comments in given post which including banned words from database elif request.method == 'POST' and 'delete_comments' in request.POST: utils.delete_comments_in_post( request.POST.dict()['post_id_where_comments_to_delete'], graph, user_data.pages[page_number].words, page_id) # deleting given post elif request.method == 'POST' and 'delete_post' in request.POST: utils.delete_post(request.POST.dict()['post_to_delete'], graph) return HttpResponseRedirect(request.path_info) return render(request, 'home/management/single_post.html', context)
def top_commenters(request, page_number=0): user_data, token, graph, page_id = utils.get_graph_api_inf( request.user.id, page_number) user_data = UserData.objects.get(user_id=request.user.id) context = {'page_number': page_number} if request.method == 'POST' and 'refresh_data' in request.POST: utils.refresh_top_commenters(graph, page_id, request.user.id, page_number) return HttpResponseRedirect(request.path_info) context['top_5_commenters'] = user_data.pages[ page_number].statistics.top_commenters context['commenters_last_refresh'] = user_data.pages[ page_number].statistics.top_commenters_refresh_date return render(request, 'home/statistics/top_commenters.html', context)
def index(request, page_number=0, after='none'): user_data, token, graph, page_id = utils.get_graph_api_inf( request.user.id, page_number) data = graph.get_object(id=page_id, fields='name, picture') profile_image_url = data['picture']['data']['url'] page_name = data['name'] connection_name = 'feed?limit=10' if after != 'none': connection_name += '&after=' + after posts_data = graph.get_connections(id=page_id, connection_name=connection_name) posts = posts_data['data'] if 'paging' in posts_data: after_this = posts_data['paging']['cursors']['after'] else: after_this = None for post in posts: post_data = graph.get_connections( id=post['id'], connection_name= '?&fields=reactions.limit(0).summary(total_count),shares,' 'comments.limit(0).summary(' 'total_count), object_id, type, created_time, message') post['created_time'] = dateutil.parser.parse(post['created_time']) # cannot get photo and video of shared post try: if post_data['type'] == "photo": photo_data = graph.get_object(id=post_data['object_id'], fields='images') post['photo_source'] = photo_data['images'][0]['source'] elif post_data['type'] == "video": video = graph.get_object(id=post_data['object_id'], fields='source') post['video_source'] = video['source'] except: pass if 'comments' in post_data: post['comments'] = post_data['comments']['data'] post['comments_nr'] = post_data['comments']['summary'][ 'total_count'] else: post['comments_nr'] = 0 if 'shares' in post_data: post['shares_nr'] = post_data['shares']['count'] else: post['shares_nr'] = 0 if 'reactions' in post_data: post['reactions_nr'] = post_data['reactions']['summary'][ 'total_count'] else: post['reactions_nr'] = 0 context = { 'page_number': page_number, 'after': after_this, 'posts': posts, 'image_url': profile_image_url, 'name': page_name } # liking all comments in every post if request.method == 'POST' and 'like_all_comments' in request.POST: utils.like_comments_in_every_post(graph, page_id) # liking all comments in given post elif request.method == 'POST' and 'like_comments' in request.POST: utils.like_comments_in_post( request.POST.dict()['post_id_where_comments_to_like'], graph, page_id) # deleting comments in every post which including banned words from database elif request.method == 'POST' and 'delete_all_comments' in request.POST: utils.delete_comments_in_every_post(graph, user_data.pages[page_number].words, page_id) # deleting comments in given post which including banned words from database elif request.method == 'POST' and 'delete_comments' in request.POST: utils.delete_comments_in_post( request.POST.dict()['post_id_where_comments_to_delete'], graph, user_data.pages[page_number].words, page_id) # deleting given post elif request.method == 'POST' and 'delete_post' in request.POST: utils.delete_post(request.POST.dict()['post_to_delete'], graph) return HttpResponseRedirect(request.path_info) return render(request, 'home/index.html', context)