Ejemplo n.º 1
0
def give_valentine_present(*arg):
    present_list = file_io.read('valentine.txt')
    present = misc.choice(present_list)
    search_result = google_image.search(present)
    if 'images' in search_result:
        for url in search_result['images']:
            if not url.endswith(('.jpg', '.gif', '.png')):
                continue
            try:
                web.download(url, '/tmp/present')
                break
            except:
                continue
    present = normalize.normalize(present)
    return {'text': '%nameに' + present + 'をヽ(´ー`)ノ', 'media[]': '/tmp/present'}
Ejemplo n.º 2
0
 def _get_title(self, url):
     title = ''
     root, ext = os.path.splitext(url)
     if ext in image_extensions:
         time.sleep(3)  # for avoiding to be treated as spam by Google
         logger.info('Search by google: %s' % url)
         results = google_image.search(url, best_kwds_max_length=18)
         keywords = filter(lambda x: not x.isdigit(), results['best_keywords'])
         title = ''.join(keywords)
     elif not ext in ignore_extensions:
         logger.info('Retrieve web resource: %s' % url)
         html = web.open_url(url)
         soup = BeautifulSoup(html, "html5lib")
         if soup.title and soup.title.string:
             title = soup.title.string
             title = normalize.normalize(title)
             title = self._shorten_title(title)
     return title
Ejemplo n.º 3
0
 def _get_title(self, url):
     title = ''
     root, ext = os.path.splitext(url)
     if ext in image_extensions:
         time.sleep(3)  # for avoiding to be treated as spam by Google
         logger.info('Search by google: %s' % url)
         results = google_image.search(url, best_kwds_max_length=18)
         keywords = filter(lambda x: not x.isdigit(),
                           results['best_keywords'])
         title = ''.join(keywords)
     elif not ext in ignore_extensions:
         logger.info('Retrieve web resource: %s' % url)
         html = web.open_url(url)
         soup = BeautifulSoup(html, "html5lib")
         if soup.title and soup.title.string:
             title = soup.title.string
             title = normalize.normalize(title)
             title = self._shorten_title(title)
     return title
Ejemplo n.º 4
0
def give_present(*arg):
    present_list = file_io.read('present.txt')
    sentence = misc.choice(present_list)
    while ('集計' in sentence or 'シュウケイ' in sentence or 'を' not in sentence or
            sentence.endswith('萌え') or len(sentence) < 3):
        sentence = misc.choice(present_list)
    present = normalize.remove_emoticon(sentence)
    present = present[:-1] if present.endswith('を') else present
    search_result = google_image.search(present)
    if 'images' in search_result:
        for url in search_result['images']:
            if url.endswith(('.jpg', '.gif', '.png')):
                try:
                    web.download(url, '/tmp/present')
                    break
                except:
                    continue
    sentence = normalize.normalize(sentence)
    return {'text': u'%nameに' + sentence, 'media[]': '/tmp/present'}
Ejemplo n.º 5
0
def give_present(*arg):
    present_list = file_io.read('present.txt', data=True)
    sentence = misc.choice(present_list)
    while ('集計' in sentence or 'シュウケイ' in sentence or 'を' not in sentence or
            sentence.endswith('萌え') or len(sentence) < 3):
        sentence = misc.choice(present_list)
    present = normalize.remove_emoticon(sentence)
    present = present.replace('!', '').replace('!', '')
    present = present.replace('漏れの', '').replace('俺の', '').replace('俺が', '')
    present = present[:-1] if present.endswith('を') else present
    search_result = google_image.search(present)
    if 'images' in search_result:
        for url in search_result['images']:
            if url.endswith(('.jpg', '.gif', '.png')):
                try:
                    web.download(url, '/tmp/present')
                    break
                except:
                    continue
    sentence = normalize.normalize(sentence)
    return {'text': '%nameに' + sentence, 'media[]': '/tmp/present'}
Ejemplo n.º 6
0
def test_search():
    with mock.patch('lib.google_image.GoogleImageSearch.search_by_url') as m:
        m.return_value = get_search_result('search_by_image.html')
        actual = google_image.search('http://mamigazou')
        assert_true('巴マミ' in actual['best_guess'])
        assert_true('巴マミ' in actual['best_keywords'])
Ejemplo n.º 7
0
def test_search():
    with mock.patch('lib.google_image.GoogleImageSearch.search_by_url') as m:
        m.return_value = get_search_result('search_by_image.html')
        actual = google_image.search('http://mamigazou')
        assert '巴マミ' in actual['best_guess']
        assert '巴マミ' in actual['best_keywords']