<commit_before>from bs4 import BeautifulSoup
import urllib
import random
import asyncio
import aiohttp
import hangups


_externals = { "running": False }


def _initialise(Handlers, bot=None):
    Handlers.register_user_command(["meme"])
    return []


@asyncio.coroutine
def _retrieve(url, css_selector, attribute):
    print("_retrieve(): getting {}".format(url))
    html_request = yield from aiohttp.request('get', url)
    html = yield from html_request.read()
    soup = BeautifulSoup(str(html, 'utf-8'))
    links = []
    for link in soup.select(css_selector):
        links.append(link.get(attribute))
    return links


def meme(bot, event, *args):
    if _externals["running"]:
        bot.send_html_to_conversation(event.conv_id, "<i>busy, give me a moment...</i>")
        return

    _externals["running"] = True

    try:
        parameters = list(args)
        if len(parameters) == 0:
            parameters.append("robot")

        links = yield from _retrieve("http://memegenerator.net/memes/search?q=" + "+".join(parameters), ".item_medium_small > a", "href")
        links = yield from _retrieve("http://memegenerator.net" + random.choice(links), ".item_medium_small > a", "href")

        instance_link = "http://memegenerator.net" + random.choice(links)
        links = yield from _retrieve(instance_link, ".instance_large > img", "src")

        if len(links) > 0:
            jpg_link = links.pop()
            the_request = yield from aiohttp.request('get', jpg_link)
            image_data = yield from the_request.read()

            legacy_segments = [
                # hangups.ChatMessageSegment('link: ', is_italic=True),
                hangups.ChatMessageSegment(instance_link, hangups.SegmentType.LINK, link_target=instance_link)]

            photoID = yield from bot._client.upload_image(image_data)
            yield from bot._client.sendchatmessage(event.conv.id_, [seg.serialize() for seg in legacy_segments], imageID=photoID)
        else:
            bot.send_html_to_conversation(event.conv_id, "<i>couldn't find a nice picture :( try again</i>")
    except Exception as e:
        bot.send_html_to_conversation(event.conv_id, "<i>couldn't find a suitable meme! try again</i>")
    finally:
        _externals["running"] = False<commit_msg>compatibility fixes for tdryer/hangups and shaunofthelive/hangups<commit_after>from bs4 import BeautifulSoup
import urllib
import random
import asyncio
import aiohttp
import hangups


_externals = { "running": False }


def _initialise(Handlers, bot=None):
    Handlers.register_user_command(["meme"])
    return []


@asyncio.coroutine
def _retrieve(url, css_selector, attribute):
    print("meme._retrieve(): getting {}".format(url))
    html_request = yield from aiohttp.request('get', url)
    html = yield from html_request.read()
    soup = BeautifulSoup(str(html, 'utf-8'))
    links = []
    for link in soup.select(css_selector):
        links.append(link.get(attribute))
    return links


def meme(bot, event, *args):
    if _externals["running"]:
        bot.send_html_to_conversation(event.conv_id, "<i>busy, give me a moment...</i>")
        return

    _externals["running"] = True

    try:
        parameters = list(args)
        if len(parameters) == 0:
            parameters.append("robot")

        links = yield from _retrieve("http://memegenerator.net/memes/search?q=" + "+".join(parameters), ".item_medium_small > a", "href")
        links = yield from _retrieve("http://memegenerator.net" + random.choice(links), ".item_medium_small > a", "href")

        instance_link = "http://memegenerator.net" + random.choice(links)
        links = yield from _retrieve(instance_link, ".instance_large > img", "src")

        if len(links) > 0:
            jpg_link = links.pop()
            print("meme(): {} in {}".format(jpg_link, instance_link))

            the_request = yield from aiohttp.request('get', jpg_link)
            image_data = yield from the_request.read()

            legacy_segments = [
                # hangups.ChatMessageSegment('link: ', is_italic=True),
                hangups.ChatMessageSegment(instance_link, hangups.SegmentType.LINK, link_target=instance_link)]

            print("meme(): uploading image data {} bytes".format(len(image_data)))
            photoID = yield from bot._client.upload_image(image_data)

            try:
                # compatibility with ShaunOfTheLive/hangups
                yield from bot._client.sendchatmessage(event.conv.id_, [seg.serialize() for seg in legacy_segments], imageID=photoID)
            except TypeError as e:
                # preferred: tdryer/hangups
                yield from bot._client.sendchatmessage(event.conv.id_, [seg.serialize() for seg in legacy_segments], image_id=photoID)
        else:
            bot.send_html_to_conversation(event.conv_id, "<i>couldn't find a nice picture :( try again</i>")
    except Exception as e:
        bot.send_html_to_conversation(event.conv_id, "<i>couldn't find a suitable meme! try again</i>")
        print("{}".format(e))
    finally:
        _externals["running"] = False
{'fraction_non_alphanumeric': 0.12497551420176298, 'fraction_numerical': 0.0015670910871694416, 'mean_word_length': 3.0913461538461537, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, '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': '5979594', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1546, 'n_words': 343}