Skip to content

Commit

Permalink
- prefetch for Facebook
Browse files Browse the repository at this point in the history
  • Loading branch information
claytonc.sousa@gmail.com authored and claytonc.sousa@gmail.com committed Sep 24, 2017
1 parent 67cb86b commit b112f81
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 0 deletions.
13 changes: 13 additions & 0 deletions sc/social/like/interfaces.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,16 @@ class ISocialLikeSettings(model.Schema):
required=False,
default='',
)

model.fieldset(
'open_graph', label=u'Open Graph', fields=['facebook_prefetch_enable'])

facebook_prefetch_enable = schema.Bool(
title=_(u'Enable Prefetching Facebook?'),
description=_(
u'help_facebook_prefetch_enable',
default=u'If enabled, an event is triggered so that Facebook '
u'downloads mobile content before someone clicks on a link.'
),
default=False,
)
9 changes: 9 additions & 0 deletions sc/social/like/locales/pt_BR/LC_MESSAGES/sc.social.like.po
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,12 @@ msgstr "Cancelar"
#: sc/social/like/vocabularies.py:36
msgid "vertical"
msgstr "vertical"


#: ./sc/social/like/interfaces.py:220
msgid "Enable Prefetching Facebook?"
msgstr ""

#: ./sc/social/like/interfaces.py:222
msgid "help_facebook_prefetch_enable"
msgstr "Se ativado, um evento é acionado para que o Facebook faça o download de conteúdo móvel antes de alguém clicar em um link."
8 changes: 8 additions & 0 deletions sc/social/like/locales/sc.social.like.pot
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,11 @@ msgstr ""
#: ./sc/social/like/vocabularies.py:36
msgid "vertical"
msgstr ""

#: ./sc/social/like/interfaces.py:220
msgid "Enable Prefetching Facebook?"
msgstr ""

#: ./sc/social/like/interfaces.py:222
msgid "help_facebook_prefetch_enable"
msgstr ""
23 changes: 23 additions & 0 deletions sc/social/like/subscribers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from zope.component import getUtility
from zope.schema.interfaces import WrongType

import requests
import traceback


Expand Down Expand Up @@ -148,9 +149,31 @@ def check_sharing_best_practices_on_editing(obj, event):
state = api.content.get_state(obj)
if state in ('published', ):
check_sharing_best_practices(obj)
prefetch_facebook(obj)


def check_sharing_best_practices_on_publishing(obj, event):
"""Event subscriber for content being published."""
if event.status['review_state'] in ('published', ):
check_sharing_best_practices(obj)
prefetch_facebook(obj)


def prefetch_facebook(obj):
"""Prefetching in object if enable."""

record = ISocialLikeSettings.__identifier__ + '.facebook_prefetch_enable'
prefetch_enable = api.portal.get_registry_record(record, default=False)

if not prefetch_enable:
return

url = obj.absolute_url()
r = requests.post('https://graph.facebook.com/?id=' + url + '&scrape=true',
timeout=10,
verify=False)

if r.status_code == '200':
logger.info(u'Prefetching: ' + url)
else:
logger.warn(u'Prefetching fail')
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
'Products.CMFPlone >=4.3',
'Products.CMFQuickInstallerTool',
'Products.GenericSetup',
'requests',
'setuptools',
'zope.component',
'zope.i18nmessageid',
Expand Down

0 comments on commit b112f81

Please sign in to comment.