Skip to content

Instantly share code, notes, and snippets.

@coderdipto
Created August 16, 2018 16:05
Show Gist options
  • Save coderdipto/79110e0d612fa4144af42cd95d8f5056 to your computer and use it in GitHub Desktop.
Save coderdipto/79110e0d612fa4144af42cd95d8f5056 to your computer and use it in GitHub Desktop.
Facebook instant article API
FB_INSTANT_TOKEN = 'The token'
FB_PAGE_ID = 'The fb page id'
class FBInstantArticle:
def __init__(self, slug, published=False, development_mode=False):
self.page_token = FB_INSTANT_TOKEN
self.page_id = FB_PAGE_ID
self.host = 'https://graph.facebook.com/'
klass = import_class('article.models.Article')
self.article = get_obj(klass, slug=slug)
self.published = published
self.development_mode = development_mode
def post(self):
try:
if self.article:
article = self.article
url = self.host + self.page_id + '/instant_articles'
template = render_to_string('articles/fb_instant_article.html', {'article': article })
params = {
'access_token': self.page_token,
'html_source': template,
'published': self.published,
'development_mode': self.development_mode
}
response = requests.post(url, timeout=5, params=params)
# Update fia to True
if response.status_code == 200:
res = response.json()
inst_art_id = res.get('id', None)
if not inst_art_id:
raise Exception('Not found any instant article id ')
article.fb_ins_art_id = inst_art_id
article.save()
return response
except Exception as e:
logger.error(e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment