From 810ab906c4fa1c2c3714981c7bf2dc99e8399526 Mon Sep 17 00:00:00 2001 From: deflax Date: Thu, 25 May 2017 08:54:33 +0300 Subject: [PATCH] simple news crawler from fb --- LICENSE | 2 +- app/__init__.py | 7 +++- app/news/__init__.py | 3 ++ app/news/routes.py | 40 +++++++++++++++++++ app/settings/routes.py | 2 +- app/templates/_formhelpers.html | 12 ------ app/templates/_slide.html | 57 --------------------------- app/templates/base.html | 2 - app/templates/footer_index.html | 13 +++--- app/templates/main/index.html | 7 ++++ app/templates/news/facebook.html | 14 +++++++ app/templates/news/latest.html | 8 ++++ app/templates/settings/profile.html | 30 +++++++------- app/templates/vmanager/dashboard.html | 26 ++++++++---- app/vmanager/routes.py | 41 ++++++++++--------- babel.cfg | 3 ++ before_upgrade.txt | 36 +++++++++++++++++ requirements.txt | 56 ++++++++++++++++++-------- 18 files changed, 217 insertions(+), 142 deletions(-) create mode 100644 app/news/__init__.py create mode 100644 app/news/routes.py delete mode 100644 app/templates/_formhelpers.html delete mode 100644 app/templates/_slide.html create mode 100644 app/templates/news/facebook.html create mode 100644 app/templates/news/latest.html create mode 100644 babel.cfg create mode 100644 before_upgrade.txt diff --git a/LICENSE b/LICENSE index 7ac2761..9a00e36 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2015-2016 deflax +Copyright (c) 2015-2017 deflax.net This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages diff --git a/app/__init__.py b/app/__init__.py index 9d7d548..4e6e2ad 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -34,6 +34,9 @@ babel.init_app(app) from .main import main as main_blueprint app.register_blueprint(main_blueprint) +from .news import news as news_blueprint +app.register_blueprint(news_blueprint, url_prefix='/news') + from .auth import auth as auth_blueprint app.register_blueprint(auth_blueprint, url_prefix='/auth') @@ -60,7 +63,7 @@ class CustomJSONEncoder(JSONEncoder): app.json_encoder = CustomJSONEncoder -#if app.debug: +#TODO: if app.debug: if not app.debug: import logging from logging.handlers import RotatingFileHandler @@ -69,7 +72,7 @@ if not app.debug: file_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s: %(message)s [in %(pathname)s:%(lineno)d]')) app.logger.addHandler(file_handler) app.logger.setLevel(logging.DEBUG) - app.logger.info('Proxadmin started.') + #app.logger.info('Proxadmin started.') @app.errorhandler(403) def forbidden(e): diff --git a/app/news/__init__.py b/app/news/__init__.py new file mode 100644 index 0000000..ede6ea5 --- /dev/null +++ b/app/news/__init__.py @@ -0,0 +1,3 @@ +from flask import Blueprint +news = Blueprint('news', __name__) +from . import routes diff --git a/app/news/routes.py b/app/news/routes.py new file mode 100644 index 0000000..fb53be6 --- /dev/null +++ b/app/news/routes.py @@ -0,0 +1,40 @@ +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 +from facepy import GraphAPI + +@lru_cache(maxsize=8) +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'] + current_app.logger.info("FB Token: {}".format(fbtoken)) + return fbtoken + +@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'] + latest = graph.get(page_id+'/posts?limit=1&fields=id,message,created_time,link', page=True, retry=3) + posts = [] + for data in latest: + posts.append(data) + content=posts[0]['data'] + return render_template('news/latest.html', result=content) + diff --git a/app/settings/routes.py b/app/settings/routes.py index 47d2472..ff60123 100644 --- a/app/settings/routes.py +++ b/app/settings/routes.py @@ -51,7 +51,7 @@ def profile(): form.twofactor.data = current_user.twofactor wallet = "%.2f" % round(current_user.wallet, 3) - current_app.logger.info('wallet: ' + wallet) + current_app.logger.info('[{}] wallet: {}'.format(current_user.email, wallet)) return render_template('settings/profile.html', page=page, form=form, wallet=wallet) diff --git a/app/templates/_formhelpers.html b/app/templates/_formhelpers.html deleted file mode 100644 index 0b6bfda..0000000 --- a/app/templates/_formhelpers.html +++ /dev/null @@ -1,12 +0,0 @@ -{% macro render_field(field) %} -
{{ field.label }} -
{{ field(**kwargs)|safe }} - {% if field.errors %} - - {% endif %} -
-{% endmacro %} diff --git a/app/templates/_slide.html b/app/templates/_slide.html deleted file mode 100644 index a84567b..0000000 --- a/app/templates/_slide.html +++ /dev/null @@ -1,57 +0,0 @@ -{% extends "base.html" %} - -{% block page_content %} -

Index

- - - -
-
-

Yr umami selfies Carles DIY, pop-up Tonx meggings stumptown freegan street art Vice ethnic. Pickled gastropub lo-fi polaroid, ennui selvage meh Tumblr organic iPhone kale chips narwhal Echo Park. Tonx literally distillery Pitchfork McSweeney's semiotics. Stumptown YOLO fanny pack bespoke, kitsch Carles gastropub vegan. Biodiesel ennui church-key McSweeney's, selvage hoodie Brooklyn 90's lomo. Quinoa photo booth cliche semiotics. Roof party Etsy ethnic, fashion axe mlkshk 8-bit paleo.

-
-
-

Yr umami selfies Carles DIY, pop-up Tonx meggings stumptown freegan street art Vice ethnic. Pickled gastropub lo-fi polaroid, ennui selvage meh Tumblr organic iPhone kale chips narwhal Echo Park. Tonx literally distillery Pitchfork McSweeney's semiotics. Stumptown YOLO fanny pack bespoke, kitsch Carles gastropub vegan. Biodiesel ennui church-key McSweeney's, selvage hoodie Brooklyn 90's lomo. Quinoa photo booth cliche semiotics. Roof party Etsy ethnic, fashion axe mlkshk 8-bit paleo.

-
-
-

Yr umami selfies Carles DIY, pop-up Tonx meggings stumptown freegan street art Vice ethnic. Pickled gastropub lo-fi polaroid, ennui selvage meh Tumblr organic iPhone kale chips narwhal Echo Park. Tonx literally distillery Pitchfork McSweeney's semiotics. Stumptown YOLO fanny pack bespoke, kitsch Carles gastropub vegan. Biodiesel ennui church-key McSweeney's, selvage hoodie Brooklyn 90's lomo. Quinoa photo booth cliche semiotics. Roof party Etsy ethnic, fashion axe mlkshk 8-bit paleo.

-
-
- -{% endblock %} diff --git a/app/templates/base.html b/app/templates/base.html index 501df7a..9feaef9 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -6,8 +6,6 @@ {% block title %}Cloud Builder - Datapoint.bg{% endblock %} -{% block html_attribs %} lang="en"{% endblock %} - {% block head %} {{ super() }} diff --git a/app/templates/footer_index.html b/app/templates/footer_index.html index 4fa9c00..bb55f71 100644 --- a/app/templates/footer_index.html +++ b/app/templates/footer_index.html @@ -7,19 +7,13 @@

tel: +359 (0) 32 398 295
email: office@datapoint.bg

-
-

Latest Tweet

-

@datapoint Продавам пирони. Изгодно.

- 2 Days ago -
-

Datapoint

We are a dedicated team working to bring you the best hosting in the business. We are passionate about what we do, and why we do it.
Read More…

-
+

Newsletter Signup

Join our newsletter to stay informed on our latest updates and sales.

@@ -29,6 +23,11 @@
+ +
+ +
+ diff --git a/app/templates/main/index.html b/app/templates/main/index.html index 182de5b..a127ba6 100644 --- a/app/templates/main/index.html +++ b/app/templates/main/index.html @@ -42,8 +42,15 @@ }; makeBSS('.bss-slides', opts); + + {% endblock %} + {% block page_content %}