Compare commits
4 Commits
c65377ffc0
...
680c71a5c6
| Author | SHA1 | Date |
|---|---|---|
|
|
680c71a5c6 | |
|
|
92b133ffbe | |
|
|
fe0cdd6b9e | |
|
|
beb95c9bee |
|
|
@ -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('', '')
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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')
|
||||
Loading…
Reference in New Issue