def schedule_post(self, email, password, post_datetime, media_type, media_username, message_text): # confirm if email is in database try: customers_collection.find_one({"email": email}) except: return "Email does not exist", 402 # confirm email-password match entry = Entry() correct_password = entry.verify_password(email, password) if not correct_password: return "Incorrect password", 405 # confirm user has access to account, and also retrieve the token key and secret account_details = retrieve_token(email) # save message_text into publishing collection post_details = {"body": message_text, "email": email, "media_username": media_username, "media_type": media_type, "sm_profile_id": account_details[0][4], "token_key": account_details[0][2], "token_secret": account_details[0][3], "post_datetime": post_datetime, "post_type": "scheduled" } publishing_collection.insert_one(post_details) return "Your post has been scheduled for {}".format(post_datetime), 200
def get_posts_twitter(self, email, password, media_username, media_type="twitter"): # confirm if email is in database try: customers_collection.find_one({"email": email}) except: return "Email does not exist", 402 # confirm email-password match entry = Entry() correct_password = entry.verify_password(email, password) if not correct_password: return "Incorrect password", 405 # confirm user has access to account, and also retrieve the right token key and secret account_details = retrieve_token(email) # fetch requested data if media_type == media_type: try: # authenticate twitter app with user account auth.set_access_token(account_details[0][2], account_details[0][3]) api = tweepy.API(auth, wait_on_rate_limit=True) api.verify_credentials() print("Authentication OK") except: return "Error during authentication" finally: # Retrieve users recent tweets on Twitter user_all_tweets_details = api.user_timeline(screen_name = account_details[0][1], count = 15, include_rts = False) # Collect required details only user_tweets_details = [(status.text, status.user.screen_name, status.favorite_count, status.retweet_count) for status in my_tweets] return user_tweets_details, 200
def setUp(self): self.res = Resource("http://"+utils.sso_server) # set logging level # Need to be moved to somewhere else for global configuration debug = logging.DEBUG logging.basicConfig(level = debug, stream = sys.stderr, format = '%(levelname)s %(asctime)s [%(message)s]') logging.info("") self.token = utils.retrieve_token()
def linkedin_quick_post(self, email, password, media_username, message_text, media_type="linkedin"): # confirm if email is in database try: customers_collection.find_one({"email": email}) except: return "Email does not exist", 402 # confirm email-password match entry = Entry() correct_password = entry.verify_password(email, password) if not correct_password: return "Incorrect password", 405 # confirm user has access to account, and also retrieve the token key and secret account_details = retrieve_token(email) # save message_text into publishing collection post_details = {"body": message_text, "email": email, "media_username": media_username, "media_type": media_type, "sm_profile_id": account_details[0][4], "token_key": account_details[0][2], "post_datetime": post_datetime, "post_type": "quick" } publishing_collection.insert_one(post_details) # linkedin api url for posting url = "https://api.linkedin.com/v2/ugcPosts" # header parameter headers = {'Content-Type': 'application/json', 'X-Restli-Protocol-Version': '2.0.0', 'Authorization': 'Bearer ' + token_key} # post json post_data = { "author": "urn:li:organization:"+sm_profile_id, "lifecycleState": "PUBLISHED", "specificContent": { "com.linkedin.ugc.ShareContent": { "shareCommentary": { "text": message_text }, "shareMediaCategory": "NONE" } }, "visibility": { "com.linkedin.ugc.MemberNetworkVisibility": "PUBLIC" } } # try posting content try: requests.post(url, headers=headers, json=post_data) except: print("Error trying to post content") return "Post successfully published to LinkedIn", 200
def test_positive_delete_tokens_exist(self): """ Delete tokens exist and check the 'revoked_token_num' is '1' """ token = utils.retrieve_token(another=True) r = self.res.delete('/tokens/'+token['access_token'], headers=utils.headers) logging.info("Return response is '%s'" % r) self.assertEqual(self.res.response.status, 200) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) # assert 'revoked_token_num' is 1 self.assertEqual(rd['revoked_token_num'], 1, "The 'revoked_token_num' is not equal to 1")
def test_positive_delete_tokens_condition_exist(self): """ Delete tokens with existing user_id and check the 'revoked_token_num' is >= '1' """ token = utils.retrieve_token(another=True) r = self.res.get('/tokens/'+token['access_token'], headers=utils.headers) token = ast.literal_eval(r) r = self.res.delete('/tokens/?user_id='+str(token['user_id']), headers=utils.headers) logging.info("Return response is '%s'" % r) self.assertEqual(self.res.response.status, 200) # convert string to dictionary rd = ast.literal_eval(r) logging.info("Return response in dictionary format is '%s'" % rd) # assert 'revoked_token_num' is >= 1 self.assertTrue((rd['revoked_token_num'] >= 1), "The 'revoked_token_num' is not >= 1")
def twitter_quick_post(self, email, password, media_username, message_text, media_type="twitter"): # confirm if email is in database try: customers_collection.find_one({"email": email}) except: return "Email does not exist", 402 # confirm email-password match entry = Entry() correct_password = entry.verify_password(email, password) if not correct_password: return "Incorrect password", 405 # confirm user has access to account, and also retrieve the token key and secret account_details = retrieve_token(email) # save message_text into publishing collection post_details = {"body": message_text, "email": email, "media_username": media_username, "media_type": media_type, "token_key": account_details[0][2], "token_secret": account_details[0][3], "post_datetime": post_datetime, "post_type": "quick" } publishing_collection.insert_one(post_details) try: # authenticate twitter app with user account auth.set_access_token(token_key, token_secret) api = tweepy.API(auth, wait_on_rate_limit=True) api.verify_credentials() print("Authentication OK") except: print("Error during authentication") finally: # Post Tweet on Twitter api.update_status(message_text) return "Post successfully published to Twitter", 200
def get_posts_linkedin(self, email, password, media_username, media_type="linkedin"): # confirm if email is in database try: customers_collection.find_one({"email": email}) except: return "Email does not exist", 402 # confirm email-password match entry = Entry() correct_password = entry.verify_password(email, password) if not correct_password: return "Incorrect password", 405 # confirm user has access to account, and also retrieve the right token key and secret account_details = retrieve_token(email) if media_type == media_type: # linkedin url for retrieving posts url = "https://api.linkedin.com/v2/ugcPosts?q=authors&authors=List(urn%3Ali%3Aorganization%3A{})&sortBy=LAST_MODIFIED".format(sm_profile_id) # required parameter params = {'oauth2_access_token': token_key} # retrieve posts response = requests.get(url, params=params) # loop through posts return response # to change it when Linkedin solve issue