diff options
Diffstat (limited to 'app/syndication/syndicators.py')
-rw-r--r-- | app/syndication/syndicators.py | 90 |
1 files changed, 74 insertions, 16 deletions
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) |