def meme(): if not request.args: message = """ Welcome to Slack Meme! Check me out on <a href="https://github.com/nicolewhite/slack-meme">GitHub</a>. """ return message memegen = Memegen() memeifier = Memeifier() slack = Slack() token = request.args["token"] text = request.args["text"] channel_id = request.args["channel_id"] user_id = request.args["user_id"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." if text.strip() == "": return memegen.error() if text[:9] == "templates": return memegen.list_templates() preview = True if text[:7] == "preview" else False text = text.replace("preview", "", 1) if preview else text template, top, bottom = parse_text_into_params(text) valid_templates = [x[0] for x in memegen.get_templates()] if template in valid_templates: meme_url = memegen.build_url(template, top, bottom) elif memeifier.image_exists(template): meme_url = memeifier.build_url(template, top, bottom) else: return memegen.error() if preview: return meme_url payload = {"channel": channel_id} user = slack.find_user_info(user_id) payload.update(user) attachments = [{ "image_url": meme_url, "fallback": "Oops. Something went wrong." }] payload.update({"attachments": attachments}) try: slack.post_meme_to_webhook(payload) except Exception as e: return e return "Success!", 200
def index(): if not request.args: message = """ Welcome to SlackTeX! Check me out on <a href="https://github.com/nicolewhite/slacktex">GitHub</a>. """ return message slack = Slack() token = request.args["token"] latex = request.args["text"] channel_id = request.args["channel_id"] user_id = request.args["user_id"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." latex = quote(latex) latex_url = "http://chart.apis.google.com/chart?cht=tx&chl={latex}".format(latex=latex) payload = {"channel": channel_id} user = slack.find_user_info(user_id) payload.update(user) attachments = [{"image_url": latex_url, "fallback": "Oops. Something went wrong."}] payload.update({"attachments": attachments}) slack.post_latex_to_webhook(payload) return "Success!", 200
def meme(): if not request.args: message = """ Welcome to Slack Meme! Check me out on <a href="https://github.com/nicolewhite/slack-meme">GitHub</a>. """ return message memegen = Memegen() memeifier = Memeifier() slack = Slack() token = request.args["token"] text = request.args["text"] channel_id = request.args["channel_id"] user_id = request.args["user_id"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." if text.strip() == "": return memegen.error() if text[:9] == "templates": return memegen.list_templates() preview = True if text[:7] == "preview" else False text = text.replace("preview", "", 1) if preview else text template, top, bottom = parse_text_into_params(text) valid_templates = [x[0] for x in memegen.get_templates()] if template in valid_templates: meme_url = memegen.build_url(template, top, bottom) elif memeifier.image_exists(template): meme_url = memeifier.build_url(template, top, bottom) else: return memegen.error() print(meme_url) if preview: return meme_url payload = {"channel": channel_id} user = slack.find_user_info(user_id) payload.update(user) attachments = [{"image_url": meme_url, "fallback": "Oops. Something went wrong."}] payload.update({"attachments": attachments}) try: slack.post_meme_to_webhook(payload) except Exception as e: return e return "Success!", 200
def meme(): if not request.args: message = """ Welcome to Slack Meme! Check me out on <a href="https://github.com/nicolewhite/slack-meme">GitHub</a>. """ return message memegen = Memegen() memeifier = Memeifier() slack = Slack() token = request.args["token"] text = request.args["text"] channel_id = request.args["channel_id"] user_id = request.args["user_id"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." if text[:9] == "templates": return memegen.get_help() preview = True if text[:7] == "preview" else False text = text.replace("preview", "", 1) if preview else text template, top, bottom = parse_text_into_params(text) valid_templates = [x[0] for x in memegen.get_templates()] if template in valid_templates: meme_url = memegen.build_url(template, top, bottom) elif memeifier.image_exists(template): meme_url = memeifier.build_url(template, top, bottom) else: return "That template doesn't exist. Type `/meme templates` to see valid templates or provide your own as a URL." if preview: return meme_url payload = {"text": meme_url, "channel": channel_id} user = slack.find_user_info(user_id) payload.update(user) try: slack.post_meme_to_webhook(payload) except Exception as e: return e return "Success!", 200
def index(): if not request.args: message = """ Welcome to SlackTeX! Check me out on <a href="https://github.com/nicolewhite/slacktex">GitHub</a>. """ return message else: slack = Slack() payload = slack.post_latex_message(request) return jsonify(payload), 200
def meme(): memegen = Memegen() slack = Slack() if not request.args: return memegen.help() token = request.args["token"] text = request.args["text"].strip() channel_id = request.args["channel_id"] user_id = request.args["user_id"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." if text.lower() in ("help", ""): return memegen.help() if text.lower() == "templates": return memegen.list_templates() try: template, top, bottom = parse_text_into_params(text) except ValueError: return "Bad command: `%s`\n" % text + memegen.help() valid_templates = [x[0] for x in memegen.get_templates()] if template in valid_templates: meme_url = memegen.build_url(template, top, bottom) elif image_exists(template): meme_url = memegen.build_url("custom", top, bottom, template) else: return memegen.bad_template(template) payload = {"channel": channel_id} user = slack.find_user_info(user_id) payload.update(user) attachments = [{"image_url": meme_url, "fallback": "; ".join([top, bottom])}] payload.update({"attachments": attachments}) try: slack.post_meme_to_webhook(payload) except Exception as e: return e return "Success!", 200
def index(): if not request.args: message = """ Welcome to SlackTeX! Check me out on <a href="https://github.com/nicolewhite/slacktex">GitHub</a>. """ return message slack = Slack() token = request.args["token"] latex = request.args["text"] channel_id = request.args["channel_id"] user_id = request.args["user_id"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." asciify = { u'\u201c': '"', u'\u201d': '"', u'\u2018': "'", u'\u2019': "'", u'\u2026': "...", } for unicodeChar, asciiReplacement in asciify.iteritems(): latex = latex.replace(unicodeChar, asciiReplacement) latex = quote(latex.encode("utf-8")) latex_url = "http://chart.apis.google.com/chart?cht=tx&chl={latex}".format( latex=latex) payload = {"channel": channel_id} user = slack.find_user_info(user_id) payload.update(user) attachments = [{ "image_url": latex_url, "fallback": "Oops. Something went wrong." }] payload.update({"attachments": attachments}) slack.post_latex_to_webhook(payload) return "", 200
def meme(): memegen = Memegen() slack = Slack() if not request.args: return memegen.help() token = request.args["token"] text = request.args["text"].strip() channel_id = request.args["channel_id"] user_id = request.args["user_id"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." if text.lower() in ("help", ""): return memegen.help() if text.lower() == "templates": return memegen.list_templates() template, top, bottom = parse_text_into_params(text) valid_templates = [x[0] for x in memegen.get_templates()] if template in valid_templates: meme_url = memegen.build_url(template, top, bottom) elif image_exists(template): meme_url = memegen.build_url("custom", top, bottom, template) else: return memegen.bad_template(template) payload = {"channel": channel_id} user = slack.find_user_info(user_id) payload.update(user) attachments = [{"image_url": meme_url, "fallback": "; ".join([top, bottom])}] payload.update({"attachments": attachments}) try: slack.post_meme_to_webhook(payload) except Exception as e: return e return "Success!", 200
def meme(): memegen = Memegen() memeifier = Memeifier() slack = Slack() token = request.args["token"] text = request.args["text"] channel_id = request.args["channel_id"] user_id = request.args["user_id"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." if text[:9] == "templates": return memegen.get_help() if text[:9] == "list": return memegen.get_help() template, top, bottom = parse_text_into_params(text) valid_templates = [x[0] for x in memegen.get_templates()] if template in valid_templates: meme_url = memegen.build_url(template, top, bottom) elif memeifier.image_exists(template): meme_url = memeifier.build_url(template, top, bottom) else: return "That template doesn't exist. Type `/meme list` to see valid templates or provide your own as a URL." payload = { "channel": channel_id, "attachments": [{ "text": "", "fields": [{ "text": "" }], "image_url": meme_url }] } user = slack.find_user_info(user_id) payload.update(user) slack.post_meme_to_webhook(payload) return "Success!", 200
def index(): if not request.args: message = """ Welcome to SlackTeX! Check me out on <a href="https://github.com/nicolewhite/slacktex">GitHub</a>. """ return message slack = Slack() token = request.args["token"] latex = request.args["text"] channel_id = request.args["channel_id"] user_id = request.args["user_id"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." asciify = { u'\u201c': '"', u'\u201d': '"', u'\u2018': "'", u'\u2019': "'", u'\u2026': "...", } for unicodeChar, asciiReplacement in asciify.iteritems(): latex = latex.replace(unicodeChar, asciiReplacement) latex = quote(latex.encode("utf-8")) latex_url = "http://chart.apis.google.com/chart?cht=tx&chl={latex}".format(latex=latex) payload = {"channel": channel_id} user = slack.find_user_info(user_id) payload.update(user) attachments = [{"image_url": latex_url, "fallback": "Oops. Something went wrong."}] payload.update({"attachments": attachments}) slack.post_latex_to_webhook(payload) return "", 200
def legacy_import(): """ This fulfills the legacy needs from slackToDB.py from the deprecated `slack-urls` repo and is only called if a `--legacy` argument is passed to the importer. For details see the legacy commands documentation: ../README.md#legacy-commands """ try: db.create_tables([Slack]) except Exception as e: # Most likely table already exists, but we'll print the error to confirm. print('%s' % e) for root, dirs, files in os.walk(SLACK_FILES_DIR): if root != SLACK_FILES_DIR: print( "You don't appear to be in the correct directory. Please double-check" ) break for fname in files: dir_name = root.split(SLACK_FILES_DIR + '/')[1] channel_date, ftype = fname.rsplit('.') # Guard against .ds_store and other stray files. # We only want to read json files. if ftype == 'json': jsonfile = '%s/%s' % (dir_name, fname) for d in json_data(jsonfile): # in the case of bot posts, there will be no user. try: Slack.get(Slack.channel == dir_name, Slack.channel_date == channel_date, Slack.data == json.dumps(d)) except Slack.DoesNotExist: Slack.create(channel=dir_name, channel_date=channel_date, data=json.dumps(d))
def meme(): meme = Memegen() slack = Slack() token = request.args["token"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." text = request.args["text"] if text[:9] == "templates": return meme.get_help() try: template, top, bottom = meme.parse_text_into_params(text) except: return "Your syntax should be in the form `/meme [template]; [top]; [bottom];. Type `/meme templates` to see valid templates." valid_templates = [x["name"] for x in meme.get_templates()] if template not in valid_templates: return "That template doesn't exist. Type `/meme templates` to see valid templates." meme_url = meme.build_url(template, top, bottom) channel_id = request.args["channel_id"] payload = {"text": meme_url, "channel": channel_id} user_id = request.args["user_id"] user = slack.find_user_info(user_id) payload.update(user) slack.post_meme_to_webhook(payload) return "Success!", 200
from flask import Flask, request from models import Memegen, Slack, parse_text_into_params, image_exists app = Flask(__name__) memegen = Memegen() slack = Slack() @app.route("/", methods=["GET", "POST"]) def meme(): data = request.form if request.method == 'POST' else request.args token, text, channel_id, user_id = [data[key] for key in ("token", "text", "channel_id", "user_id")] text = text.strip() if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." if text.lower() in ("help", ""): return memegen.help() if text.lower() == "templates": return memegen.template_list template, top, bottom = parse_text_into_params(text) if template in memegen.valid_templates: meme_url = memegen.build_url(template, top, bottom) elif image_exists(template): meme_url = memegen.build_url("custom", top, bottom, template) else: return memegen.bad_template(template)
def get_data(): return (Slack.select().limit(1000))
if 'attachments' in attachment: file_type = 'attachment' url = attachment['attachments'][0].get('from_url') else: file_type = 'file_share' if attachment.get( 'file' ): # Apparently, there's an edge case with uploaded null files. /shrug url = attachment['file']['url_private'] if url and re.search(pattern, url): results.append( (msg['id'], url, msg['channel'], msg['channel_date'], attachment['user'], file_type)) return results if __name__ == "__main__": from models import Slack import csv query = (Slack.select()) rows = SlackAttachmentCheck.iterate_through_json_obj(query) pattern = "docs\.google\.com" results = SlackAttachmentCheck.check_urls_in_attachments(pattern, rows) with open('results.csv', 'w') as csvfile: slackwriter = csv.writer(csvfile) for res in results: slackwriter.writerow(res)
def meme(): if not request.args: message = """ Welcome to Slack Meme! Check me out on <a href="https://github.com/nicolewhite/slack-meme">GitHub</a>. """ return message memegen = Memegen() slack = Slack() token = request.args["token"] text = request.args["text"] channel_id = request.args["channel_id"] user_id = request.args["user_id"] if token != slack.SLASH_COMMAND_TOKEN: return "Unauthorized." if text.strip() == "": return memegen.error() if text[:9] == "templates": return memegen.list_templates() if text[:6] == "search": command, search, ignore = parse_text_into_params(text) return memegen.search_templates(search) if text[:9] == "shortcuts": return memegen.list_shortcuts() if text[:6] == "create": name, url, description = parse_text_into_params(text) if name and url: set_shortcut(name, url, description) return "Success! Now you can use it with: `/meme %s;<top>;<bottom>`" % (name) else: return "You need to pass name and url at least to create a shortcut: `/meme create;<name>;<url>;<description>`" template, top, bottom = parse_text_into_params(text) templates_not_shortcuts = [t[0] for t in memegen.get_templates() if not t[3]] if template in templates_not_shortcuts: meme_url = memegen.build_url(template, top, bottom) elif get_shortcut(template): meme_url = memegen.build_url("custom", top, bottom, get_shortcut(template)) print meme_url elif image_exists(template): print template meme_url = memegen.build_url("custom", top, bottom, template) print meme_url else: return memegen.error() payload = {"channel": channel_id} user = slack.find_user_info(user_id) payload.update(user) attachments = [{"image_url": meme_url, "fallback": "Oops. Something went wrong."}] payload.update({"attachments": attachments}) try: slack.post_meme_to_webhook(payload) except Exception as e: return e return "Success!", 200