class FacebookGetter(object): """ Download documents from Facebook. """ def __init__(self, fb_api): self.fb_api = fb_api self.db_model = FacebookDbModel() self.exec_error = False #### METHOD 1: get new posts def get_new_posts(self): """Get new posts and their comments for every company.""" # Declare fields for FB query. my_fields='id,created_time,message,shares,likes.limit(0).summary(true),comments.limit(100).summary(true)' # Get posts for every company. for company in self.db_model.get_companies(): try: self.__get_posts_for(company, my_fields) except Exception, e: self.exec_error = True print "serious error: %s" % repr(e) self.__send_serious_error(e, company['fb_page'], 'get_new_posts') quit() # end script # Log execution. self.db_model.add_log_exec(1, self.exec_error)
def __init__(self, fb_api): self.fb_api = fb_api self.db_model = FacebookDbModel() self.exec_error = False # Generate query for posts basic_fields = ('id,created_time,message,shares,type,status_type,story,' 'likes.limit(0).summary(true),comments.limit(100).summary(true)' '{id,created_time,message,from,likes.limit(0).summary(true)},') reactions_list = [ 'reactions.type({0}).limit(0).summary(total_count).as(reactions_{1})' .format(r_type, r_type.lower()) for r_type in ['LOVE', 'WOW', 'HAHA', 'SAD', 'ANGRY', 'THANKFUL'] ] self.posts_query_fields = basic_fields + ','.join(reactions_list)
class FacebookGetter(object): """ Download documents from Facebook. """ def __init__(self, fb_api): self.fb_api = fb_api self.db_model = FacebookDbModel() self.exec_error = False # Generate query for posts basic_fields = ('id,created_time,message,shares,type,status_type,story,' 'likes.limit(0).summary(true),comments.limit(100).summary(true)' '{id,created_time,message,from,likes.limit(0).summary(true)},') reactions_list = [ 'reactions.type({0}).limit(0).summary(total_count).as(reactions_{1})' .format(r_type, r_type.lower()) for r_type in ['LOVE', 'WOW', 'HAHA', 'SAD', 'ANGRY', 'THANKFUL'] ] self.posts_query_fields = basic_fields + ','.join(reactions_list) #### METHOD 1: get new posts def get_new_posts(self): """Get new posts and their comments for every company.""" for company in self.db_model.get_companies(): try: self.__get_posts_for(company) except Exception: self.exec_error = True print "serious error: {0}".format(traceback.format_exc()) self.__send_serious_error(traceback.format_exc(), company['fb_page'], 'get_new_posts') def __get_posts_for(self, company): # Show company print "====%d: %s====" % (company['id'], company['fb_page']) # Send request to FB. try: result = self.fb_api.get_connections(id=company['fb_page'], connection_name='posts', limit=50, date_format='U', since=company['fb_post_timestamp'], fields='id,created_time') #result = json.load(open('../test_data/fb_posts.json')) # There was no or wrong response from FB. except GraphAPIError, e: self.exec_error = True print "FB error:", str(e) # Get error code error_code = int(e.result['error']['code']) # OK errors: unsupported get request, unknown error if error_code in [100, 1]: self.exec_error = False return False # skip company # Name error? if error_code == 803 or error_code == 21: print "Name problem with company %s" % company['fb_page'] self.__send_name_error(company['fb_page'], e.result['error'], 'get_new_posts') return False # skip company # If other than OK errors, send serious message. if error_code not in [100, 1, -3]: print "Serious problem with company %s" % company['fb_page'] self.__send_serious_error(e.result['error'], company['fb_page'], '__get_posts_for') raise e # Check for network error except URLError, e: print(repr(e)) return False # skip company
def __init__(self, fb_api): self.fb_api = fb_api self.db_model = FacebookDbModel() self.exec_error = False