From d99666a52cb7b7d7b8f2fe8bdb355b2c995b069c Mon Sep 17 00:00:00 2001 From: nckmml Date: Sat, 19 Jan 2019 19:04:25 +0100 Subject: [PATCH 1/5] Update image search API from old deprecated one --- api.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/api.py b/api.py index f1ded9a..8c264c8 100644 --- a/api.py +++ b/api.py @@ -4,6 +4,8 @@ import random logger = logging.getLogger() +ApiKey = '' # insert your Google-API key here +CustomSearch = '' #insert your cx ID here # Surpress warnings 'Unverified HTTPS request is being made" requests.packages.urllib3.disable_warnings() @@ -63,15 +65,15 @@ def send_photo(self, chat_id, photo, **kwargs): class GoogleImagesAPI(API): def __init__(self): - self.url = 'https://ajax.googleapis.com/ajax/services/search/' + self.url = 'https://www.googleapis.com/customsearch/' def request(self, method, endpoint, **kwargs): content = super(GoogleImagesAPI, self).request(method, endpoint, **kwargs) - return content['responseData']['results'] + return content['items'] def images(self, query, results=8): assert 1 <= results <= 8 - return self.get('images', v='1.0', rsz=results, q=query) + return self.get('v1', key=ApiKey, cx=CustomSearch, num=results, q=query, alt='json', searchType='image') def random_image(self, query, results=8): results = self.images(query, results) From 89a40d51f7d307b9a3ea615e14ed886e4e1553be Mon Sep 17 00:00:00 2001 From: nckmml Date: Sat, 19 Jan 2019 19:05:47 +0100 Subject: [PATCH 2/5] Adapt to new custom search --- bot.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index a021f94..8cb26fe 100755 --- a/bot.py +++ b/bot.py @@ -25,7 +25,7 @@ def __init__(self, token): # Define valid commands self.commands = { - 'image': self.image + 'img': self.image } def image(self, text): @@ -35,7 +35,7 @@ def image(self, text): if image is None: found = "nothing" else: - found = image['url'] + found = image['link'] returntext = ('Searched for {}, found {}'.format(text, found)) return returntext @@ -83,7 +83,7 @@ def respond(self, message): bot = TelegramBot(token=args.token) offset = args.offset if args.offset else 0 - wait = args.wait if args.wait else 15 + wait = args.wait if args.wait else 5 while True: logger.info('Waiting {} seconds'.format(wait)) time.sleep(wait) From 3cd29e5096697ca5f56309611bc955ab9b9dfc68 Mon Sep 17 00:00:00 2001 From: nckmml Date: Sat, 19 Jan 2019 19:10:31 +0100 Subject: [PATCH 3/5] adapt readme.md to new modifications --- README.md | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 82e6c67..b6d7728 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,8 @@ Contact BotFather on Telegram (https://telegram.me/botfather) via private messag This token is used to authenticate requests to the bot API. -### Run docker container -Automated build on Docker Hub is available. - -``` -docker run -i --name googleimagebot stanislavb/telegram-googleimagebot 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw -``` +### Insert your API key and Custom Search ID +edit the api.py and copy your API key and Custom Search ID into the variables on top and build the container or start it without docker ### Build docker image by yourself Handy instructions are in the Makefile. @@ -23,3 +19,19 @@ Handy instructions are in the Makefile. make build TOKEN=110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw make run ``` + +### Without docker + +start the bot.py with your Token: + +``` +$ bot.py 110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw +``` + +### Usage + +Add the bot to a telegram group or write a message to him with + +``` +/img query +``` From af0190ad91d890d18bcc0fd942cf3116fed4c342 Mon Sep 17 00:00:00 2001 From: nckmml Date: Sat, 19 Jan 2019 19:14:11 +0100 Subject: [PATCH 4/5] limited search queries information --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index b6d7728..7eab515 100644 --- a/README.md +++ b/README.md @@ -35,3 +35,5 @@ Add the bot to a telegram group or write a message to him with ``` /img query ``` + +The Google Custom Search API is limited to 100 search queries per day. From 203c9dbfbd24d42cf62a9e8a0f7398cfa06bb6b9 Mon Sep 17 00:00:00 2001 From: nckmml Date: Sun, 20 Jan 2019 12:44:51 +0100 Subject: [PATCH 5/5] catched KeyError if search has no or invalid results --- api.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/api.py b/api.py index 8c264c8..4bcf69b 100644 --- a/api.py +++ b/api.py @@ -47,8 +47,12 @@ def request(self, method, endpoint, **kwargs): content = super(TelegramAPI, self).request(method, endpoint, **kwargs) if not content['ok']: logger.error('API returned error: {}'.format(content['description'])) - return content['result'] - + try: + return content['items'] + except KeyError as e: + logger.error('Invalid or no search result') + return None + def get_me(self): return self.get('getMe')