Ejemplo n.º 1
0
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')
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
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')
Ejemplo n.º 4
0
def login_facebook(request):
    login_next = request.GET.get('login_next')
    # quote_login_next = quote(login_next, safe='')
    # quote_login_next = login_next
    # print('login_next : %s' % login_next)
    # print('quote_login_next : %s' % quote_login_next)
    REDIRECT_URI = '{}{}'.format('%s://%s' % (request.scheme, request.META['HTTP_HOST']), reverse('member:login_facebook'))

    if request.GET.get('error'):
        messages.error(request, '사용자에 의해 페이스북 로그인 거부')
        return redirect('member:login')

    if request.GET.get('code'):
        code = request.GET.get('code')
        print('code: %s' % code)

        access_token = facebook.get_access_token(code, REDIRECT_URI)
        user_id = facebook.get_user_id_from_access_token(access_token)
        user_info = facebook.get_user_info(user_id, access_token)

        user = authenticate(user_info=user_info)
        if user is not None:
            login(request, user)
            messages.success(request, '페이스북 아이디로 로그인 되었습니다')
            if login_next:
                return redirect(login_next)
            else:
                return redirect('blog:post_list')
        else:
            messages.error(request, '페이스북 로그인에 실패하였습니다')
            return redirect('member:login')
Ejemplo n.º 5
0
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')
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
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})
Ejemplo n.º 8
0
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')
Ejemplo n.º 9
0
def login_facebook(request):
    if request.GET.get('error'):
        messages.error(request, 'failed login')
        return redirect('member:login')
    if request.GET.get('code'):
        REDIRECT_URI = 'http://127.0.0.1:8000/member/login/facebook/'
        code = request.GET.get('code')
        access_token = facebook.get_access_token(code, REDIRECT_URI)
        user_id = facebook.debug_token(access_token)
        user_info = facebook.get_user_info(user_id, access_token)

        #authenticate backends
        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, 'Facebook login success')
            return redirect('blog:post_list')
        else:
            messages.error(request, 'Failed Facebook login')
            return redirect('member:login')
Ejemplo n.º 10
0
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')
Ejemplo n.º 11
0
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)
Ejemplo n.º 12
0
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)
Ejemplo n.º 13
0
def friends_ranking(request):
    if request.GET.get('error'):
        return HttpResponse('access denied')
    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.debug_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,
                           )

        comment_list = []

        r = requests.get(url_request_feed)
        dict_feed_info = r.json()
        json_data = (json.dumps(dict_feed_info, indent=2))
        print(json_data)

        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)

        uri_request_user_info_list = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \
                                     'fields=cover,email,picture.width(500).height.width(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]
                    })

        for item in most_list:
            id = item[0]

            print(item)

        return HttpResponse(json_data)
Ejemplo n.º 14
0
def friends_ranking(request):
    if request.method == 'GET':
        redirect_uri = '{}{}'.format(
            '%s://%s' % (request.scheme, request.META['HTTP_HOST']),
            reverse('sns:friends_ranking'))

        if request.GET.get('error'):
            messages.error(request, '사용자에 의해 페이스북 로그인 거부')
            return redirect('member:login')

        if request.GET.get('code'):
            code = request.GET.get('code')

            access_token = facebook.get_access_token(code, redirect_uri)
            user_id = facebook.get_user_id_from_access_token(access_token)
            url_request_feed = 'https://graph.facebook.com/v2.8/{user_id}/feed?' \
                               'limit=10000&' \
                               'since=2013-01-01&' \
                               'until=2016-12-31&' \
                               'fields=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()

            comment_list = []
            for feed in dict_feed_info.get('data'):
                if feed.get('comments'):
                    for comment in feed.get('comments').get('data'):
                        comment_list.append(comment)

            counter = Counter()
            id_list = [
                comment.get('from', {}).get('id') for comment in comment_list
            ]
            for id in id_list:
                counter[id] += 1

            most_list = counter.most_common()
            most_id_list = [item[0] for item in most_list]
            str_most_id_list = ','.join(most_id_list)
            print(most_id_list)

            url_request_friends = 'https://graph.facebook.com/v2.8/?ids={ids}&' \
                                  'fields=cover,email,picture.width(500).height(500),name&' \
                                  'access_token={access_token}'.format(
                                    ids=str_most_id_list,
                                    access_token=access_token
                                )
            r = requests.get(url_request_friends)
            dict_friends_info = r.json()

            most_dict_list = []
            for most in most_list:
                id = most[0]
                for k in dict_friends_info:
                    if k == id and k != user_id:
                        most_dict_list.append({
                            'info': dict_friends_info[k],
                            'number': most[1],
                        })

            context = {
                'facebook_user_id': user_id,
                'access_token': access_token,
                'most_dict_list': most_dict_list
            }
            return render(request,
                          'projects/sns/facebook/friends_ranking.html',
                          context)

    elif request.method == 'POST':
        import ast
        user = request.user
        if not user.is_authenticated():
            access_token = request.POST['access_token']
            facebook_user_id = request.POST['facebook_user_id']
            user_info = facebook.get_user_info(facebook_user_id, access_token)
            user = authenticate(user_info=user_info)

        fr = FriendsRanking.objects.create(user=user)
        str_item_list = request.POST.getlist('item')
        for str_item in str_item_list:
            item = ast.literal_eval(str_item)
            facebook_user_id = item['info']['id']
            name = item['info']['name']
            url_profile = item['info']['picture']['data']['url']
            comment_count = item['number']
            FriendsRankingItem.objects.create(
                parent=fr,
                facebook_user_id=facebook_user_id,
                name=name,
                url_profile=url_profile,
                comment_count=comment_count)
        return redirect('sns:friends_ranking_saved', pk=fr.pk)
Ejemplo n.º 15
0
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)