Compare commits

...

4 Commits

Author SHA1 Message Date
Patrick Neff 680c71a5c6 Make workers and django play together 2019-03-02 19:31:31 +01:00
Patrick Neff 92b133ffbe Merge branch 'master' into add-django 2019-03-02 19:03:30 +01:00
Patrick Neff fe0cdd6b9e Add methods to upload an album of images 2019-03-02 19:03:17 +01:00
Patrick Neff beb95c9bee Add gitignore 2019-03-02 18:26:30 +01:00
5 changed files with 54 additions and 8 deletions

View File

@ -2,6 +2,6 @@ from .workers.instagram import InstagramWorker
from .workers.facebook import FacebookWorker
def run_scheduler():
def run_scheduler(accounts):
#facebook = FacebookWorker('', '')
#instagram = InstagramWorker('', '')

View File

@ -3,18 +3,33 @@ from datetime import datetime
from django.core.management.base import BaseCommand
from social_scheduler.web.posts.models import Post, Image
from social_scheduler.workers.instagram import InstagramWorker
from social_scheduler.workers.facebook import FacebookWorker
class Command(BaseCommand):
help = 'Post all scheduled posts.'
def post(self, message, images):
self.facebook = FacebookWorker('', '')
self.instagram = InstagramWorker('', '')
if len(images) > 1:
self.facebook.post_multiple(message, images)
self.instagram.post_multiple(message, images[0:9])
elif len(images) == 1:
for image in images:
self.facebook.post_single(message, image)
self.instagram.post_single(message, image)
def handle(self, *args, **options):
posts = Post.objects.filter(
publication_date__lte=datetime.now().astimezone(),
posted=False)
for post in posts:
print(post.id)
images = Image.objects.filter(post_id=post.id)
self.stdout.write(self.style.SUCCESS(post.message))
imgs = {img.path.path: img.caption for img in images}
self.post(post.message, imgs)
self.stdout.write(self.style.SUCCESS(post.message))
self.stdout.write(self.style.SUCCESS(imgs))

View File

@ -1,12 +1,26 @@
import facebook
from .worker import Worker
class FacebookWorker(object):
class FacebookWorker(Worker):
def __init__(self, page_id, oauth_access_token):
self.page_id = page_id
self.oauth_access_token = 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:
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 .worker import Worker
class InstagramWorker(object):
class InstagramWorker(Worker):
def __init__(self, username, password):
self.api = InstagramAPI(username, password)
self.api.login()
def post(self, image, caption):
def post_single(self, caption, image):
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')