diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/syndication/models.py | 7 | ||||
-rw-r--r-- | app/syndication/syndicators.py | 90 |
2 files changed, 77 insertions, 20 deletions
diff --git a/app/syndication/models.py b/app/syndication/models.py index a7a9343..32b8dd3 100644 --- a/app/syndication/models.py +++ b/app/syndication/models.py @@ -4,7 +4,7 @@ from django.contrib.contenttypes.fields import GenericForeignKey from django.db.models.signals import post_save from django.dispatch import receiver -from .syndicators import post_to_medium, build_facebook_feed, post_photo_to_twitter, post_photo_to_flickr +from .syndicators import post_to_medium, build_facebook_feed, post_to_twitter, post_photo_to_flickr class Syndicate(models.Model): @@ -48,9 +48,8 @@ def post_save_events(sender, update_fields, created, instance, **kwargs): build_facebook_feed() instance.status = 2 if item.name == "Twitter": - if instance.content_type.name == "lux image": - print("calling function") - post_photo_to_twitter(instance.content_object) + print("calling function") + post_to_twitter(instance.content_object, instance.content_type.name) if item.name == "Flickr": if instance.content_type.name == "lux image": post_photo_to_flickr(instance.content_object) diff --git a/app/syndication/syndicators.py b/app/syndication/syndicators.py index 189c760..89280e3 100644 --- a/app/syndication/syndicators.py +++ b/app/syndication/syndicators.py @@ -1,10 +1,19 @@ from django.conf import settings from django.test.client import Client +import urllib +from urllib import parse +from urllib.parse import urlencode +import subprocess +import warnings from twython import Twython from bs4 import BeautifulSoup from medium import Client as MediumClient +from jrnl.models import extract_images +from photos.models import LuxImage import flickrapi +import facebook + def absolute_urls_for_syndication(s): @@ -43,27 +52,52 @@ def build_facebook_feed(): f.close() -def post_photo_to_twitter(photo): - p = open(photo.get_image_path_by_size("2280"), 'rb') - print(p) +def post_to_twitter(obj, ctype): + print("content type is" + ctype) t = Twython(settings.TWITTER_API_KEY, settings.TWITTER_API_SECRET, settings.TWITTER_ACCESS_TOKEN, settings.TWITTER_ACCESS_SECRET) + imgs = [] + if ctype == "lux image": + p = open(obj.get_image_path_by_size("2280"), 'rb') + if obj.caption: + status = obj.caption + else: + status = obj.title + response = t.upload_media(media=p) + imgs.append(response) + elif ctype == "lux note": + status = obj.body_markdown + # parse out images and send seperately + soup = BeautifulSoup(status, "lxml") + loop = 0 + for img in soup.find_all('img'): + src = img['src'].split("images/")[1] + i = LuxImage.objects.get(image__icontains=src) + p = open(i.get_image_path_by_size("2280"), 'rb') + response = t.upload_media(media=p) + imgs.append(response) + loop = loop+1 + if loop == 3: + break + soup.find_all('img').replaceWith("") + # truncate message + if status.length > 140: + try: + status = status.split("|")[0] + obj.get_absolute_url() + except: + status = status[:140] + obj.get_absolute_url() + print(status) try: - geo = t.reverse_geocode(lat=photo.latitude, lon=photo.longitude, accuracy=1500, granularity="city") + geo = t.reverse_geocode(lat=obj.latitude, lon=obj.longitude, accuracy=1500, granularity="city") geo_id = geo['result']['places'][0]['id'] except: pass - response = t.upload_media(media=p) - print(response) - if photo.caption: - status = photo.caption - else: - status = photo.title try: - status = t.update_status(status=status, media_ids=[response['media_id']], place_id=geo_id) + status = t.update_status(status=status, media_ids=[img['media_id'] for img in imgs], place_id=geo_id) except: - status = t.update_status(status=status, media_ids=[response['media_id']]) - print(status['entities']['media'][0]['expanded_url']) - + try: + status = t.update_status(status=status, media_ids=[img['media_id'] for img in imgs]) + except: + status = t.update_status(status=status) def post_photo_to_flickr(photo): flickr = flickrapi.FlickrAPI(settings.FLICKR_API_KEY, settings.FLICKR_API_SECRET) @@ -77,5 +111,29 @@ def post_photo_to_flickr(photo): except: pass -def post_photo_to_facebook(photo): - pass + +# Hide deprecation warnings. The facebook module isn't that up-to-date (facebook.GraphAPIError). +warnings.filterwarnings('ignore', category=DeprecationWarning) + + +# Parameters of your app and the id of the profile you want to mess with. + + +# Trying to get an access token. Very awkward. + +def post_photo_to_facebook(obj, ctype): + token = facebook.GraphAPI().get_app_access_token(settings.FACEBOOK_APP_ID, settings.FACEBOOK_APP_SECRET) + graph = facebook.GraphAPI(access_token=token, version='2.2') + + # Try to post something on the wall. + if ctype == "lux image": + p = open(obj.get_image_path_by_size("2280"), 'rb') + if obj.caption: + message = obj.caption + else: + message = obj.title + try: + fb_response = graph.put_photo(p, message=message) + print(fb_response) + except facebook.GraphAPIError as e: + print('Something went wrong:', e.type, e.message) |