Compare commits
No commits in common. "680c71a5c65dd18b4f83615e445d7d8aeb73054b" and "c65377ffc0ed1cbdc3cf634d0813284804b6aa86" have entirely different histories.
680c71a5c6
...
c65377ffc0
|
|
@ -2,6 +2,6 @@ from .workers.instagram import InstagramWorker
|
||||||
from .workers.facebook import FacebookWorker
|
from .workers.facebook import FacebookWorker
|
||||||
|
|
||||||
|
|
||||||
def run_scheduler(accounts):
|
def run_scheduler():
|
||||||
#facebook = FacebookWorker('', '')
|
#facebook = FacebookWorker('', '')
|
||||||
#instagram = InstagramWorker('', '')
|
#instagram = InstagramWorker('', '')
|
||||||
|
|
|
||||||
|
|
@ -3,33 +3,18 @@ from datetime import datetime
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
|
|
||||||
from social_scheduler.web.posts.models import Post, Image
|
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):
|
class Command(BaseCommand):
|
||||||
help = 'Post all scheduled posts.'
|
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):
|
def handle(self, *args, **options):
|
||||||
posts = Post.objects.filter(
|
posts = Post.objects.filter(
|
||||||
publication_date__lte=datetime.now().astimezone(),
|
publication_date__lte=datetime.now().astimezone(),
|
||||||
posted=False)
|
posted=False)
|
||||||
for post in posts:
|
for post in posts:
|
||||||
|
print(post.id)
|
||||||
images = Image.objects.filter(post_id=post.id)
|
images = Image.objects.filter(post_id=post.id)
|
||||||
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(post.message))
|
||||||
|
imgs = {img.path.path: img.caption for img in images}
|
||||||
self.stdout.write(self.style.SUCCESS(imgs))
|
self.stdout.write(self.style.SUCCESS(imgs))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,26 +1,12 @@
|
||||||
import facebook
|
import facebook
|
||||||
|
|
||||||
from .worker 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_single(self, caption, image):
|
def post(self, image, caption):
|
||||||
with open(image, 'rb') as photo:
|
with open(image, 'rb') as photo:
|
||||||
self.api.put_object(self.page_id, 'photos', message=caption,
|
self.api.put_object(self.page_id, 'photos', message=caption, source=photo.read())
|
||||||
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,21 +1,10 @@
|
||||||
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_single(self, caption, image):
|
def post(self, image, caption):
|
||||||
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)
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
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