<commit_before>"""
Identify images, upload them to google plus, post in hangouts
"""

import asyncio
import aiohttp
import asyncio
import os
import io

def _initialise(Handlers, bot=None):
    Handlers.register_handler(_watch_image_link, type="message")
    return []


@asyncio.coroutine
def _watch_image_link(bot, event, command):
    # Don't handle events caused by the bot himself
    if event.user.is_self:
        return

    if " " in event.text:
        """immediately reject anything with spaces, must be a link"""
        return

    probable_image_link = False
    event_text_lower = event.text.lower()
    if event_text_lower.startswith(("imgur.com/", "i.imgur.com/")):
        """special processing for naked imgur links with no protocol"""
        probable_image_link = True
    elif event_text_lower.startswith(("http://", "https://")):
        if event_text_lower.startswith(("http://imgur.com/", "https://imgur.com/")):
            """standard imgur links may not have an extension"""
            probable_image_link = True
        elif event_text_lower.endswith((".png", ".gif", ".gifv", ".jpg")):
            """all other image links should have a protocol and end with a valid extension"""
            probable_image_link = True
    if probable_image_link and "googleusercontent" in event_text_lower:
        """reject links posted by google to prevent endless attachment loop"""
        print("_watch_image_link(): rejected link {}".format(event.text))
        return

    if probable_image_link:
        link_image = event.text

        if "imgur.com" in link_image:
            """special imgur link handling"""
            if not link_image.endswith((".jpg", ".gif", "gifv", "png")):
                link_image = link_image + ".gif"
            link_image = "https://i.imgur.com/" + os.path.basename(link_image)
 
        link_image = link_image.replace(".gifv",".gif")

        print("_watch_image_link(): getting {}".format(link_image))

        filename = os.path.basename(link_image)
        r = yield from aiohttp.request('get', link_image)
        raw = yield from r.read()
        image_data = io.BytesIO(raw)

        image_id = yield from bot._client.upload_image(image_data, filename=filename)

        yield from bot._client.sendchatmessage(event.conv.id_, None, image_id=image_id)<commit_msg>added regex for imgur, optimised link scanning<commit_after>"""
Identify images, upload them to google plus, post in hangouts
"""

import asyncio
import aiohttp
import asyncio
import os
import io
import re

def _initialise(Handlers, bot=None):
    Handlers.register_handler(_watch_image_link, type="message")
    return []


@asyncio.coroutine
def _watch_image_link(bot, event, command):
    # Don't handle events caused by the bot himself
    if event.user.is_self:
        return

    if " " in event.text:
        """immediately reject anything with spaces, must be a link"""
        return

    probable_image_link = False
    event_text_lower = event.text.lower()
    if re.match("^(https?://)?([a-z0-9.]*?\.)?imgur.com/", event_text_lower, re.IGNORECASE):
        """imgur links can be supplied with/without protocol and extension"""
        probable_image_link = True
    elif event_text_lower.startswith(("http://", "https://")) and event_text_lower.endswith((".png", ".gif", ".gifv", ".jpg")):
        """other image links must have protocol and end with valid extension"""
        probable_image_link = True
    if probable_image_link and "googleusercontent" in event_text_lower:
        """reject links posted by google to prevent endless attachment loop"""
        print("_watch_image_link(): rejected link {}".format(event.text))
        return

    if probable_image_link:
        link_image = event.text

        if "imgur.com" in link_image:
            """special imgur link handling"""
            if not link_image.endswith((".jpg", ".gif", "gifv", "png")):
                link_image = link_image + ".gif"
            link_image = "https://i.imgur.com/" + os.path.basename(link_image)
 
        link_image = link_image.replace(".gifv",".gif")

        print("_watch_image_link(): getting {}".format(link_image))

        filename = os.path.basename(link_image)
        r = yield from aiohttp.request('get', link_image)
        raw = yield from r.read()
        image_data = io.BytesIO(raw)

        image_id = yield from bot._client.upload_image(image_data, filename=filename)

        yield from bot._client.sendchatmessage(event.conv.id_, None, image_id=image_id)
{'fraction_non_alphanumeric': 0.12231331708397962, 'fraction_numerical': 0.00044316419233325947, 'mean_word_length': 3.066666666666667, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2223196', 'n_tokens_mistral': 1347, 'n_tokens_neox': 1320, 'n_words': 360}