diff options
author | luxagraf <sng@luxagraf.net> | 2016-03-31 11:09:08 -0400 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2016-03-31 11:09:08 -0400 |
commit | 9bc9b213fb5849b20c965b7bd6695b5796ba19ae (patch) | |
tree | bfe475dee42e6ebc814fb1d6c0f270df3627f814 /app | |
parent | f81d4cdc61baf6904746ace7226eb6031de32b1a (diff) |
added support for Facebook Instant Articles to syndication app
Diffstat (limited to 'app')
-rw-r--r-- | app/jrnl/views.py | 2 | ||||
-rw-r--r-- | app/syndication/admin.py | 1 | ||||
-rw-r--r-- | app/syndication/models.py | 15 | ||||
-rw-r--r-- | app/syndication/syndicators.py | 15 | ||||
-rw-r--r-- | app/syndication/templatetags/facebook_processor.py | 4 | ||||
-rw-r--r-- | app/syndication/views.py | 17 |
6 files changed, 44 insertions, 10 deletions
diff --git a/app/jrnl/views.py b/app/jrnl/views.py index e7f704b..58bdf0b 100644 --- a/app/jrnl/views.py +++ b/app/jrnl/views.py @@ -100,7 +100,7 @@ class HomepageList(ListView): class JrnlRSSFeedView(Feed): title = "Luxagraf: Topographical Writings" - link = "/writing/" + link = "/jrnl/" description = "Latest postings to luxagraf.net" description_template = 'feeds/blog_description.html' diff --git a/app/syndication/admin.py b/app/syndication/admin.py index b7568a4..9d88183 100644 --- a/app/syndication/admin.py +++ b/app/syndication/admin.py @@ -10,4 +10,5 @@ class SyndicateAdmin(admin.ModelAdmin): @admin.register(SyndicatedItem) class SyndicatedItemAdmin(admin.ModelAdmin): + list_display = ('__str__', 'syndicated_to',) pass diff --git a/app/syndication/models.py b/app/syndication/models.py index cc4f4a4..53a6ed1 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 +from .syndicators import post_to_medium, build_facebook_feed class Syndicate(models.Model): @@ -30,6 +30,10 @@ class SyndicatedItem(models.Model): def __str__(self): return self.content_object.title + def syndicated_to(self): + return ','.join(str(synd) for synd in self.syndicate.all()) + + @receiver(post_save, sender=SyndicatedItem) def post_save_events(sender, update_fields, created, instance, **kwargs): @@ -38,6 +42,9 @@ def post_save_events(sender, update_fields, created, instance, **kwargs): if item.name == "Medium": instance.rel_link = post_to_medium(instance.content_object) instance.status = 2 - post_save.disconnect(post_save_events, sender=SyndicatedItem) - instance.save() - post_save.connect(post_save_events, sender=SyndicatedItem) + if item.name == "Facebook": + build_facebook_feed(instance) + instance.status = 2 + post_save.disconnect(post_save_events, sender=SyndicatedItem) + instance.save() + post_save.connect(post_save_events, sender=SyndicatedItem) diff --git a/app/syndication/syndicators.py b/app/syndication/syndicators.py index de157af..82b9564 100644 --- a/app/syndication/syndicators.py +++ b/app/syndication/syndicators.py @@ -1,7 +1,8 @@ from django.conf import settings +from django.test.client import Client from bs4 import BeautifulSoup -from medium import Client +from medium import Client as MediumClient def absolute_urls_for_syndication(s): @@ -9,11 +10,11 @@ def absolute_urls_for_syndication(s): for a in soup.find_all('a'): if a['href'][:1] == "/": a['href'] = "https://luxagraf.net%s" % a['href'] - print(soup) + return soup def post_to_medium(item): - client = Client(application_id=settings.MEDIUM_CLIENT_ID, application_secret=settings.MEDIUM_CLIENT_SECRET) + 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() @@ -29,3 +30,11 @@ def post_to_medium(item): license="all-rights-reserved" ) return post["url"] + + +def build_facebook_feed(item): + 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() diff --git a/app/syndication/templatetags/facebook_processor.py b/app/syndication/templatetags/facebook_processor.py index 59c4812..973f863 100644 --- a/app/syndication/templatetags/facebook_processor.py +++ b/app/syndication/templatetags/facebook_processor.py @@ -17,7 +17,7 @@ def wrap_image_tags(text): def facebook_processor(text): ia = absolute_urls_for_syndication(text) - ia = wrap_image_tags(ia) - return ia + ia = wrap_image_tags(str(ia)[12:-14]) + return str(ia)[12:-14] register.filter('facebook_processor', facebook_processor) diff --git a/app/syndication/views.py b/app/syndication/views.py new file mode 100644 index 0000000..e489846 --- /dev/null +++ b/app/syndication/views.py @@ -0,0 +1,17 @@ +from django.views.generic import ListView + +from .models import SyndicatedItem + + +class FacebookFeedView(ListView): + """ + Return a main entry and list of Entries in reverse chronological order + """ + queryset = SyndicatedItem.objects.filter(syndicate__name__exact="Facebook")[:16] + + def get_template_names(self): + return "fb-feed.xml" + + def render_to_response(self, context, **response_kwargs): + response_kwargs.update({'content_type': 'text/xml'}) + return super(FacebookFeedView, self).render_to_response(context, **response_kwargs) |