<commit_before>#!/usr/bin/env python
# encoding: utf-8

from postcards import Postcards
import urllib
from util.random_search_term.random_search_term import get_random_search_term
from bs4 import BeautifulSoup
import json
import urllib.request, urllib.error, urllib.parse
import random
import sys


class PostcardsBingRandom(Postcards):
    """
    Get an arbitrary picture from the internet as postcard image.
    Note: image content may be inappropriate
    """

    def get_img_and_text(self, plugin_config, cli_args):
        found = False
        counter = 0

        imgs = []
        while not found and counter < 10:
            keyword = self._get_search_term()
            self.logger.debug('using keyword=' + keyword)

            imgs = self._fetch_img_urls(keyword)
            random.shuffle(imgs)
            self.logger.trace(imgs)
            self.logger.debug('fetched {} images'.format(len(imgs)))

            counter += 1
            if len(imgs) > 0:
                found = True

        if not found:
            self.logger.error('no images found although {} times tried'.format(counter))
            exit(1)

        img = random.choice(imgs)[2]
        self.logger.info('choosing image {}'.format(img))

        return {
            'img': img,
            'text': ''
        }

    def _get_search_term(self):
        try:
            return get_random_search_term()
        except Exception as e:
            self.logger.error('something broke with the generated python code')
            raise e

    def _get_bing_url(self, keyword, adult_content=True, large_size=True):
        if large_size:
            keyword += '+filterui:imagesize-large'

        url = "http://www.bing.com/images/search?q=" + keyword + "&FORM=HDRSC2"

        if adult_content:
            url += '&adlt=off'

        return url<commit_msg>introduce keyword flag for random images plugin<commit_after>#!/usr/bin/env python
# encoding: utf-8

from postcards import Postcards
import urllib
from util.random_search_term.random_search_term import get_random_search_term
from bs4 import BeautifulSoup
import json
import urllib.request, urllib.error, urllib.parse
import random
import sys


class PostcardsBingRandom(Postcards):
    """
    Get an arbitrary picture from the internet as postcard image.
    Note: image content may be inappropriate

    Use flag --keyword <keyword> to search for specific images
    """

    def enrich_parser(self, parser):
        parser.add_argument('--keyword', default=None, type=str,
                            help='use custom keyword to search for images')
        pass

    def get_img_and_text(self, plugin_config, cli_args):

        imgs = []

        if cli_args.keyword:
            self.logger.info('using custom keyword {}'.format(cli_args.keyword))
            imgs = self._fetch_img_urls(cli_args.keyword)
        else:
            imgs = self._get_images_with_random_keyword()

        if not imgs:
            self.logger.error('No images found for given keyword')
            exit(1)

        img = random.choice(imgs)[2]
        self.logger.info('choosing image {}'.format(img))

        return {
            'img': img,
            'text': ''
        }

    def _get_images_for_random_keyword(self):
        found = False
        counter = 0

        while not found and counter < 10:
            keyword = self._get_search_term()
            self.logger.debug('trying to search for images with keyword=' + keyword)

            imgs = self._fetch_img_urls(keyword)
            random.shuffle(imgs)
            self.logger.trace(imgs)
            self.logger.debug('fetched {} images'.format(len(imgs)))

            counter += 1
            if len(imgs) > 0:
                found = True

    def _get_search_term(self):
        try:
            return get_random_search_term()
        except Exception as e:
            self.logger.error('something broke with the generated python code')
            raise e

    def _get_bing_url(self, keyword, adult_content=True, large_size=True):
        if large_size:
            keyword += '+filterui:imagesize-large'

        url = "http://www.bing.com/images/search?q=" + keyword + "&FORM=HDRSC2"

        if adult_content:
            url += '&adlt=off'

        return url
{'fraction_non_alphanumeric': 0.09473684210526316, 'fraction_numerical': 0.004678362573099415, 'mean_word_length': 2.2150375939849622, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '725025', 'n_tokens_mistral': 1258, 'n_tokens_neox': 1203, 'n_words': 316}