Skip to content

Commit

Permalink
Flickr publish support
Browse files Browse the repository at this point in the history
relates to snarfed#432
  • Loading branch information
kylewm committed Nov 16, 2015
1 parent a18c15a commit 13b0471
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 19 deletions.
2 changes: 1 addition & 1 deletion app.yaml
Expand Up @@ -68,7 +68,7 @@ handlers:
script: facebook.application
secure: always

- url: /flickr/(start|add|delete)(/.*)?
- url: /flickr/(start|add|delete|publish)(/.*)?
script: flickr.application
secure: always

Expand Down
20 changes: 18 additions & 2 deletions flickr.py
Expand Up @@ -87,14 +87,30 @@ def canonicalize_syndication_url(self, url, **kwargs):
return url


class StartHandler(util.Handler):
"""Custom handler that sets OAuth scopes based on the requested
feature(s)
"""
def post(self):
features = self.request.get('feature')
features = features.split(',') if features else []
starter = util.oauth_starter(oauth_flickr.StartHandler).to(
'/flickr/add',
scopes='write' if 'publish' in features else 'read')
starter(self.request, self.response).post()


class AddFlickr(oauth_flickr.CallbackHandler, util.Handler):
def finish(self, auth_entity, state=None):
logging.debug('finish with %s, %s', auth_entity, state)
self.maybe_add_or_delete_source(Flickr, auth_entity, state)


application = webapp2.WSGIApplication([
('/flickr/start', util.oauth_starter(oauth_flickr.StartHandler).to('/flickr/add')),
('/flickr/start', StartHandler),
('/flickr/add', AddFlickr),
('/flickr/delete/finish', oauth_flickr.CallbackHandler.to('/delete/finish')),
('/flickr/delete/finish',
oauth_flickr.CallbackHandler.to('/delete/finish')),
('/flickr/publish/start',
oauth_flickr.StartHandler.to('/publish/flickr/finish')),
], debug=appengine_config.DEBUG)
35 changes: 21 additions & 14 deletions publish.py
Expand Up @@ -38,32 +38,31 @@
from appengine_config import HTTP_TIMEOUT

from bs4 import BeautifulSoup
from facebook import FacebookPage
from flickr import Flickr
from googleplus import GooglePlusPage
from granary import microformats2
from granary import source as gr_source
from instagram import Instagram
from models import Publish, PublishedPage
from oauth_dropins import facebook as oauth_facebook
from oauth_dropins import flickr as oauth_flickr
from oauth_dropins import instagram as oauth_instagram
from oauth_dropins import twitter as oauth_twitter
from facebook import FacebookPage
from googleplus import GooglePlusPage
from twitter import Twitter
import html2text
from instagram import Instagram
import models
from models import Publish, PublishedPage
import requests
from twitter import Twitter
import util
import webapp2
import webmention

from google.appengine.ext import ndb
from google.appengine.ext.webapp import template

SOURCE_NAMES = {
cls.SHORT_NAME: cls for cls in
(FacebookPage, Twitter, Instagram, GooglePlusPage)}
SOURCE_DOMAINS = {
cls.GR_CLASS.DOMAIN: cls for cls in
(FacebookPage, Twitter, Instagram, GooglePlusPage)}
SOURCES = (FacebookPage, Flickr, Twitter, Instagram, GooglePlusPage)
SOURCE_NAMES = {cls.SHORT_NAME: cls for cls in SOURCES}
SOURCE_DOMAINS = {cls.GR_CLASS.DOMAIN: cls for cls in SOURCES}


class Handler(webmention.WebmentionHandler):
Expand Down Expand Up @@ -120,7 +119,8 @@ def _run(self):
source_cls = SOURCE_NAMES.get(path_parts[-1])
if (domain not in ('brid.gy', 'www.brid.gy', 'localhost:8080') or
len(path_parts) != 2 or path_parts[0] != '/publish' or not source_cls):
return self.error('Target must be brid.gy/publish/{facebook,twitter,instagram}')
return self.error(
'Target must be brid.gy/publish/{facebook,flickr,twitter,instagram}')
elif source_cls == GooglePlusPage:
return self.error('Sorry, %s is not yet supported.' %
source_cls.GR_CLASS.NAME)
Expand Down Expand Up @@ -539,14 +539,20 @@ def error(self, error, html=None, status=400, data=None, mail=False):
self.mail_me(error)


# We want CallbackHandler.get() and SendHandler.finish(), so put CallbackHandler
# first and override finish.
# We want CallbackHandler.get() and SendHandler.finish(), so put
# CallbackHandler first and override finish.
class FacebookSendHandler(oauth_facebook.CallbackHandler, SendHandler):
finish = SendHandler.finish


class FlickrSendHandler(oauth_flickr.CallbackHandler, SendHandler):
finish = SendHandler.finish


class TwitterSendHandler(oauth_twitter.CallbackHandler, SendHandler):
finish = SendHandler.finish


# Instagram only allows a single OAuth callback URL, so that's handled in
# instagram.py and it redirects here for publishes.
class InstagramSendHandler(SendHandler):
Expand Down Expand Up @@ -590,6 +596,7 @@ def authorize(self):
('/publish/webmention', WebmentionHandler),
('/publish/(facebook|twitter|instagram)', webmention.WebmentionGetHandler),
('/publish/facebook/finish', FacebookSendHandler),
('/publish/flickr/finish', FlickrSendHandler),
('/publish/instagram/finish', InstagramSendHandler),
('/publish/twitter/finish', TwitterSendHandler),
],
Expand Down
10 changes: 10 additions & 0 deletions templates/flickr_user.html
Expand Up @@ -10,3 +10,13 @@
enter it in the <em>Website</em> field, then click here:
{% include "flickr_signup.html" %}
{% endblock %}


{% block publish-signup %}
<span class="big">Click to enable <a href="/about#publish">publishing</a>:</span>
<form method="post" name="publish-flickr-start" action="/flickr/start" class="signup">
<input type="image" alt="Sign in with Flickr"
src="/oauth_dropins/static/flickr.png" />
<input name="feature" type="hidden" value="publish" />
</form>
{% endblock %}
2 changes: 1 addition & 1 deletion templates/social_user.html
Expand Up @@ -48,7 +48,7 @@
<div class="row">

<!-- Listen UI -->
<div id="listen-ui" class="{% if source.SHORT_NAME == 'facebook' or source.SHORT_NAME == 'twitter' or source.SHORT_NAME == 'instagram' %}col-sm-6{% endif %}">
<div id="listen-ui" class="{% if source.SHORT_NAME == 'facebook' or source.SHORT_NAME == 'twitter' or source.SHORT_NAME == 'instagram' or source.SHORT_NAME == 'flickr' %}col-sm-6{% endif %}">

{% if "listen" in source.features and source.status != "disabled" %}
<data class="p-bridgy-listen-status" value="enabled">
Expand Down
2 changes: 1 addition & 1 deletion util.py
Expand Up @@ -25,7 +25,7 @@
from google.appengine.ext import ndb

# when running in dev_appserver, replace these domains in links with localhost
LOCALHOST_TEST_DOMAINS = frozenset(('kylewm.com', 'snarfed.org'))
LOCALHOST_TEST_DOMAINS = frozenset(('snarfed.org',))

EPOCH = datetime.datetime.utcfromtimestamp(0)
POLL_TASK_DATETIME_FORMAT = '%Y-%m-%d-%H-%M-%S'
Expand Down

0 comments on commit 13b0471

Please sign in to comment.