def delete(self, j): logging.info("Deleting %d"% j) post = self.obtainPostData(j) logging.info("Deleting %s"% post[0]) profile = self.getProfile() from buffpy.models.update import Update update = Update(api=self.client, id=profile.updates.pending[j].id) update = update.delete() logging.info("Update before return %s"% update) return(update)
def publish(self, j): logging.info("Publishing %d"% j) post = self.obtainPostData(j) logging.info("Publishing %s"% post[0]) profile = self.getProfile() update = Update(api=self.client, id=profile.updates.pending[j].id) res = update.publish() logging.info("Update before return %s"% res) if res: if 'message' in res: return(res['message']) else: return(res) else: return("Published!")
def test_update_edit_params(): ''' Test basic update editing with all the params ''' mocked_api = MagicMock() mocked_api.post.return_value = { 'update': {'id': 1, 'text': 'hey!'} } update = Update(mocked_api, raw_response={'id':1, 'text': 'ola!'}) new_update = update.edit(text='hey!', media={'link':'w'}, utc="a", now=True) assert_update = Update(mocked_api, raw_response={'id':1, 'text': 'hey!'}) post_data = 'text=hey!&now=True&utc=a&media[link]=w&' mocked_api.post.assert_called_once_with(url='updates/1/update.json', data=post_data) eq_(new_update, assert_update)
def test_update_edit(): ''' Test basic update editing ''' mocked_api = MagicMock() mocked_api.post.return_value = { 'update': {'id': 1, 'text': 'hey!'} } update = Update(mocked_api, raw_response={'id':1, 'text': 'ola!'}) new_update = update.edit(text='hey!') assert_update = Update(mocked_api, raw_response={'id':1, 'text': 'hey!'}) post_data = 'text=hey!&' mocked_api.post.assert_called_once_with(url='updates/1/update.json', data=post_data) eq_(new_update, assert_update)
def editl(self, j, newLink=''): logging.info("New link %s", newLink) thePost = self.obtainPostData(j) oldLink = thePost[1] profile = self.getProfile() logging.info("servicename %s" %self.service) from buffpy.models.update import Update i=0 update = Update(api=self.client, id=profile.updates.pending[j].id) print(update) # media = {'original': newLink } return('Not implemented!') update = update.edit(text=newTitle) title = oldTitle update = "Changed "+title+" with "+newTitle logging.info("Res update %s" % update) return(update)
def edit(self, j, newTitle=''): logging.info("New title %s", newTitle) thePost = self.obtainPostData(j) oldTitle = thePost[0] if not newTitle: newTitle = self.reorderTitle(oldTitle) profile = self.getProfile() logging.info("servicename %s" %self.service) from buffpy.models.update import Update i=0 update = Update(api=self.client, id=profile.updates.pending[j].id) print(update) update = update.edit(text=newTitle) title = oldTitle update = "Changed "+title+" with "+newTitle logging.info("Res update %s" % update) return(update)
def test_update_deleting(): ''' Test update's deleting ''' mocked_api = MagicMock() mocked_api.post.return_value = True response = Update(api=mocked_api, raw_response={'id': 1}).delete() mocked_api.post.assert_called_once_with(url='updates/1/destroy.json') eq_(response, True)
def test_udpate_publishing(): ''' Test basic update publishing ''' mocked_api = MagicMock() mocked_api.post.return_value = True response = Update(api=mocked_api, raw_response={'id': 1}).publish() mocked_api.post.assert_called_once_with(url="updates/1/share.json") eq_(response, True)
def test_update_interactions(): ''' Test basic analytics retrieving ''' mocked_api = MagicMock() mocked_api.get.return_value = {'interactions': [{'replies': 3}]} update = Update(mocked_api, raw_response={'id': 1, 'text': 'hey'}) eq_(update.interactions, [{'replies': 3}]) mocked_api.get.assert_called_once_with(url='updates/1/interactions.json')
def test_update_edit(): """ Test basic update editing """ mocked_api = MagicMock() mocked_api.post.return_value = {"update": {"id": 1, "text": "hey!🏳️🌈"}} update = Update(mocked_api, raw_response={"id": 1, "text": "ola!"}) new_update = update.edit(text="hey!🏳️🌈") assert_update = Update(mocked_api, raw_response={ "id": 1, "text": "hey!🏳️🌈" }) post_data = "text=hey!🏳️🌈&" mocked_api.post.assert_called_once_with(url="updates/1/update.json", data=post_data) assert new_update == assert_update
def test_update_deleting(): """ Test update"s deleting """ mocked_api = MagicMock() mocked_api.post.return_value = True response = Update(api=mocked_api, raw_response={"id": 1}).delete() mocked_api.post.assert_called_once_with(url="updates/1/destroy.json") assert response
def test_update_move_to_top(): ''' Test move_to_top implementation ''' mocked_api = MagicMock() mocked_api.post.return_value = {'id': 1, 'text': 'hey'} response = Update(api=mocked_api, raw_response={'id': 1}).move_to_top() mocked_api.post.assert_called_once_with(url='updates/1/move_to_top.json') eq_(response.text, 'hey')
def test_udpate_publishing(): """ Test basic update publishing """ mocked_api = MagicMock() mocked_api.post.return_value = True response = Update(api=mocked_api, raw_response={"id": 1}).publish() mocked_api.post.assert_called_once_with(url="updates/1/share.json") assert response
def test_update_move_to_top(): """ Test move_to_top implementation """ mocked_api = MagicMock() mocked_api.post.return_value = {"id": 1, "text": "hey"} response = Update(api=mocked_api, raw_response={"id": 1}).move_to_top() mocked_api.post.assert_called_once_with(url="updates/1/move_to_top.json") assert response.text == "hey"
def test_update_interactions(): """ Test basic analytics retrieving """ mocked_api = MagicMock() mocked_api.get.return_value = {"interactions": [{"replies": 3}]} update = Update(mocked_api, raw_response={"id": 1, "text": "hey"}) assert update.interactions == [{"replies": 3}] mocked_api.get.assert_called_once_with(url="updates/1/interactions.json")
def test_udpates_manager_sent(): """ Test basic sent updates retrieving """ mocked_api = MagicMock() mocked_api.get.return_value = {"updates": [{"text": "sent"}]} sent = Updates(api=mocked_api, profile_id=1).sent assert_update = Update(api=mocked_api, raw_response={"text": "sent"}) mocked_api.get.assert_called_once_with(url="profiles/1/updates/sent.json") assert sent == [assert_update]
def test_update_retrieving(): """ Test basic update retrieving based on update"s id """ mocked_api = MagicMock() mocked_api.get.return_value = {"text": "me", "id": 1} update = Update(api=mocked_api, id=1) mocked_api.get.assert_called_once_with(url="updates/1.json") assert update.api == mocked_api assert update.text == "me"
def test_updates_manager_pending(): """ Test basic pending updates retrieving """ mocked_api = MagicMock() mocked_api.get.return_value = {"updates": [{"text": "hey"}]} pendings = Updates(api=mocked_api, profile_id=1).pending mocked_api.get.assert_called_once_with( url="profiles/1/updates/pending.json") assert pendings == [Update(api=mocked_api, raw_response={"text": "hey"})]
def test_updates_manager_pending(): ''' Test basic pending updates retrieving ''' mocked_api = MagicMock() mocked_api.get.return_value = {'updates': [{'text': 'hey'}]} pendings = Updates(api=mocked_api, profile_id=1).pending mocked_api.get.assert_called_once_with( url='profiles/1/updates/pending.json') eq_(pendings, [Update(api=mocked_api, raw_response={'text': 'hey'})])
def test_udpates_manager_sent(): ''' Test basic sent updates retrieving ''' mocked_api = MagicMock() mocked_api.get.return_value = {'updates': [{'text': 'sent'}]} sent = Updates(api=mocked_api, profile_id=1).sent assert_update = Update(api=mocked_api, raw_response={'text': 'sent'}) mocked_api.get.assert_called_once_with(url='profiles/1/updates/sent.json') eq_(sent, [assert_update])
def new(self, text: str, shorten: str = None, now: str = None, top: str = None, media: str = None, when: str = None, service_geolocation_id: str = None, service_geolocation_name: str = None): """ Create one or more new status updates. """ url = PATHS["CREATE"] post_data = ["text={}&".format(quote(text.encode("utf-8")))] post_data.append("profile_ids[]={}&".format(self.profile_id)) if shorten: post_data.append("shorten={}&".format(shorten)) if now: post_data.append("now={}&".format(now)) if top: post_data.append("top={}&".format(top)) if when: post_data.append("scheduled_at={}&".format(str(when))) if media: media_format = "media[{}]={}&" for media_type, media_item in list(media.items()): quoted_media = quote(media_item.encode("utf-8")) post_data.append(media_format.format(media_type, quoted_media)) if service_geolocation_id: post_data.append( "service_geolocation_id={}&".format(service_geolocation_id)) if service_geolocation_name: post_data.append("service_geolocation_name={}&".format( service_geolocation_name)) response = self.api.post(url=url, data="".join(post_data)) new_update = Update(api=self.api, raw_response=response["updates"][0]) self.append(new_update) return new_update
def sent(self) -> List[Update]: """ Returns an array of updates that have been sent from buffer for an individual social media profile. """ url = PATHS["GET_SENT"].format(self.profile_id) response = self.api.get(url=url) self.__sent = [ Update(api=self.api, raw_response=update) for update in response.get("updates", []) ] return self.__sent
def test_update_edit_params(): """ Test basic update editing with all the params """ mocked_api = MagicMock() mocked_api.post.return_value = {"update": {"id": 1, "text": "hey!🏳️🌈"}} update = Update(mocked_api, raw_response={"id": 1, "text": "ola!"}) new_update = update.edit(text="hey!🏳️🌈", media={"link": "w"}, utc="a", now=True) assert_update = Update(mocked_api, raw_response={ "id": 1, "text": "hey!🏳️🌈" }) post_data = "text=hey!🏳️🌈&now=True&utc=a&media[link]=w&" mocked_api.post.assert_called_once_with(url="updates/1/update.json", data=post_data) assert new_update == assert_update
def pending(self) -> List[Update]: """ Returns an array of updates that are currently in buffer for an individual social media profile. """ url = PATHS["GET_PENDING"].format(self.profile_id) response = self.api.get(url=url) self.__pending = [ Update(api=self.api, raw_response=update) for update in response.get("updates", []) ] return self.__pending
def test_update_retrieving(): ''' Test basic update retrieving based on update's id ''' mocked_api = MagicMock() mocked_api.get.return_value = { 'text': 'me', 'id': 1 } update = Update(api=mocked_api, id=1) mocked_api.get.assert_called_once_with(url='updates/1.json') eq_(update.api, mocked_api) eq_(update.text, 'me')
def sent(self): ''' Returns an array of updates that have been sent from the buffer for an individual social media profile. ''' sent_updates = [] url = PATHS['GET_SENT'] % self.profile_id response = self.api.get(url=url) for update in response['updates']: sent_updates.append(Update(api=self.api, raw_response=update)) self.__sent = sent_updates return self.__sent
def pending(self): ''' Returns an array of updates that are currently in the buffer for an individual social media profile. ''' pending_updates = [] url = PATHS['GET_PENDING'] % self.profile_id response = self.api.get(url=url) for update in response['updates']: pending_updates.append(Update(api=self.api, raw_response=update)) self.__pending = pending_updates return self.__pending
def new(self, text, shorten=None, now=None, top=None, media=None, when=None): ''' Create one or more new status updates. ''' url = PATHS['CREATE'] post_data = "text=%s&" % text post_data += "profile_ids[]=%s&" % self.profile_id if shorten: post_data += "shorten=%s&" % shorten if now: post_data += "now=%s&" % now if top: post_data += "top=%s&" % top if when: post_data += "scheduled_at=%s&" % str(when) if media: media_format = "media[%s]=%s&" for media_type, media_item in media.iteritems(): post_data += media_format % (media_type, media_item) response = self.api.post(url=url, data=post_data) #error handling success = response.get('success', False) if success and 'updates' in response: new_update = Update(api=self.api, raw_response=response['updates'][0]) self.append(new_update) return new_update elif 'message' in response: raise BufferException(response['message']) else: raise BufferException("Unknown Error")
def test_updates_manager_new_update(): ''' Test update creation ''' mocked_api = MagicMock() mocked_api.post.return_value = {'updates': [{'text': 'hey'}]} updates = Updates(api=mocked_api, profile_id=1) update = updates.new("hey") data = "text=hey&profile_ids[]=1&" mocked_api.post.assert_called_once_with(url='updates/create.json', data=data) assert_update = Update(api=mocked_api, raw_response={'text': 'hey'}) eq_(update, assert_update) assert assert_update in updates
def test_updates_manager_new_update(): """ Test update creation """ mocked_api = MagicMock() mocked_api.post.return_value = {"updates": [{"text": "hey"}]} updates = Updates(api=mocked_api, profile_id=1) update = updates.new("hey") data = "text=hey&profile_ids[]=1&" mocked_api.post.assert_called_once_with(url="updates/create.json", data=data) assert_update = Update(api=mocked_api, raw_response={"text": "hey"}) assert update == assert_update assert assert_update in updates
def new(self, text, shorten=None, now=None, top=None, media=None, when=None): ''' Create one or more new status updates. ''' url = PATHS['CREATE'] post_data = "text=%s&" % urllib.quote(text.encode("utf-8")) post_data += "profile_ids[]=%s&" % self.profile_id if shorten: post_data += "shorten=%s&" % shorten if now: post_data += "now=%s&" % now if top: post_data += "top=%s&" % top if when: post_data += "scheduled_at=%s&" % str(when) if media: media_format = "media[%s]=%s&" for media_type, media_item in media.iteritems(): post_data += media_format % ( media_type, urllib.quote(media_item.encode("utf-8"))) response = self.api.post(url=url, data=post_data) new_update = Update(api=self.api, raw_response=response['updates'][0]) self.append(new_update) return new_update
class Updates(list): ''' Implenents all the profiles+updates logic. + retrieve updates related to a profile + create a new update + reorder and shuffle updates ''' def __init__(self, api, profile_id): self.api = api self.profile_id = profile_id self.__pending = [] self.__sent = [] @property def pending(self): ''' Returns an array of updates that are currently in the buffer for an individual social media profile. ''' pending_updates = [] url = PATHS['GET_PENDING'] % self.profile_id response = self.api.get(url=url) for update in response['updates']: pending_updates.append(Update(api=self.api, raw_response=update)) self.__pending = pending_updates return self.__pending @property def sent(self): ''' Returns an array of updates that have been sent from the buffer for an individual social media profile. ''' sent_updates = [] url = PATHS['GET_SENT'] % self.profile_id response = self.api.get(url=url) for update in response['updates']: sent_updates.append(Update(api=self.api, raw_response=update)) self.__sent = sent_updates return self.__sent def shuffle(self, count=None, utc=None): ''' Randomize the order at which statuses for the specified social media profile will be sent out of the buffer. ''' url = PATHS['SHUFFLE'] % self.profile_id post_data = '' if count: post_data += 'count=%s&' % count if utc: post_data += 'utc=%s' % utc return self.api.post(url=url, data=post_data) def reorder(self, updates_ids, offset=None, utc=None): ''' Edit the order at which statuses for the specified social media profile will be sent out of the buffer. ''' url = PATHS['REORDER'] % self.profile_id order_format = "order[]=%s&" post_data = '' if offset: post_data += 'offset=%s&' % offset if utc: post_data += 'utc=%s&' % utc for update in updates_ids: post_data += order_format % update return self.api.post(url=url, data=post_data) #TODO: Multiple profile posting def new(self, text, shorten=None, now=None, top=None, media=None, scheduled_at=None): ''' Create one or more new status updates. ''' url = PATHS['CREATE'] post_data = "text=%s&" % text post_data += "profile_ids[]=%s&" % self.profile_id if shorten: post_data += "shorten=%s&" % shorten if now: post_data += "now=%s&" % now if top: post_data += "top=%s&" % top if scheduled_at: post_data+="scheduled_at=%s&" % scheduled_at if media: media_format = "media[%s]=%s&" for media_type, media_item in media.iteritems(): post_data += media_format % (media_type, media_item) response = self.api.post(url=url, data=post_data) new_update = Update(api=self.api, raw_response=response['updates'][0]) self.append(new_update) return new_update
from buffpy.managers.profiles import Profiles from buffpy.managers.updates import Updates from buffpy.api import API # check http://bufferapp.com/developers/apps to retrieve a token # or generate one with the example token = "awesome_token" # instantiate the api object api = API(client_id="client_id", client_secret="client_secret", access_token=token) # retrieve a single update based on an id update = Update(api=api, id="update_id") print(update) # get update"s interactions print(update.interactions) # edit update = update.edit(text="Hey!") # publish now update.publish() # move to top update.move_to_top() # delete
from buffpy.models.update import Update from buffpy.managers.profiles import Profiles from buffpy.managers.updates import Updates from buffpy.api import API # check http://bufferapp.com/developers/apps to retrieve a token # or generate one with the example token = 'awesome_token' # instantiate the api object api = API(client_id='client_id', client_secret='client_secret', access_token=token) # retrieve a single update based on an id update = Update(api=api, id='update_id') print(update) # get update's interactions print(update.interactions) # edit update = update.edit(text="Hey!") # publish now update.publish() # move to top update.move_to_top() # delete