讀取臉書貼文內容

程式列表

import sys
import fbconsole

# 登入 Facebook
def FB_Login():
    # 成功登入後顯示
    fbconsole.AUTH_SUCCESS_HTML = '<h3>請關閉視窗</h3>'
    
    # 指派 Facebook 權限
    fbconsole.AUTH_SCOPE = ['read_friendlists', 'read_stream', 'publish_checkins', 'publish_actions', 'manage_pages']
    
    # 第 1 次出現登入 Facebook 畫面
    # 之後出現成功登入畫面
    fbconsole.authenticate()

# 讀取訊息
def FB_Posts(who, numOfPosts):
    totalRecord = 0
    for post in fbconsole.iter_pages(fbconsole.get('/' + who + '/posts')):
        if post.has_key('message'):
            message_owner_id = post['from']['id']
            message = post['message']
            print '[%s]\t[%s]\t[%s]' % (post['id'], post['created_time'], message)
            
            if post['comments'].has_key('data'):
                for comment in post['comments']['data']:
                    if comment['from']['id']==message_owner_id:
                        message = comment['message']
                        print '[%s]\t[%s]\t[%s]' % (comment['id'], comment['created_time'], message)
                    
            totalRecord = totalRecord + 1
            if totalRecord == numOfPosts:
                break
            
    return totalRecord
  
# 登出 Facebook  
def FB_Logout():    
    fbconsole.logout()

if __name__=="__main__":
    fb_who = 'me'
    fb_numOfPost = 20
    
    if len(sys.argv) > 1:
        fb_who = sys.argv[1]
        
    if len(sys.argv) > 2:
        fb_numOfPost = int(sys.argv[2])
        
    FB_Login()
    totalRecord = FB_Posts(fb_who, fb_numOfPost)
    FB_Logout()
    
    print '總共:%d' % (totalRecord)

import sys
import fbconsole
from datetime import datetime

# 登入 Facebook
def FB_Login():
    # 成功登入後顯示
    fbconsole.AUTH_SUCCESS_HTML = '<h3>請關閉視窗</h3>'
    
    # 指派 Facebook 權限
    fbconsole.AUTH_SCOPE = ['read_friendlists', 'read_stream', 'publish_checkins', 'publish_actions', 'manage_pages']
    
    # 第 1 次出現登入 Facebook 畫面
    # 之後出現成功登入畫面
    fbconsole.authenticate()

# 讀取訊息
def FB_Posts(who, numOfPosts):
    totalRecord = 0
    for post in fbconsole.iter_pages(fbconsole.get('/' + who + '/posts')):
        if post.has_key('message'):
            message_owner_id = post['from']['id']
            message = post['message']
            print '[%s]\t[%s]\t[%s]' % (post['id'], post['created_time'], message)
            
            if post['comments'].has_key('data'):
                for comment in post['comments']['data']:
                    if comment['from']['id']==message_owner_id:
                        message = comment['message']
                        print '[%s]\t[%s]\t[%s]' % (comment['id'], comment['created_time'], message)
                    
            totalRecord = totalRecord + 1
            if totalRecord == numOfPosts:
                break
            
    return totalRecord
  
# 讀取訊息
def FB_Posts(who, dateFrom):
    totalRecord = 0
    for post in fbconsole.iter_pages(fbconsole.get('/' + who + '/posts')):
        if post.has_key('message'):
            message_owner_id = post['from']['id']
            message = post['message']
            
            create_date = datetime.strptime(post['created_time'][:10], '%Y-%m-%d')
            if create_date < dateFrom:
                break

            print '[%s]\t[%s]\t[%s]' % (post['id'], post['created_time'], message)
                
            if post['comments'].has_key('data'):
                for comment in post['comments']['data']:
                    if comment['from']['id']==message_owner_id:
                        message = comment['message']
                        print '[%s]\t[%s]\t[%s]' % (comment['id'], comment['created_time'], message)
                        
            totalRecord = totalRecord + 1
                
    return totalRecord
  
# 登出 Facebook  
def FB_Logout():    
    fbconsole.logout()

if __name__=="__main__":
    fb_who = 'me'
    fb_dateFrom = None
    
    if len(sys.argv) > 1:
        fb_who = sys.argv[1]
        
    if len(sys.argv) > 2:
        fb_dateFrom = datetime.strptime(sys.argv[2], '%Y-%m-%d')

    FB_Login()
    totalRecord = FB_Posts(fb_who, fb_dateFrom)
    FB_Logout()
    
    print '總共:%d' % (totalRecord)
ċ
FB_Posts.py
(2k)
李智,
2013年4月15日 上午7:27
ċ
FB_Posts_Date.py
(3k)
李智,
2013年4月15日 上午7:27