Add methods to upload an album of images

This commit is contained in:
Patrick Neff 2019-03-02 19:03:17 +01:00
parent beb95c9bee
commit fe0cdd6b9e
3 changed files with 36 additions and 5 deletions

View File

@ -1,12 +1,26 @@
import facebook import facebook
from . import Worker
class FacebookWorker(object):
class FacebookWorker(Worker):
def __init__(self, page_id, oauth_access_token): def __init__(self, page_id, oauth_access_token):
self.page_id = page_id self.page_id = page_id
self.oauth_access_token = oauth_access_token self.oauth_access_token = oauth_access_token
self.api = facebook.GraphAPI(self.oauth_access_token) self.api = facebook.GraphAPI(self.oauth_access_token)
def post(self, image, caption): def post_single(self, caption, image):
with open(image, 'rb') as photo: with open(image, 'rb') as photo:
self.api.put_object(self.page_id, 'photos', message=caption, source=photo.read()) self.api.put_object(self.page_id, 'photos', message=caption,
source=photo.read())
def post_multiple(self, message, images):
ids = []
for image, msg in images.items:
with open(image, 'rb') as photo:
result = self.api.put_object(self.page_id, 'photos',
message=msg, source=photo.read(),
published=False)
ids.append(result['id'])
self.api.put_object(self.page_id, 'feed', message=message,
attached_media=ids)

View File

@ -1,10 +1,21 @@
from InstagramAPI import InstagramAPI from InstagramAPI import InstagramAPI
from .worker import Worker
class InstagramWorker(object):
class InstagramWorker(Worker):
def __init__(self, username, password): def __init__(self, username, password):
self.api = InstagramAPI(username, password) self.api = InstagramAPI(username, password)
self.api.login() self.api.login()
def post(self, image, caption): def post_single(self, caption, image):
self.api.uploadPhoto(image, caption) self.api.uploadPhoto(image, caption)
def post_multiple(self, message, images):
imgs = list()
for image in images.keys():
imgs.append({
'type': 'photo',
'file': image
})
self.api.uploadAlbum(imgs, caption=message)

View File

@ -0,0 +1,6 @@
class Worker(object):
def post_single(self, caption, image):
raise NotImplementedError('post_single has to be implemented')
def post_multiple(self, message, images):
raise NotImplementedError('post_multiple has to be implemented')