diff options
Diffstat (limited to 'app/unused_apps/syndication/syndicators.py')
-rw-r--r-- | app/unused_apps/syndication/syndicators.py | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/app/unused_apps/syndication/syndicators.py b/app/unused_apps/syndication/syndicators.py new file mode 100644 index 0000000..5f8762c --- /dev/null +++ b/app/unused_apps/syndication/syndicators.py @@ -0,0 +1,123 @@ +import os +from django.conf import settings +from django.test.client import Client + +from twython import Twython +from bs4 import BeautifulSoup +from medium import Client as MediumClient +import flickrapi +import facebook + +from photos.models import LuxImage + + +def absolute_urls_for_syndication(s): + soup = BeautifulSoup(s, "lxml") + for a in soup.find_all('a'): + if a['href'][:1] == "/": + a['href'] = "https://luxagraf.net%s" % a['href'] + return soup + + +def post_to_medium(item): + client = MediumClient(application_id=settings.MEDIUM_CLIENT_ID, application_secret=settings.MEDIUM_CLIENT_SECRET) + client.access_token = settings.MEDIUM_INT_TOKEN + user = client.get_current_user() + head = '<p><i>This was originally posted <a href="https://luxagraf.net%s" rel="canonical">on my own site</a>.</i></p>' % item.get_absolute_url() + body = "%s %s" % (head, absolute_urls_for_syndication(item.body_html)) + # Create a post. + post = client.create_post( + user_id=user["id"], + title=item.title, + content=body, + content_format="html", + publish_status="public", + canonical_url="https://luxagraf.net%s" % item.get_absolute_url(), + license="all-rights-reserved" + ) + return post["url"] + + +def build_facebook_feed(): + print("+++++++++++++building+++++++++++") + c = Client() + response = c.get('/iafeed.xml', HTTP_HOST='127.0.0.1') + f = open("%siafeed.xml" % settings.FLATFILES_ROOT, 'wb') + f.write(response.content) + f.close() + + +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_largest_image(), '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_largest_image(), 'rb') + response = t.upload_media(media=p) + imgs.append(response) + loop = loop+1 + if loop == 3: + break + for t in soup.find_all('img'): + t.replaceWith("") + # truncate message + if len(status) > 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=obj.latitude, lon=obj.longitude, accuracy=1500, granularity="city") + geo_id = geo['result']['places'][0]['id'] + tweet = t.update_status(status=status, media_ids=[img['media_id'] for img in imgs], place_id=geo_id) + except: + try: + tweet = t.update_status(status=status, media_ids=[img['media_id'] for img in imgs]) + except: + tweet = t.update_status(status=status) + + +def post_photo_to_flickr(photo): + TOKEN_FILE = os.path.join(settings.PROJ_ROOT, "config/flickrToken") + token = open(TOKEN_FILE).read() + flickr = flickrapi.FlickrAPI(settings.FLICKR_API_KEY, settings.FLICKR_API_SECRET, token=token) + sent = flickr.upload(filename=photo.get_image_path_by_size("original"), title=photo.title, description=photo.caption) + photo_id = sent.find('photoid').text + photo.flickr_id = photo_id + try: + flickr.photos.geo.setLocation(photo_id=photo_id, lat=photo.latitude, lon=photo.longitude, accuracy=12) + except: + pass + + +def post_to_facebook(obj, ctype): + from syndication.models import FBOAuthToken + token = FBOAuthToken.objects.latest() + graph = facebook.GraphAPI(access_token=token.long_token, version='2.2') + if ctype == "lux image": + p = open(obj.get_largest_image(), '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) |