proxadmin/app/news/routes.py

50 lines
1.7 KiB
Python
Raw Normal View History

2017-05-25 01:54:33 -04:00
from flask import render_template, abort, redirect, url_for, abort, flash, request, current_app, make_response, g
from . import news
from functools import lru_cache
import requests
import json
2017-11-05 15:20:39 -05:00
from dateutil.parser import parse
from dateutil.tz import tzutc
2017-05-25 01:54:33 -04:00
from facepy import GraphAPI
2017-10-07 11:39:50 -04:00
@lru_cache(maxsize=32)
2017-05-25 01:54:33 -04:00
def get_fb_token():
payload = {'grant_type': 'client_credentials', 'client_id': current_app.config['FB_APP_ID'], 'client_secret': current_app.config['FB_APP_SECRET']}
response = requests.post('https://graph.facebook.com/oauth/access_token?', params = payload)
fbjson = json.loads(response.text)
fbtoken = fbjson['access_token']
2017-10-07 11:39:50 -04:00
#current_app.logger.info("FB Token: {}".format(fbtoken))
2017-05-25 01:54:33 -04:00
return fbtoken
2017-11-05 15:20:39 -05:00
@news.app_template_filter('fbtime')
def fbtime(event_date):
UTC = tzutc()
dt = parse(event_date)
dt_utc = dt.astimezone(UTC) # Convert to UTC
return dt.strftime('%d %b %Y')
2017-05-25 01:54:33 -04:00
@news.route("/all", methods=['GET'])
def all():
graph = GraphAPI(get_fb_token())
#current_app.logger.info(get_fb_token.cache_info())
page_id = current_app.config['FB_PAGE_ID']
datas = graph.get(page_id+'/posts?fields=id,message,created_time,link,icon', page=True, retry=3)
posts = []
for data in datas:
posts.append(data)
content=posts[0]['data']
return render_template('news/facebook.html', result=content)
@news.route('/latest')
def latest():
graph = GraphAPI(get_fb_token())
page_id = current_app.config['FB_PAGE_ID']
2017-06-11 22:12:27 -04:00
latest = graph.get(page_id+'/posts?limit=2&fields=id,message,created_time,link', page=True, retry=3)
2017-05-25 01:54:33 -04:00
posts = []
for data in latest:
posts.append(data)
content=posts[0]['data']
return render_template('news/latest.html', result=content)