Exemplo n.º 1
0
def sync():
    global recent_synced_time
    destpageID = config['destpage']
    graph = facebook.GraphAPI(config['access_token'])
    getpostid = lambda x: int(x['id'].split('_')[1])
    gettime = lambda x: datetime.strptime(x[:-5], '%Y-%m-%dT%H:%M:%S')

    for page in config['pages']:
        sourcepage = page['from']
        arguments = {'limit': 2, 'fields': 'link,name,picture,description,message,created_time'}
        response = graph.get_connections(sourcepage, 'feed', **arguments)

        total_posts = 0
        posts = response['data']
        last_synced_time = posts[0]['created_time']
        last_synced_post = getpostid(posts[0])
        posts.reverse()

        # First run, get the last post time from destination page
        if recent_synced_time == 0:
            arguments = {'limit': 1, 'fields': 'created_time'}
            response = graph.get_connections(destpageID, 'feed', **arguments)
            recent_synced_time = gettime(response['data'][0]['created_time'])

        for post in posts:
            if gettime(post['created_time']) <= recent_synced_time:
                continue

            # if postid is newer than last sycned share it
            message = ''
            if post.has_key('message'):
                message = converter.zg12uni51(post['message']).encode('utf8')

            if post.has_key('description'):
                post['description'] = converter.zg12uni51(post['description']).encode('utf8')

            if not post.has_key('link'):
                post['link'] = 'https://www.facebook.com/%s/posts/%s' %(destpageID, getpostid(post))

            if post.has_key('name'):
                post['name'] = converter.zg12uni51(post['name']).encode('utf8')
            else:
                post['name'] = page['name']

            thepost = post
            total_posts += 1
            del thepost['id']
            if thepost.has_key('message'): del thepost['message']
            graph.put_wall_post(message=message, attachment=post)

            logging.info ("Synced %d posts for %s." %(total_posts, page['name']))
            page['last_synced_post'] = last_synced_post
            page['last_synced_time'] = last_synced_time

    recent_synced_time = datetime.now()
    return 'ok', 200
Exemplo n.º 2
0
def processFacebookPageFeedStatus(status):
    
    # The status is now a Python dictionary, so for top-level items,
    # we can simply call the key.
    
    # Additionally, some items may not always exist,
    # so must check for existence first
    try: 
        status_id = status['id']
        status_message = '' if 'message' not in status.keys() else zg12uni51(status['message']).encode('utf-8').replace('"', r'\"')
        #status_message = '' if 'message' not in status.keys() else status['message'].encode('utf-8')
        link_name = '' if 'name' not in status.keys() else status['name'].encode('utf-8')
        status_type = status['type']
        status_link = '' if 'link' not in status.keys() else status['link'].encode('utf-8')
        
        
        # Time needs special care since a) it's in UTC and
        # b) it's not easy to use in statistical programs.
        
        status_published = datetime.datetime.strptime(status['created_time'],'%Y-%m-%dT%H:%M:%S+0000')
        status_published = status_published + datetime.timedelta(hours=-5) # EST
        status_published = status_published.strftime('%Y-%m-%d %H:%M:%S') # best time format for spreadsheet programs
        
        # Nested items require chaining dictionary keys.
        #print status['likes']
        #num_likes = 0 if 'likes' not in status.keys() else status['likes']['summary']['total_count']

        num_likes = 0
        if 'likes' in status.keys(): 
            if 'summary' in status['likes']: 
                if 'total_count' in status['likes']['summary']: 
                    num_likes = status['likes']['summary']['total_count']
        num_reactions = 0
        if 'reactions' in status.keys(): 
            if 'summary' in status['reactions']: 
                if 'total_count' in status['reactions']['summary']: 
                    num_reactions = status['reactions']['summary']['total_count']
        num_comments = 0
        if 'comments' in status.keys(): 
            if 'summary' in status['comments']: 
                if 'total_count' in status['comments']['summary']: 
                    num_comments = status['comments']['summary']['total_count']
        #comment_1 = "" if 'comments' not in status.keys() else status['comments']['data'][0]['message']
        #comment_2 = "" if 'comments' not in status.keys() else status['comments']['data'][1]['message']
        num_shares = 0 if 'shares' not in status.keys() else status['shares']['count']
    except Exception, e:
        print e
        print "Error for status %s" % (status)
Exemplo n.º 3
0
def sync():
    graph = facebook.GraphAPI(config['user_access_token'])
    resp = graph.get_object('me/accounts')

    # For each page that the user manage
    for page in resp['data']:
        pageid = int(page['id'])

        # if the page is configured to sync
        if config['pages'].has_key(pageid):
            page_access_token = page['access_token']
            graph = facebook.GraphAPI(page_access_token)
            getpostid = lambda x: int(x['id'].split('_')[1])
            gettime = lambda x: datetime.strptime(x[:-5], '%Y-%m-%dT%H:%M:%S')

            thispage = config['pages'][pageid]
            sourcepage = str(thispage['from'])
            arguments = {'limit': 5, 'fields': 'link,name,picture,description,message,created_time'}
            response = graph.get_connections(sourcepage, 'feed', **arguments)

            total_posts = 0
            posts = response['data']
            last_synced_time = posts[0]['created_time']
            last_synced_post = getpostid(posts[0])
            posts.reverse()

            # First run, get the last post time from destination page
            if thispage['last_synced_time'] == 0:
                arguments = {'limit': 1, 'fields': 'created_time'}
                response = graph.get_connections(page['id'], 'feed', **arguments)
                thispage['last_synced_time'] = response['data'][0]['created_time']

            for post in posts:
                if gettime(post['created_time']) <= gettime(thispage['last_synced_time']):
                    continue

                # if postid is newer than last sycned share it
                message = converter.zg12uni51(post['message'])
                message = message.encode('utf8')

                if post.has_key('description'):
                    post['description'] = converter.zg12uni51(post['description']).encode('utf8')

                if not post.has_key('link'):
                    post['link'] = 'https://www.facebook.com/%s/posts/%s' %(pageid, getpostid(post))

                if post.has_key('name'):
                    post['name'] = converter.zg12uni51(post['name']).encode('utf8')
                else:
                    post['name'] = thispage['name']

                del post['id']
                del post['message']
                total_posts += 1

                graph.put_wall_post(message=message, attachment=post)

            logging.info ("Synced %d posts for %s." %(total_posts, thispage['name']))
            thispage['last_synced_post'] = last_synced_post
            thispage['last_synced_time'] = last_synced_time

    global recent_synced_time
    recent_synced_time = datetime.now()
    return 'ok', 200
def toUnicode(text):
    return (zg12uni51(input_text=text))