Beispiel #1
0
    def __init__(self, subreddit, refresh_token, spamcruncher=None, un=None, users_tracked=False, botbans=False):

        self.imgur = ImgurClient(credentials.get_token("IMGUR_CLIENT_ID", 'credentials'),
                                 credentials.get_token("IMGUR_CLIENT_SECRET", 'credentials'))
        self.users_tracked = users_tracked
        self.subreddit = subreddit
        self.un = un
        self.refresh_token = refresh_token
        self.spamcruncher = spamcruncher
        self.botbans = botbans
        self.r = praw.Reddit(user_agent="Snoohelper 0.1 by /u/Santi871",
                                 client_id=REDDIT_APP_ID, client_secret=REDDIT_APP_SECRET,
                                 refresh_token=self.refresh_token)
Beispiel #2
0
def create_dummy_command_request(command):
    dummy_request = {
        'user_name':
        'Santi871',
        'team_domain':
        'snoohelpertesting',
        'team_id':
        get_token("team_id", "SnooHelper Testing", "teams_test.ini"),
        'command':
        command,
        'text':
        'santi871',
        'channel_name':
        '#general',
        'response_url':
        get_token("webhook_url", "SnooHelper Testing", "teams_test.ini"),
        'token':
        'abc123'
    }
    return snoohelper.utils.slack.SlackRequest(form=dummy_request)
Beispiel #3
0
def create_app(teams_controller, handler):
    new_app = Flask(__name__, template_folder='../webapp/templates')
    SSLify(new_app)
    new_app.config['CONTROLLER'] = teams_controller
    new_app.config['HANDLER'] = handler
    new_app.config['WTF_CSRF_ENABLED'] = True
    new_app.config['SECRET_KEY'] = get_token("SECRET_KEY", "credentials")

    reddits = dict()
    cur_bot = dict()

    @new_app.route("/slack/oauthcallback", methods=['POST', 'GET'])
    def slack_oauth_callback():
        form = ModulesSelectForm()
        slack_teams_controller = new_app.config['CONTROLLER']

        if not form.validate_on_submit():
            # Handle Slack auth and redirect to modules selection
            data = {
                'client_id': SLACK_APP_ID,
                'client_secret': SLACK_APP_SECRET,
                'code': request.args.get('code')
            }
            response = requests.post('https://slack.com/api/oauth.access',
                                     params=data)
            response_json = response.json()

            cur_bot[
                response_json['team_name']] = slack_teams_controller.teams.get(
                    response_json['team_name'], None)
            slack_teams_controller.add_team(response_json)

            response = make_response(
                render_template('modules_select.html',
                                title='Modules Select',
                                form=form))
            response.set_cookie('slack_team_name', response_json['team_name'])

            return response
        else:
            # Handle modules selection and redirect to Reddit auth
            form_data = form.modules_select.data
            team_name = request.cookies.get('slack_team_name')
            scopes, modules = snoohelper.utils.reddit.get_scopes(form_data)

            slack_teams_controller.teams[team_name].set("modules", modules)
            slack_teams_controller.teams[team_name].set("scopes", scopes)

            state = ''.join(
                random.SystemRandom().choice(string.ascii_uppercase +
                                             string.digits) for _ in range(8))
            r = praw.Reddit(
                user_agent=
                "Snoohelper 0.3 by /u/Santi871 - authorization module",
                client_id=REDDIT_APP_ID,
                client_secret=REDDIT_APP_SECRET,
                redirect_uri=REDDIT_REDIRECT_URI)
            reddits[state] = r
            url = r.auth.url(scopes, state, 'permanent')
            response = make_response(redirect(url, code=302))

            return response

    @new_app.route('/reddit/oauthcallback', methods=['POST', 'GET'])
    def reddit_oauth_callback():
        form = SubredditSelectForm()
        slack_teams_controller = new_app.config['CONTROLLER']

        if request.method == 'GET':
            # Handle Reddit auth and show subreddit selection template
            code = request.args.get('code', None)
            state = request.args.get('state', None)
            r = reddits[state]
            team_name = request.cookies.get('slack_team_name')
            if code is not None:
                while True:
                    try:
                        refresh_token = r.auth.authorize(code)
                        slack_teams_controller.teams[team_name].set(
                            "reddit_refresh_token", refresh_token)
                        break
                        # catch oauth errors
                    except requests.exceptions.ConnectionError:
                        print("Connection error")
                        time.sleep(0.5)
                        continue

                choices = [
                    (subreddit.display_name, subreddit.display_name)
                    for subreddit in r.user.moderator_subreddits(limit=None)
                ]
                form.subreddit_select.choices = choices
                reddits.pop(state, None)
                return render_template('subreddit_select.html',
                                       title='Select Subreddit',
                                       form=form)

        elif request.method == 'POST':
            # Finish up with Reddit auth and start bot
            subreddit = form.subreddit_select.data
            team_name = request.cookies.get('slack_team_name', None)

            if team_name is None:
                return "There was an error processing your request, please try again."

            slack_teams_controller.remove_team(team_name)
            slack_teams_controller.teams[team_name].set("subreddit", subreddit)
            slack_teams_controller.add_bot(team_name)

            return "Successfully added Slack team and linked to subreddit. Enjoy!"

    @new_app.route('/slack/commands', methods=['POST'])
    def command():
        requests_handler = new_app.config['HANDLER']
        slack_request = utils.slack.SlackRequest(request, SLACK_COMMANDS_TOKEN)
        if slack_request.is_valid:
            response = requests_handler.handle_command(slack_request)
            return Response(response=response.get_json(),
                            mimetype="application/json")

        else:
            return "Invalid request token."

    @new_app.route('/slack/action-endpoint', methods=['POST'])
    def button_response():
        requests_handler = new_app.config['HANDLER']
        slack_request = utils.slack.SlackRequest(request, SLACK_COMMANDS_TOKEN)
        if slack_request.is_valid:

            response = requests_handler.handle_button(slack_request)
            if response is None:
                return Response(status=200)

            return Response(response=response.get_json(),
                            mimetype="application/json")

        else:
            return "Invalid request token."

    return new_app
Beispiel #4
0
import random
import string
import time

import praw
import requests
from flask import Flask, request, Response, redirect, render_template, make_response
from flask_sslify import SSLify
import snoohelper.utils as utils
from snoohelper.utils.credentials import get_token
import snoohelper.utils.slack
import snoohelper.utils.reddit
from .form import SubredditSelectForm, ModulesSelectForm

SLACK_APP_ID = get_token("SLACK_APP_ID", "credentials")
SLACK_APP_SECRET = get_token("SLACK_APP_SECRET", "credentials")
SLACK_COMMANDS_TOKEN = get_token("SLACK_COMMANDS_TOKEN", "credentials")
REDDIT_APP_ID = get_token("REDDIT_APP_ID", "credentials")
REDDIT_APP_SECRET = get_token("REDDIT_APP_SECRET", "credentials")
REDDIT_REDIRECT_URI = get_token("REDDIT_REDIRECT_URI", "credentials")


def create_app(teams_controller, handler):
    new_app = Flask(__name__, template_folder='../webapp/templates')
    SSLify(new_app)
    new_app.config['CONTROLLER'] = teams_controller
    new_app.config['HANDLER'] = handler
    new_app.config['WTF_CSRF_ENABLED'] = True
    new_app.config['SECRET_KEY'] = get_token("SECRET_KEY", "credentials")

    reddits = dict()
Beispiel #5
0
from snoohelper.utils.teams import SlackTeamsController
from snoohelper.webapp.requests_handler import RequestsHandler
from snoohelper.webapp.webapp import create_app
from snoohelper.utils.credentials import get_token

if __name__ == '__main__':
    context = ('santihub.crt', 'santihub.key')
    testing = get_token("testing", "credentials", is_bool=True)
    if not testing:
        controller = SlackTeamsController("teams.ini", 'snoohelper_master.db')
    else:
        controller = SlackTeamsController("teams_test.ini",
                                          'snoohelper_test.db')
    handler = RequestsHandler(controller)
    app = create_app(controller, handler)
    app.run(host='0.0.0.0', port=5023, ssl_context=context, threaded=True)
Beispiel #6
0
import os

import matplotlib.pyplot as plt
import numpy as np
import praw
import prawcore.exceptions
from imgurpython import ImgurClient
from retrying import retry
from wordcloud import WordCloud, STOPWORDS
import imgurpython.helpers.error
from snoohelper.database.models import UserModel
from snoohelper.utils import credentials
import snoohelper.utils as utils
import snoohelper.utils.slack

REDDIT_APP_ID = credentials.get_token("REDDIT_APP_ID", "credentials")
REDDIT_APP_SECRET = credentials.get_token("REDDIT_APP_SECRET", "credentials")
REDDIT_REDIRECT_URI = credentials.get_token("REDDIT_REDIRECT_URI", "credentials")


class SummaryGenerator:

    """Module that generates user summaries. Requires 'read' and 'history' permissions."""

    def __init__(self, subreddit, refresh_token, spamcruncher=None, un=None, users_tracked=False, botbans=False):

        self.imgur = ImgurClient(credentials.get_token("IMGUR_CLIENT_ID", 'credentials'),
                                 credentials.get_token("IMGUR_CLIENT_SECRET", 'credentials'))
        self.users_tracked = users_tracked
        self.subreddit = subreddit
        self.un = un