<commit_before>from app import *
from flask.ext.login import login_required, current_user
from flask import request, redirect, url_for

import facebook
import json

@app.route('/admin/authorize_facebook')
@login_required
def authorize_facebook():
    import urllib.parse, urllib.request, json
    
    params = {"client_id" : app.config.get('FACEBOOK_APP_ID'),
              "redirect_uri" : app.config.get('SITE_URL') + '/admin/authorize_facebook',
              "scope" : "publish_stream" }

    code = request.args.get('code')
    if code:
        params["code"] = code
        params["client_secret"] = app.config.get('FACEBOOK_APP_SECRET')
  
        r = urllib.request.urlopen("https://graph.facebook.com/oauth/access_token?" \
                                   + urllib.parse.urlencode(params))
        payload = urllib.parse.parse_qs(r.read())
        access_token = payload[b"access_token"]
        current_user.facebook_access_token = access_token
        db.session.commit()
        return redirect(url_for('settings'))
    else:
        return redirect("https://graph.facebook.com/oauth/authorize?" \
                        + urllib.parse.urlencode(params))


class FacebookClient:
    def __init__(self, app):
        self.app = app

    def handle_new_or_edit(self, post):
        app.logger.debug("publishing to facebook")
        graph = facebook.GraphAPI(post.author.facebook_access_token)
        actions = { "name" : "See Original", "link" : post.permalink_url }
        privacy = { "value" : "EVERYONE" }
        response = graph.put_object("me", "feed", name=post.title, message=post.content,
                                    link=post.repost_source or post.permalink_url,
                                    actions=json.dumps(actions), privacy=json.dumps(privacy))
        app.logger.debug("published to facebook. response {}".format(response))
        post.facebook_post_id = response['id']
<commit_msg>only include a link if we are reposing something<commit_after>from app import *
from flask.ext.login import login_required, current_user
from flask import request, redirect, url_for

import facebook
import json

@app.route('/admin/authorize_facebook')
@login_required
def authorize_facebook():
    import urllib.parse, urllib.request, json
    
    params = {"client_id" : app.config.get('FACEBOOK_APP_ID'),
              "redirect_uri" : app.config.get('SITE_URL') + '/admin/authorize_facebook',
              "scope" : "publish_stream" }

    code = request.args.get('code')
    if code:
        params["code"] = code
        params["client_secret"] = app.config.get('FACEBOOK_APP_SECRET')
  
        r = urllib.request.urlopen("https://graph.facebook.com/oauth/access_token?" \
                                   + urllib.parse.urlencode(params))
        payload = urllib.parse.parse_qs(r.read())
        access_token = payload[b"access_token"]
        current_user.facebook_access_token = access_token
        db.session.commit()
        return redirect(url_for('settings'))
    else:
        return redirect("https://graph.facebook.com/oauth/authorize?" \
                        + urllib.parse.urlencode(params))


class FacebookClient:
    def __init__(self, app):
        self.app = app

    def handle_new_or_edit(self, post):
        app.logger.debug("publishing to facebook")
        graph = facebook.GraphAPI(post.author.facebook_access_token)
        actions = { "name" : "See Original", "link" : post.permalink_url }
        privacy = { "value" : "EVERYONE" }
        response = graph.put_object("me", "feed", name=post.title, message=post.content,
                                    link=post.repost_source,
                                    actions=json.dumps(actions), privacy=json.dumps(privacy))
        app.logger.debug("published to facebook. response {}".format(response))
        post.facebook_post_id = response['id']

{'fraction_non_alphanumeric': 0.1225174103688419, 'fraction_numerical': 0.0, 'mean_word_length': 2.6447368421052633, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3053531', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1133, 'n_words': 204}