def login_facebook(request): if request.GET.get('error'): messages.error(request, '사용자가 페이스북 로그인을 취소했습니다') return redirect('member:login') if request.GET.get('code'): # 사용자가 페이스북로그인 버튼을 눌러 페이스북에서 로그인에 성공했을 경우, # 페이스북에서 mysite로 access_token을 요청할 수 있는 'code'값을 보내준다 REDIRECT_URL = 'http://127.0.0.1:8000/member/login/facebook/' code = request.GET.get('code') access_token = facebook.get_access_token(code, REDIRECT_URL) user_id = facebook.get_user_id_from_token(access_token) user_info = facebook.get_user_info(user_id, access_token) # authenticate backends에 FacebookBackend추가해서 dict_user_info객체로 로그인 가능 user = auth_authenticate(user_info=user_info) if user is not None: auth_login(request, user) messages.success(request, '페이스북 유저로 로그인 되었습니다') return redirect('blog:post_list') else: messages.error(request, '페이스북 로그인에 실패하였습니다') return redirect('member:login')
def friends_ranking(request): if request.GET.get('error'): return HttpResponse('사용자 로그인 거부') if request.GET.get('code'): redirect_uri = 'http://{host}{url}'.format( host=request.META['HTTP_HOST'], url=reverse('sns:friends_ranking') ) print('redirect_url : %s' % redirect_uri) code = request.GET.get('code') access_token = facebook.get_access_token(code, redirect_uri) user_id = facebook.get_user_id_from_token(access_token) url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \ 'fields=comments{{from,comments}}&' \ 'access_token={access_token}'.format( user_id=user_id, access_token=access_token, ) r = requests.get(url_request_feed) dict_feed_info = r.json() json_data = json.dumps(dict_feed_info, indent=2) print(json_data) return HttpResponse(json_data)
def login_facebook(request): if request.GET.get('error'): messages.error(request, '페이스북 로그인을 취소했습니다') return redirect('member:login') if request.GET.get('code'): code = request.GET.get('code') REDIRECT_URL = 'http://127.0.0.1:8000/member/login/facebook/' # 모듈화 작업함함 access_token = facebook.get_access_token(code, REDIRECT_URL) user_id = facebook.get_user_id_from_token(access_token) dict_user_info = facebook.get_user_info(user_id, access_token) # 유저를 인증하고 로그인시키자 user = auth_authenticate(user_info=dict_user_info) if user is not None: auth_login(request, user) messages.success(request, '페이스북 로그인 성공') return redirect('blog:post_list') else: messages.error(request, '페이스북 로그인 실패') return redirect('member:login')
def show_mypost(request): if request.GET.get('error'): return HttpResponse('사용자 거부') if request.GET.get('code'): redirect_uri = 'http://{host}{url}'.format( host=request.META['HTTP_HOST'], url=reverse('sns:show_mypost')) code = request.GET.get('code') access_token = facebook.get_access_token(code, redirect_uri) user_id = facebook.get_user_id_from_token(access_token) url_request_post = 'https://graph.facebook.com/v2.8/{user_id}/posts?' \ 'access_token={access_token}'.format( user_id=user_id, access_token=access_token, ) r = requests.get(url_request_post) dict_post_info = r.json() post_data_list = [] for post in dict_post_info['data']: if post.get('message'): post_data_list.append({ 'message': post['message'], 'created_time': post['created_time'], }) context = { 'posts': post_data_list, } return render(request, 'sns/facebook/show_my_posts.html', context)
def friends_ranking(request): if request.GET.get('error'): return HttpResponse('사용자 로그인 거부') if request.GET.get('code'): redirect_url = 'http://{host}{url}'.format( host=request.META['HTTP_HOST'], url=reverse('sns:friends_ranking') ) print('%s' % redirect_url) code = request.GET.get('code') access_token = facebook.get_access_token(code, redirect_url) user_id = facebook.get_user_id_from_token(access_token) url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \ 'fields=from{{name}},message,comments.limit(100){{from{{name,picture}},message,comments{{from{{name,picture}},message}}}}&' \ 'access_token={access_token}'.format( user_id=user_id, access_token=access_token, ) r = requests.get(url_request_feed) dict_feed_info = r.json() json_data = json.dumps(dict_feed_info, indent=2) print(json_data) feed_count = {} for feed in dict_feed_info['data']: if 'comments' in feed: for feed2 in feed['comments']['data']: if 'comments' in feed2: for feed3 in feed2['comments']['data']: if feed3['from']['name'] in feed_count: feed_count[feed3['from']['name']] += 1 else: feed_count[feed3['from']['name']] = 1 if feed2['from']['name'] in feed_count: feed_count[feed2['from']['name']] += 1 else: feed_count[feed2['from']['name']] = 1 return render(request, 'sns/facebook.html', {'feed_count': feed_count})
def login_facebook(request): if request.GET.get('error'): messages.error(request, '사용자가 페이스북 로그인을 거절하였습니다.') return redirect('member:login') if request.GET.get('code'): REDIRECT_URL = 'http://127.0.0.1:8000/member/login/facebook/' code = request.GET.get('code') access_token = facebook.get_access_token(code, REDIRECT_URL) user_id = facebook.get_user_id_from_token(access_token) user_info = facebook.get_user_info(user_id, access_token) user = auth_authenticate(user_info=user_info) if user is not None: auth_login(request, user) messages.success(request, '페이스북 유저로 로그인 되었습니다') return redirect('blog:post_list') else: messages.error(request, '페이스북 로그인에 실패하였습니다') return redirect('member:login')
def login_facebook(request): if request.GET.get('error'): messages.error(request, '사용자가 페이스북 로그인을 취소하셨습니다.' ) return redirect( 'login' ) code = request.GET.get('code') print(code) redirect_uri = 'http://127.0.0.1:8000/member/login/facebook/' access_token = facebook.get_access_token(code, redirect_uri) user_id = facebook.get_user_id_from_token(access_token) user_info = facebook.get_user_info(user_id) user = auth_auth(user_info=user_info) if user is None: messages.error(request, '페이스북 로그인에 실패하였습니다.') return render(request, "member/login.html", {}) else: auth_login(request, user) messages.success(request, '페이스북 로그인에 성공하였습니다.') return redirect( 'post_list')
def friends_ranking(request): if request.method == 'GET': if request.GET.get('error'): return HttpResponse('사용자 로그인 거부') if request.GET.get('code'): redirect_uri = 'http://{host}{url}'.format( host=request.META['HTTP_HOST'], url=reverse('sns:friends_ranking') ) ValueError print('redirect_uri : %s' % redirect_uri) code = request.GET.get('code') access_token = facebook.get_access_token(code, redirect_uri) user_id = facebook.get_user_id_from_token(access_token) url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \ 'fields=comments{{from,comments}}&' \ 'access_token={access_token}'.format( user_id=user_id, access_token=access_token, ) r = requests.get(url_request_feed) dict_feed_info = r.json() json_data = json.dumps(dict_feed_info, indent=2) # print(json_data) comment_list = [] # data의 각 item이 feed for feed in dict_feed_info.get('data'): if feed.get('comments'): for comment in feed.get('comments').get('data'): comment_list.append(comment) # print(comment_list) counter = Counter() id_list = [comment.get('from', {}).get('id') for comment in comment_list] for id in id_list: counter[id] += 1 # print(counter) most_list = counter.most_common() req_id_list = list(set(id_list)) print(req_id_list) str_req_id_list = ','.join(req_id_list) print(str_req_id_list) uri_request_user_info_list = 'https://graph.facebook.com/v2.8/?ids={ids}&' \ 'fields=cover,email,picture.width(500).height(500),name&' \ 'access_token={access_token}'.format( ids=str_req_id_list, access_token=access_token ) r = requests.get(uri_request_user_info_list) dict_friends_info = r.json() json_friends_info = json.dumps(dict_friends_info, indent=2) print(json_friends_info) most_dict_list = [] for item in most_list: id = item[0] for k in dict_friends_info: if k == id and k != user_id: most_dict_list.append({ 'info': dict_friends_info[k], 'number': item[1] }) context = { 'most_dict_list': most_dict_list } return render(request, 'sns/facebook/friends_ranking.html', context) elif request.method == 'POST': import ast str_item_list = request.POST.getlist('item') for str_item in str_item_list: item = ast.literal_eval(str_item) print(item)
def friends_ranking(request): if request.method == 'GET': if request.GET.get('error'): return HttpResponse('사용자 로그인 거부') if request.GET.get('code'): redirect_uri = 'http://{host}{url}'.format( host=request.META['HTTP_HOST'], url=reverse('sns:friends_ranking')) print('redirect_uri : %s' % redirect_uri) code = request.GET.get('code') access_token = facebook.get_access_token(code, redirect_uri) user_id = facebook.get_user_id_from_token(access_token) url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \ 'fields=comments{{from,comments}}&' \ 'access_token={access_token}'.format( user_id=user_id, access_token=access_token, ) r = requests.get(url_request_feed) dict_feed_info = r.json() json_data = json.dumps(dict_feed_info, indent=2) # print(json_data) comment_list = [] # data의 각 item이 feed for feed in dict_feed_info.get('data'): if feed.get('comments'): for comment in feed.get('comments').get('data'): comment_list.append(comment) # print(comment_list) counter = Counter() id_list = [ comment.get('from', {}).get('id') for comment in comment_list ] for id in id_list: counter[id] += 1 # print(counter) most_list = counter.most_common() req_id_list = list(set(id_list)) print(req_id_list) str_req_id_list = ','.join(req_id_list) print(str_req_id_list) uri_request_user_info_list = 'https://graph.facebook.com/v2.8/?ids={ids}&' \ 'fields=cover,email,picture.width(500).height(500),name&' \ 'access_token={access_token}'.format( ids=str_req_id_list, access_token=access_token ) r = requests.get(uri_request_user_info_list) dict_friends_info = r.json() json_friends_info = json.dumps(dict_friends_info, indent=2) print(json_friends_info) most_dict_list = [] for item in most_list: id = item[0] for k in dict_friends_info: if k == id and k != user_id: most_dict_list.append({ 'info': dict_friends_info[k], 'number': item[1] }) context = {'most_dict_list': most_dict_list} return render(request, 'sns/facebook/friends_ranking.html', context) elif request.method == 'POST': import ast str_item_list = request.POST.getlist('item') for str_item in str_item_list: item = ast.literal_eval(str_item) print(item)
def friends_ranking(request): if request.method == 'GET': if request.GET.get('error'): return HttpResponse('사용자 로그인 거부') if request.GET.get('code'): code = request.GET.get('code') REDIRECT_URL = 'http://{host}{url}'.format( host=request.META['HTTP_HOST'], url=reverse('sns:friends_ranking'), ) access_token = facebook.get_access_token(code, REDIRECT_URL) # return HttpResponse('%s<br>%s' % (REDIRECT_URL, access_token)) user_id = facebook.get_user_id_from_token(access_token) # facebook에서 comment 하위 속성은 { } 형식으로 불러온다 # {{}} 형식을 사용해야 format() 형식에서 사용 가능하다 url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \ 'fields=comments{{from,comments}}&' \ 'limit=1000&' \ 'access_token={access_token}'.format( user_id=user_id, access_token=access_token, ) r = requests.get(url_request_feed) dict_feed_info = r.json() json_data = json.dumps(dict_feed_info, indent=2) # print(json_data) # print(dict_feed_info) comment_list = [] for feed in dict_feed_info.get('data'): # print(feed) if feed.get('comments'): for comment in feed.get('comments').get('data'): comment_list.append(comment) id_list = [ comment.get('from', {}).get('id') for comment in comment_list ] """ Counter 함수를 사용해보자 >> > # Tally occurrences of words in a list >> > cnt = Counter() >> > for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']: ... cnt[word] += 1 >> > cnt Counter({'blue': 3, 'red': 2, 'green': 1}) """ counter = Counter() for id in id_list: counter[id] += 1 # order by most most_id_list = counter.most_common() print(most_id_list) set_most_id_list = list(set(id_list)) set_most_id_list = set_most_id_list[:50] str_set_most_id_list = ','.join(set_most_id_list) # print(str_set_most_id_list) uri_request_ids_info = 'https://graph.facebook.com/v2.8/{user_id}/' \ '?ids={ids}&' \ 'fields=cover,email,picture,name&' \ 'access_token={access_token}'.format( user_id=user_id, ids=str_set_most_id_list, access_token=access_token, ) r = requests.get(uri_request_ids_info) dict_ids_info = r.json() print(json.dumps(dict_ids_info, indent=2)) dict_ids_info_list = [] for item in most_id_list: id = item[0] for k in dict_ids_info: if k == id and k != user_id: dict_ids_info_list.append({ 'info': dict_ids_info[k], 'number': item[1], }) print(dict_ids_info_list) context = {'dict_ids_info_list': dict_ids_info_list} return render(request, 'sns/facebook/friends_ranking.html', context) elif request.method == 'POST': import ast str_item_list = request.POST.getlist('item') # string 을 list로 변환 for str_item in str_item_list: item = ast.literal_eval(str_item)