Ejemplo n.º 1
0
def test_get_extended_access_token_v23_plus():
    mock_request.return_value.status_code = 200
    mock_request.return_value.content = (
        '{"access_token":"<extended access token>","token_type":"bearer"}'
    )

    access_token, expires_at = get_extended_access_token(
        '<access token>',
        '<application id>',
        '<application secret key>',
        api_version='2.3'
    )

    mock_request.assert_called_with(
        'GET',
        'https://graph.facebook.com/v2.3/oauth/access_token',
        allow_redirects=True,
        verify=True,
        timeout=None,
        params={
            'client_id': '<application id>',
            'client_secret': '<application secret key>',
            'grant_type': 'fb_exchange_token',
            'fb_exchange_token': '<access token>'
        }
    )

    assert_equal(access_token, '<extended access token>')
    assert not expires_at
Ejemplo n.º 2
0
def test_get_extended_access_token_no_expiry():
    mock_request.return_value.status_code = 200
    mock_request.return_value.content = 'access_token=<extended access token>'

    access_token, expires_at = get_extended_access_token(
        '<access token>',
        '<application id>',
        '<application secret key>'
    )

    mock_request.assert_called_with(
        'GET',
        'https://graph.facebook.com/oauth/access_token',
        allow_redirects=True,
        verify=True,
        timeout=None,
        params={
            'client_id': '<application id>',
            'client_secret': '<application secret key>',
            'grant_type': 'fb_exchange_token',
            'fb_exchange_token': '<access token>'
        }
    )

    assert_equal(access_token, '<extended access token>')
    assert expires_at is None
Ejemplo n.º 3
0
def test_get_extended_access_token_v23_plus():
    mock_request.return_value.status_code = 200
    mock_request.return_value.content = (
        '{"access_token":"<extended access token>","token_type":"bearer"}')

    access_token, expires_at = get_extended_access_token(
        '<access token>',
        '<application id>',
        '<application secret key>',
        api_version='2.3')

    mock_request.assert_called_with(
        'GET',
        'https://graph.facebook.com/v2.3/oauth/access_token',
        allow_redirects=True,
        verify=True,
        timeout=None,
        params={
            'client_id': '<application id>',
            'client_secret': '<application secret key>',
            'grant_type': 'fb_exchange_token',
            'fb_exchange_token': '<access token>'
        })

    assert_equal(access_token, '<extended access token>')
    assert not expires_at
Ejemplo n.º 4
0
def extend_access_token(saved_props, token, sublets_api_id,
                        sublets_secret_key):
    log("Extending access token", Color.BOLD)
    access_token, expires_at = facepy.get_extended_access_token(
        token, sublets_api_id, sublets_secret_key)
    new_token = access_token
    unixtime = time.mktime(expires_at.timetuple())
    print time.mktime(expires_at.timetuple())
    saved_props['sublets_oauth_access_token'] = new_token
    saved_props['access_token_expiration'] = unixtime
    log("Token extended", Color.BOLD)
Ejemplo n.º 5
0
def extend_access_token(saved_props, token, sublets_api_id,
                        sublets_secret_key):
    log("Extending access token", Color.BOLD)
    access_token, expires_at = facepy.get_extended_access_token(
        token,
        sublets_api_id,
        sublets_secret_key
    )
    new_token = access_token
    unixtime = time.mktime(expires_at.timetuple())
    print time.mktime(expires_at.timetuple())
    saved_props['sublets_oauth_access_token'] = new_token
    saved_props['access_token_expiration'] = unixtime
    log("Token extended", Color.BOLD)
Ejemplo n.º 6
0
    def validate(self, data):
        req = facepy.SignedRequest(data['signed_request'],
                                   settings.FACEBOOK_APP_SECRET_KEY,
                                   settings.FACEBOOK_APP_ID)

        # XXX handle facebook exceptions
        # for example, the user can refuse to share their email address

        graph = facepy.GraphAPI(req.user.oauth_token.token)
        data = graph.get('me?fields=email,first_name,last_name,third_party_id')

        extended_token = facepy.get_extended_access_token(
            req.user.oauth_token.token,
            settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET_KEY)

        user = User.objects.get_or_create_facebook_user(data, extended_token)[0]

        return {'user': user}
Ejemplo n.º 7
0
def test_get_extended_access_token_no_expiry():
    mock_request.return_value.status_code = 200
    mock_request.return_value.content = 'access_token=<extended access token>'

    access_token, expires_at = get_extended_access_token(
        '<access token>', '<application id>', '<application secret key>')

    mock_request.assert_called_with(
        'GET',
        'https://graph.facebook.com/oauth/access_token',
        allow_redirects=True,
        verify=True,
        timeout=None,
        params={
            'client_id': '<application id>',
            'client_secret': '<application secret key>',
            'grant_type': 'fb_exchange_token',
            'fb_exchange_token': '<access token>'
        })

    assert_equal(access_token, '<extended access token>')
    assert expires_at is None
Ejemplo n.º 8
0
def api_backend_login(request):
    if not request.is_ajax():
        return HttpResponse()

    # only take this shortcut for admin users
    if request.user.is_authenticated() and request.user.is_superuser:
        logger.debug(
            "Superuser already logged in: {0}".format(request.user.username))
        return HttpResponse()

    data = request.POST
    logger.debug("Post data: {0}".format(data));
    fbUid = data['fbUid']
    fbName = data['name']
    fbAccessToken = data['accessToken']
    logger.debug("Logging in with facebook: {0} - {1}".format(fbName, fbUid))

    fb_app_id = os.environ.get('FB_APP_ID', '')
    fb_app_secret_key = os.environ.get('FB_APP_SECRET_KEY', '')
    # get the accessToken, and verify with the app secret
    # convert it into a long term token (use app id and app secret)
    try:
        fbLongAccessToken, expires_at = facepy.get_extended_access_token(
            fbAccessToken, fb_app_id, fb_app_secret_key)
        logger.debug(
            "facebook access token valid: {0}, long token: {1}"
            "".format(fbAccessToken, fbLongAccessToken))
    except facepy.exceptions.OAuthError:
        logger.debug("facebook access token failed: {0}".format(fbAccessToken))
        reply = {"reply": "ERROR", "user": user.username}
        return HttpResponse(
            json.dumps(reply, sort_keys=True, separators=(',',':'), indent=4),
            content_type='application/json'
        )
    if fbUid:
        user = User.objects.filter(username=fbUid)
        if user:
            logger.debug("Logging in existing user: {0} - {1}".format(fbName, fbUid))
        elif user is not None:
            logger.debug("Logging in new user: {0} - {1}".format(fbName, fbUid))
            user = User.objects.create_user(
                username=fbUid,
                first_name = fbName,
                password='******')
            user.save()
        # we don't use password based authentication (dummy pw)
        user = authenticate(username=fbUid, password='******')
        if user is not None:
            login(request, user)
            fb_user = FacebookUser(
                user_id=user,
                fb_name = data.get('fbData[name]', 'fb_name'),
                fb_first_name = data.get('fbData[first_name]', 'fb_first_name'),
                fb_last_name = data.get('fbData[last_name]', 'fb_last_name'),
                fb_email = data.get('fbData[email]', 'fb_email'),
            )
            fb_user.save()
            user.email = fb_user.fb_email
            user.save()
        
    reply = {"reply": "OK", "user": user.username}
    return HttpResponse(
        json.dumps(reply, sort_keys=True, separators=(',',':'), indent=4),
        content_type='application/json'
    )
#!/usr/bin/env python3
# Extend user access token

import argparse
from facepy import get_extended_access_token

argParser = argparse.ArgumentParser()
argParser.add_argument('--appid', required=True)
argParser.add_argument('--appsecret', required=True)
argParser.add_argument('--token', required=True)
args = argParser.parse_args()

print(get_extended_access_token(args.token, args.appid, args.appsecret)[0])

Ejemplo n.º 10
0
def count_group_likes():
    fb_api_access_token = props.token
    fb_app_id = props.app_id
    fb_secret_key = props.secret_key

    # Counter object to do the counting for us
    total_likes_counter = Counter()
    top_liked_posts_counter = Counter()
    top_liked_comments_counter = Counter()
    total_posts_counter = Counter()
    total_comments_counter = Counter()
    most_discussed_counter = Counter()

    group_id = props.group_id
    num_of_items_to_return = props.num_of_items

    # Put the number of weeks you want it to increment by each time
    #   smaller is better, but too small and you could hit your rate limit
    #       ... which is 600 calls per 600 seconds. Maybe apps get more
    num_weeks = int("2")

    # Convert to unix time
    num_weeks_unix = num_weeks * 604800

    start_date = props.start_date

    datetime_start_date = datetime.datetime.fromtimestamp(start_date)

    # Query strings for FQL
    posts_query = \
        "SELECT post_id, like_info, actor_id, created_time FROM stream" + \
        " WHERE source_id=" + group_id + " AND created_time<"
    person_query = "SELECT first_name, last_name FROM user WHERE uid="

    # Authorize our API wrapper
    graph = facepy.GraphAPI(fb_api_access_token)

    # Code to programatically extend key
    if extend_key:
        access_token, expires_at = facepy.get_extended_access_token(
            fb_api_access_token,
            fb_app_id,
            fb_secret_key
        )

        # This will print out new extended token and new expiration date
        # Copy them and replace your token above with this one
        print 'New token: ' + access_token
        print 'New expiration date: ' + expires_at

    log('Getting group posts', Color.BLUE)

    # Send end time to current time and work backward
    end_time = int(time.time())

    # Or manually set end time
    # end_time = 1392422400

    log('Current date is: ' + datetime.datetime.fromtimestamp(
        end_time).strftime('%Y-%m-%d'))

    log('Incrementing by ' + str(num_weeks) + ' weeks at a time')

    # List of thread objects
    threads = []

    # Threadsafe queue for the threads to dump their data in
    final_queue = Queue()

    log("Initializing threads...", Color.BLUE)

    # While loop that creates the threads
    # Instantiates each thread with calculated time, keeps decrementing to
    # start
    while end_time > start_date:
        # New query
        new_query = posts_query + str(
            end_time) + " AND created_time>" + \
            str(end_time - num_weeks_unix) + " LIMIT 600"

        # Thread creation
        t = RequestThread(final_queue, fb_api_access_token, new_query,
                          end_time, num_weeks_unix)

        # Add it to our list
        threads.append(t)

        # Decrement the time
        end_time -= num_weeks_unix

        # Start the thread
        t.start()

    log("Joining threads...", Color.BLUE)

    # Wait for all the threads to finish before counting everything up
    for t in threads:
        t.join()

    log("Done, merging data...", Color.BLUE)

    # Count up all the data by merging all the counters from each thread result
    for stuff in list(final_queue.queue):
        total_likes_counter += stuff['t']
        top_liked_posts_counter += stuff['p']
        top_liked_comments_counter += stuff['c']
        total_posts_counter += stuff['tp']
        total_comments_counter += stuff['tc']
        most_discussed_counter += stuff['cc']

    most_active_day_counter = total_posts_counter + total_comments_counter

    # Returns key-value list of top <num_of_items_to_return> items
    most_common_people = total_likes_counter.most_common(
        num_of_items_to_return)

    top_posts = top_liked_posts_counter.most_common(num_of_items_to_return)

    top_comments = top_liked_comments_counter.most_common(
        num_of_items_to_return)

    total_posts = total_posts_counter.most_common(num_of_items_to_return)

    total_comments = total_comments_counter.most_common(num_of_items_to_return)

    most_active_days = most_active_day_counter.most_common(
        num_of_items_to_return)

    most_discussed = most_discussed_counter.most_common(num_of_items_to_return)

    top_people_stats = []
    # Iterate over top people and retrieve names from their ID's
    # Use enumerate to keep track of indices for rank numbers
    log('\nPeople Stats', Color.BOLD)
    log("* = Weighted average calc'd from user's first post date")
    for i, x in enumerate(most_common_people):
        person = graph.fql(person_query + str(x[0]))["data"][0]

        now = datetime.datetime.now()
        join_date = datetime.datetime.fromtimestamp(appeared[x[0]])
        diff1 = now - datetime_start_date
        diff2 = now - join_date

        avg = x[1] / (diff1.total_seconds() / 60 / 60 / 24 / 7)
        weighted_avg = x[1] / (diff2.total_seconds() / 60 / 60 / 24 / 7)

        top_people_stats.append({
            "name": person['first_name'] + " " + person['last_name'],
            "likes": x[1],
            "avg": avg,
            "augmented_avg": weighted_avg,
            "first": int(
                (join_date - datetime.datetime(1970, 1, 1)).total_seconds())
        })

        print '#' + str(i + 1) + '. ' + person['first_name'] + " " + person[
            'last_name']
        print '-- Likes: ' + str(x[1])
        print '-- Weekly average: ' + str(avg)
        print '-- Weekly average*: ' + str(weighted_avg)
        print '-- First post: ' + join_date.strftime('%Y-%m-%d')

    # Iterate over top posts and get info
    log('\nTop posts!', Color.BOLD)
    for x in top_posts:
        post = graph.get(str(x[0]))
        s = str(x[1]) + " - " + post['from']['name'] + " - " + post['type']
        print s
        if 'message' in post:
            m = str(post['message'].encode('ascii', 'ignore')).replace('\n',
                                                                       ' ')
            if len(m) > 70:
                print '-- ' + m[0:70] + "..."
            else:
                print '-- ' + m
        print '-- http://www.facebook.com/' + post['id']

    # Iterate over top comments and get info
    log('\nTop comments!', Color.BOLD)
    for x in top_comments:
        comment = graph.get(str(x[0]))
        s = str(x[1]) + " - " + comment['from']['name']
        print s
        if 'message' in comment:
            c = str(comment['message'].encode('ascii', 'ignore')).replace('\n',
                                                                          ' ')
            if len(c) > 70:
                print '-- ' + c[0:70] + "..."
            else:
                print '-- ' + c
        print '-- http://www.facebook.com/' + comment['id']

    # Iterate over total posts/comments and calculate info
    log('\nMost active days (by number of posts and comments)', Color.BOLD)
    for x in most_active_days:
        d = datetime.datetime.fromtimestamp(float(x[0])).strftime('%m/%d/%Y')
        print str(x[1]) + " - " + d

    # Iterate over total posts and calculate info
    log('\nMost active days (by number of posts)', Color.BOLD)
    for x in total_posts:
        d = datetime.datetime.fromtimestamp(float(x[0])).strftime('%m/%d/%Y')
        print str(x[1]) + " - " + d

    # Iterate over total comments and calculate info
    log('\nMost active days (by number of comments)', Color.BOLD)
    for x in total_comments:
        d = datetime.datetime.fromtimestamp(float(x[0])).strftime('%m/%d/%Y')
        print str(x[1]) + " - " + d

    # Iterate over top posts and get info
    log('\nMost discussed', Color.BOLD)
    for x in most_discussed:
        post = graph.get(str(x[0]))
        s = str(x[1]) + " - " + post['from']['name'] + " - " + post['type']
        print s
        if 'message' in post:
            m = str(post['message'].encode('ascii', 'ignore')).replace('\n',
                                                                       ' ')
            if len(m) > 70:
                print '-- ' + m[0:70] + "..."
            else:
                print '-- ' + m
        print '-- http://www.facebook.com/' + post['id']

    log('\nExporting...', Color.BLUE)
    dataDict = json.dumps({"top_people_stats": top_people_stats,
                           "top_liked_posts_counter": top_liked_posts_counter,
                           "top_liked_comments_counter": top_liked_comments_counter,
                           "total_posts_counter": total_posts_counter,
                           "total_comments_counter": total_comments_counter,
                           "most_active_day_counter": most_active_day_counter,
                           "most_common_people": most_common_people,
                           "top_posts": top_posts,
                           "top_comments": top_comments,
                           "total_posts": total_posts,
                           "total_comments": total_comments,
                           "most_active_days": most_active_days})

    exportData(dataDict)
def count_group_likes():

    # Only necessary if you want to get an extended access token
    # You'll have to make a facebook app and generate a token with it
    # You'll also need to get the following two values from it
    fb_app_id = "id_goes_here"
    fb_secret_key = "key_goes_here"

    # Counter object to do the counting for us
    total_likes_counter = Counter()
    top_liked_posts_counter = Counter()
    top_liked_comments_counter = Counter()
    total_posts_counter = Counter()
    total_comments_counter = Counter()
    most_discussed_counter = Counter()

    group_id = "155607091223285"  # Unique ID of the group to search.

    # Put the number of weeks you want it to increment by each time
    #   smaller is better, but too small and you could hit your rate limit
    #       ... which is 600 calls per 600 seconds. Maybe apps get more
    num_weeks = int("2")

    # Convert to unix time
    num_weeks_unix = num_weeks * 604800

    # Start date, in unix time (our group was made 2/13/12) 1329091200
    # You can use this to convert: http://goo.gl/4QMFbW
    start_date = int("1329091200")

    datetime_start_date = datetime.datetime.fromtimestamp(start_date)

    # Query strings for FQL
    posts_query = \
        "SELECT post_id, like_info, actor_id, created_time FROM stream" + \
        " WHERE source_id=" + group_id + " AND created_time<"
    person_query = "SELECT first_name, middle_name, last_name FROM user WHERE uid="

    # Authorize our API wrapper
    graph = facepy.GraphAPI(fb_API_access_token)

    # Code to programatically extend key
    if extend_key:
        access_token, expires_at = facepy.get_extended_access_token(
            fb_API_access_token, fb_app_id, fb_secret_key)

        # This will print out new extended token and new expiration date
        # Copy them and replace your token above with this one
        print 'New token: ' + access_token
        print 'New expiration date: ' + expires_at

    log('Getting group posts', Color.BLUE)

    # Send end time to current time and work backward
    end_time = int(time.time())

    # Or manually set end time
    # end_time = 1392422400

    log('Current date is: ' +
        datetime.datetime.fromtimestamp(end_time).strftime('%Y-%m-%d'))

    log('Incrementing by ' + str(num_weeks) + ' weeks at a time')

    # List of thread objects
    threads = []

    # Threadsafe queue for the threads to dump their data in
    final_queue = Queue()

    log("Initializing threads...", Color.BLUE)

    # While loop that creates the threads
    # Instantiates each thread with calculated time, keeps decrementing to
    # start
    while end_time > start_date:
        # New query
        new_query = posts_query + str(
            end_time) + " AND created_time>" + \
            str(end_time - num_weeks_unix) + " LIMIT 2000"

        # Thread creation
        t = RequestThread(final_queue, fb_API_access_token, new_query,
                          end_time, num_weeks_unix)

        # Add it to our list
        threads.append(t)

        # Decrement the time
        end_time -= num_weeks_unix

        # Start the thread
        t.start()

    log("Joining threads...", Color.BLUE)

    # Wait for all the threads to finish before counting everything up
    for t in threads:
        t.join()

    log("Done, merging data...", Color.BLUE)

    # Count up all the data by merging all the counters from each thread result
    for stuff in list(final_queue.queue):
        total_likes_counter += stuff['t']
        top_liked_posts_counter += stuff['p']
        top_liked_comments_counter += stuff['c']
        total_posts_counter += stuff['tp']
        total_comments_counter += stuff['tc']
        most_discussed_counter += stuff['cc']

    most_active_day_counter = total_posts_counter + total_comments_counter

    # Returns key-value list of top <num_of_items_to_return> items
    num_of_items_to_return = 2000
    most_common_people = total_likes_counter.most_common(
        num_of_items_to_return)
    total_comments = total_comments_counter.most_common(num_of_items_to_return)

    top_people_stats = []
    # Iterate over top people and retrieve names from their ID's
    # Use enumerate to keep track of indices for rank numbers

    schema = [
        "rank", "name", "likes", "likesPerWeek", "comments", "commentsPerWeek",
        "likesPerComment", "posts", "firstPost"
    ]

    standings_file = open('../likes.json', 'w')
    standings_file.write("[\n")
    for i, x in enumerate(most_common_people):
        person = graph.fql(person_query + str(x[0]))["data"][0]

        now = datetime.datetime.now()
        join_date = datetime.datetime.fromtimestamp(appeared[x[0]])
        diff1 = now - datetime_start_date
        diff2 = now - join_date

        avg = x[1] / (diff1.total_seconds() / 60 / 60 / 24 / 7)
        weighted_avg = x[1] / (diff2.total_seconds() / 60 / 60 / 24 / 7)
        comments_per_week = total_comments_counter[x[0]] / (
            diff2.total_seconds() / 60 / 60 / 24 / 7)

        top_people_stats.append({
            "name":
            person['first_name'] + " " + person['last_name'],
            "likes":
            x[1],
            "avg":
            avg,
            "augmented_avg":
            weighted_avg,
            "first":
            int((join_date - datetime.datetime(1970, 1, 1)).total_seconds())
        })

        fullName = ""
        if person['middle_name'] is None:
            fullName = person['first_name'] + " " + person['last_name']
        elif (len(person['middle_name']) > 0):
            fullName = person['first_name'] + " " + person[
                'middle_name'] + " " + person['last_name']
        else:
            fullName = person['first_name'] + " " + person['last_name']

        likesPerComment = 0
        if total_comments_counter[x[0]] > 0:
            likesPerComment = (1.0 * x[1]) / float(
                total_comments_counter[x[0]])

        rank = i + 1
        dp = DataPoint(i + 1, fullName, x[1], weighted_avg,
                       total_comments_counter[x[0]], comments_per_week,
                       likesPerComment, total_posts_counter[x[0]],
                       join_date.strftime('%Y-%m-%d'))

        try:
            print str(dp).encode('utf8')
            standings_file.write("{\n")
            standings_file.write(str(dp).encode('utf8'))
            standings_file.write("},\n")
        except UnicodeEncodeError:
            pass

    standings_file.write("\n]")
    standings_file.close()
Ejemplo n.º 12
0
 def _genLL(self, token, appId, appSecret):
     try:
         return get_extended_access_token(token, appId, appSecret)
     except Exception as e:
         self.print(e)
Ejemplo n.º 13
0
def count_group_likes():
    fb_api_access_token = props.token
    fb_app_id = props.app_id
    fb_secret_key = props.secret_key

    # Counter object to do the counting for us
    total_likes_counter = Counter()
    top_liked_posts_counter = Counter()
    top_liked_comments_counter = Counter()
    total_posts_counter = Counter()
    total_comments_counter = Counter()
    most_discussed_counter = Counter()

    group_id = props.group_id
    num_of_items_to_return = props.num_of_items

    # Put the number of weeks you want it to increment by each time
    #   smaller is better, but too small and you could hit your rate limit
    #       ... which is 600 calls per 600 seconds. Maybe apps get more
    num_weeks = int("2")

    # Convert to unix time
    num_weeks_unix = num_weeks * 604800

    start_date = props.start_date

    datetime_start_date = datetime.datetime.fromtimestamp(start_date)

    # Query strings for FQL
    posts_query = \
        "SELECT post_id, like_info, actor_id, created_time FROM stream" + \
        " WHERE source_id=" + group_id + " AND created_time<"
    person_query = "SELECT first_name, last_name FROM user WHERE uid="

    # Authorize our API wrapper
    graph = facepy.GraphAPI(fb_api_access_token)

    # Code to programatically extend key
    if extend_key:
        access_token, expires_at = facepy.get_extended_access_token(
            fb_api_access_token, fb_app_id, fb_secret_key)

        # This will print out new extended token and new expiration date
        # Copy them and replace your token above with this one
        print 'New token: ' + access_token
        print 'New expiration date: ' + expires_at

    log('Getting group posts', Color.BLUE)

    # Send end time to current time and work backward
    end_time = int(time.time())

    # Or manually set end time
    # end_time = 1392422400

    log('Current date is: ' +
        datetime.datetime.fromtimestamp(end_time).strftime('%Y-%m-%d'))

    log('Incrementing by ' + str(num_weeks) + ' weeks at a time')

    # List of thread objects
    threads = []

    # Threadsafe queue for the threads to dump their data in
    final_queue = Queue()

    log("Initializing threads...", Color.BLUE)

    # While loop that creates the threads
    # Instantiates each thread with calculated time, keeps decrementing to
    # start
    while end_time > start_date:
        # New query
        new_query = posts_query + str(
            end_time) + " AND created_time>" + \
            str(end_time - num_weeks_unix) + " LIMIT 600"

        # Thread creation
        t = RequestThread(final_queue, fb_api_access_token, new_query,
                          end_time, num_weeks_unix)

        # Add it to our list
        threads.append(t)

        # Decrement the time
        end_time -= num_weeks_unix

        # Start the thread
        t.start()

    log("Joining threads...", Color.BLUE)

    # Wait for all the threads to finish before counting everything up
    for t in threads:
        t.join()

    log("Done, merging data...", Color.BLUE)

    # Count up all the data by merging all the counters from each thread result
    for stuff in list(final_queue.queue):
        total_likes_counter += stuff['t']
        top_liked_posts_counter += stuff['p']
        top_liked_comments_counter += stuff['c']
        total_posts_counter += stuff['tp']
        total_comments_counter += stuff['tc']
        most_discussed_counter += stuff['cc']

    most_active_day_counter = total_posts_counter + total_comments_counter

    # Returns key-value list of top <num_of_items_to_return> items
    most_common_people = total_likes_counter.most_common(
        num_of_items_to_return)

    top_posts = top_liked_posts_counter.most_common(num_of_items_to_return)

    top_comments = top_liked_comments_counter.most_common(
        num_of_items_to_return)

    total_posts = total_posts_counter.most_common(num_of_items_to_return)

    total_comments = total_comments_counter.most_common(num_of_items_to_return)

    most_active_days = most_active_day_counter.most_common(
        num_of_items_to_return)

    most_discussed = most_discussed_counter.most_common(num_of_items_to_return)

    top_people_stats = []
    # Iterate over top people and retrieve names from their ID's
    # Use enumerate to keep track of indices for rank numbers
    log('\nPeople Stats', Color.BOLD)
    log("* = Weighted average calc'd from user's first post date")
    for i, x in enumerate(most_common_people):
        person = graph.fql(person_query + str(x[0]))["data"][0]

        now = datetime.datetime.now()
        join_date = datetime.datetime.fromtimestamp(appeared[x[0]])
        diff1 = now - datetime_start_date
        diff2 = now - join_date

        avg = x[1] / (diff1.total_seconds() / 60 / 60 / 24 / 7)
        weighted_avg = x[1] / (diff2.total_seconds() / 60 / 60 / 24 / 7)

        top_people_stats.append({
            "name":
            person['first_name'] + " " + person['last_name'],
            "likes":
            x[1],
            "avg":
            avg,
            "augmented_avg":
            weighted_avg,
            "first":
            int((join_date - datetime.datetime(1970, 1, 1)).total_seconds())
        })

        print '#' + str(
            i + 1) + '. ' + person['first_name'] + " " + person['last_name']
        print '-- Likes: ' + str(x[1])
        print '-- Weekly average: ' + str(avg)
        print '-- Weekly average*: ' + str(weighted_avg)
        print '-- First post: ' + join_date.strftime('%Y-%m-%d')

    # Iterate over top posts and get info
    log('\nTop posts!', Color.BOLD)
    for x in top_posts:
        post = graph.get(str(x[0]))
        s = str(x[1]) + " - " + post['from']['name'] + " - " + post['type']
        print s
        if 'message' in post:
            m = str(post['message'].encode('ascii',
                                           'ignore')).replace('\n', ' ')
            if len(m) > 70:
                print '-- ' + m[0:70] + "..."
            else:
                print '-- ' + m
        print '-- http://www.facebook.com/' + post['id']

    # Iterate over top comments and get info
    log('\nTop comments!', Color.BOLD)
    for x in top_comments:
        comment = graph.get(str(x[0]))
        s = str(x[1]) + " - " + comment['from']['name']
        print s
        if 'message' in comment:
            c = str(comment['message'].encode('ascii',
                                              'ignore')).replace('\n', ' ')
            if len(c) > 70:
                print '-- ' + c[0:70] + "..."
            else:
                print '-- ' + c
        print '-- http://www.facebook.com/' + comment['id']

    # Iterate over total posts/comments and calculate info
    log('\nMost active days (by number of posts and comments)', Color.BOLD)
    for x in most_active_days:
        d = datetime.datetime.fromtimestamp(float(x[0])).strftime('%m/%d/%Y')
        print str(x[1]) + " - " + d

    # Iterate over total posts and calculate info
    log('\nMost active days (by number of posts)', Color.BOLD)
    for x in total_posts:
        d = datetime.datetime.fromtimestamp(float(x[0])).strftime('%m/%d/%Y')
        print str(x[1]) + " - " + d

    # Iterate over total comments and calculate info
    log('\nMost active days (by number of comments)', Color.BOLD)
    for x in total_comments:
        d = datetime.datetime.fromtimestamp(float(x[0])).strftime('%m/%d/%Y')
        print str(x[1]) + " - " + d

    # Iterate over top posts and get info
    log('\nMost discussed', Color.BOLD)
    for x in most_discussed:
        post = graph.get(str(x[0]))
        s = str(x[1]) + " - " + post['from']['name'] + " - " + post['type']
        print s
        if 'message' in post:
            m = str(post['message'].encode('ascii',
                                           'ignore')).replace('\n', ' ')
            if len(m) > 70:
                print '-- ' + m[0:70] + "..."
            else:
                print '-- ' + m
        print '-- http://www.facebook.com/' + post['id']

    log('\nExporting...', Color.BLUE)
    dataDict = json.dumps({
        "top_people_stats": top_people_stats,
        "top_liked_posts_counter": top_liked_posts_counter,
        "top_liked_comments_counter": top_liked_comments_counter,
        "total_posts_counter": total_posts_counter,
        "total_comments_counter": total_comments_counter,
        "most_active_day_counter": most_active_day_counter,
        "most_common_people": most_common_people,
        "top_posts": top_posts,
        "top_comments": top_comments,
        "total_posts": total_posts,
        "total_comments": total_comments,
        "most_active_days": most_active_days
    })

    exportData(dataDict)
Ejemplo n.º 14
0
def count_group_likes():

    # Only necessary if you want to get an extended access token
    # You'll have to make a facebook app and generate a token with it
    # You'll also need to get the following two values from it
    fb_app_id = "id_goes_here"
    fb_secret_key = "key_goes_here"

    # Counter object to do the counting for us
    total_likes_counter = Counter()
    top_liked_posts_counter = Counter()
    top_liked_comments_counter = Counter()
    total_posts_counter = Counter()
    total_comments_counter = Counter()
    most_discussed_counter = Counter()

    group_id = "155607091223285"  # Unique ID of the group to search.

    # Put the number of weeks you want it to increment by each time
    #   smaller is better, but too small and you could hit your rate limit
    #       ... which is 600 calls per 600 seconds. Maybe apps get more
    num_weeks = int("2")

    # Convert to unix time
    num_weeks_unix = num_weeks * 604800

    # Start date, in unix time (our group was made 2/13/12) 1329091200
    # You can use this to convert: http://goo.gl/4QMFbW
    start_date = int("1329091200")
    
    datetime_start_date = datetime.datetime.fromtimestamp(start_date)

    # Query strings for FQL
    posts_query = \
        "SELECT post_id, like_info, actor_id, created_time FROM stream" + \
        " WHERE source_id=" + group_id + " AND created_time<"
    person_query = "SELECT first_name, middle_name, last_name FROM user WHERE uid="

    # Authorize our API wrapper
    graph = facepy.GraphAPI(fb_API_access_token)

    # Code to programatically extend key
    if extend_key:
        access_token, expires_at = facepy.get_extended_access_token(
            fb_API_access_token,
            fb_app_id,
            fb_secret_key
        )

        # This will print out new extended token and new expiration date
        # Copy them and replace your token above with this one
        print 'New token: ' + access_token
        print 'New expiration date: ' + expires_at

    log('Getting group posts', Color.BLUE)

    # Send end time to current time and work backward
    end_time = int(time.time())

    # Or manually set end time
    # end_time = 1392422400

    log('Current date is: ' + datetime.datetime.fromtimestamp(
        end_time).strftime('%Y-%m-%d'))

    log('Incrementing by ' + str(num_weeks) + ' weeks at a time')

    # List of thread objects
    threads = []

    # Threadsafe queue for the threads to dump their data in
    final_queue = Queue()

    log("Initializing threads...", Color.BLUE)

    # While loop that creates the threads
    # Instantiates each thread with calculated time, keeps decrementing to
    # start
    while end_time > start_date:
        # New query
        new_query = posts_query + str(
            end_time) + " AND created_time>" + \
            str(end_time - num_weeks_unix) + " LIMIT 2000"

        # Thread creation
        t = RequestThread(final_queue, fb_API_access_token, new_query,
                          end_time, num_weeks_unix)

        # Add it to our list
        threads.append(t)

        # Decrement the time
        end_time -= num_weeks_unix

        # Start the thread
        t.start()

    log("Joining threads...", Color.BLUE)

    # Wait for all the threads to finish before counting everything up
    for t in threads:
        t.join()

    log("Done, merging data...", Color.BLUE)

    # Count up all the data by merging all the counters from each thread result
    for stuff in list(final_queue.queue):
        total_likes_counter += stuff['t']
        top_liked_posts_counter += stuff['p']
        top_liked_comments_counter += stuff['c']
        total_posts_counter += stuff['tp']
        total_comments_counter += stuff['tc']
        most_discussed_counter += stuff['cc']

    most_active_day_counter = total_posts_counter + total_comments_counter

    # Returns key-value list of top <num_of_items_to_return> items
    num_of_items_to_return = 2000
    most_common_people = total_likes_counter.most_common(num_of_items_to_return)
    total_comments = total_comments_counter.most_common(num_of_items_to_return)


    top_people_stats = []
    # Iterate over top people and retrieve names from their ID's
    # Use enumerate to keep track of indices for rank numbers

    schema = ["rank","name","likes","likesPerWeek","comments","commentsPerWeek","likesPerComment","posts","firstPost"]

    standings_file = open('../likes.json', 'w')
    standings_file.write("[\n")
    for i, x in enumerate(most_common_people):
        person = graph.fql(person_query + str(x[0]))["data"][0]

        now = datetime.datetime.now()
        join_date = datetime.datetime.fromtimestamp(appeared[x[0]])
        diff1 = now - datetime_start_date
        diff2 = now - join_date

        avg = x[1] / (diff1.total_seconds() / 60 / 60 / 24 / 7)
        weighted_avg = x[1] / (diff2.total_seconds() / 60 / 60 / 24 / 7)
        comments_per_week = total_comments_counter[x[0]] / (diff2.total_seconds() / 60 / 60 / 24 / 7)

        top_people_stats.append({
            "name": person['first_name'] + " " + person['last_name'],
            "likes": x[1],
            "avg": avg,
            "augmented_avg": weighted_avg,
            "first": int(
                (join_date - datetime.datetime(1970, 1, 1)).total_seconds())
        })

		
        fullName = ""
        if person['middle_name'] is None:
            fullName = person['first_name'] + " " + person['last_name']
        elif (len(person['middle_name']) > 0):
            fullName = person['first_name'] + " " + person['middle_name'] + " " + person['last_name']
        else:
            fullName = person['first_name'] + " " + person['last_name']

        likesPerComment = 0
        if total_comments_counter[x[0]] > 0:
            likesPerComment = (1.0*x[1])/float(total_comments_counter[x[0]])

        rank = i+1
        dp = DataPoint(i+1,fullName,x[1],weighted_avg,total_comments_counter[x[0]],comments_per_week,likesPerComment,total_posts_counter[x[0]],join_date.strftime('%Y-%m-%d'))

        try:
            print str(dp).encode('utf8')
            standings_file.write("{\n")
            standings_file.write(str(dp).encode('utf8'))
            standings_file.write("},\n")
        except UnicodeEncodeError:
        	pass

    standings_file.write("\n]")
    standings_file.close()
Ejemplo n.º 15
0
    def post(self, request, *args, **kwargs):
        body = json.loads(request.body)
        api_type = body.get('type', None)
        email = None
        # expect social media outlet (facebook, linkedin, etc) and access token
        if (api_type == 'facebook'):
            # TODO for facebook, get extended access token and save that instead
            access_token, expires_at = get_extended_access_token(body.get('access_token', None), settings.FACEBOOK_APP_ID, settings.FACEBOOK_API_SECRET)
            graph = GraphAPI(access_token)
            api_user = graph.get('/me')
            # TODO check for existing user profile
            try:
                user = User.objects.get(email = api_user.get('email', None))
                is_new = False
            except User.DoesNotExist:
                is_new = True
                print 'new user {0}'.format(api_user.get('email'))
                arg_user = {
                    'email': api_user.get('email'),
                    'username': api_user.get('email'),
                    'first_name': api_user.get('first_name'),
                    'last_name': api_user.get('last_name'),
                    'password': None
                }
                #user = User.objects.create_user(api_user.get('email'), api_user.get('email'), None)
                user = User.objects.create_user(**arg_user)

            user.first_name = api_user.get('first_name')
            user.last_name = api_user.get('last_name')
            user.save()

            profile = user.profile
            profile.avatar_url = 'http://graph.facebook.com/{0}/picture'.format(api_user.get('id'))
            profile.blurb = api_user.get('quotes', None)
            profile.save()

            # TODO generate access token or get existing
            arg_access_token = {
                'access_token': access_token, # TODO don't use facebook's access token
                'expires': time.mktime(expires_at.timetuple())  # TODO don't use facebook's expiry
            }
            try:
                user_access_token = UserAccessToken.objects.get(user = user)
                is_new_access_token = False
            except UserAccessToken.DoesNotExist:
                user_access_token = UserAccessToken.objects.create(user = user, **arg_access_token)
                is_new_access_token = True
                
            # TODO update access token
            print 'user {0} exists'.format(user.email)
        else:
            return HttpResponse('{}', mimetype="application/json")
            
        response = {
            'access_token': user_access_token.access_token,
            'expires': user_access_token.expires,
            'user': {
                'is_new': is_new,
                'id': user.id,
                'first_name': user.first_name,
                'last_name': user.last_name,
                'avatar_url': profile.avatar_url,
                'blurb': profile.blurb
            }
        }
        return HttpResponse(json.dumps(response), mimetype="application/json")